4
$\begingroup$

Situation: I have a project with 20+ scenes and hundreds of text objects in random places (a song).

Is it possible to put the text object data (text itself) into a text file(s), to edit externally without the need of searching the object inside blender? So that edited and saved text file will update the objects in project on restart of blender?

PS: text objects aren't keyframed (if that makes a change).

$\endgroup$
4
  • $\begingroup$ see if this answer helps $\endgroup$
    – Chebhou
    Mar 30, 2015 at 15:02
  • $\begingroup$ Hey thanks, it's neat and useful, but it doesn't solve the problem. $\endgroup$
    – markac
    Mar 30, 2015 at 18:33
  • $\begingroup$ do you want to export them to individual files then synch them at the end ? $\endgroup$
    – Chebhou
    Mar 30, 2015 at 18:34
  • $\begingroup$ Exactly, I was writing this, but it timed me out. xml or csv is also cool. $\endgroup$
    – markac
    Mar 30, 2015 at 18:47

1 Answer 1

4
$\begingroup$

This is an addon for writing text objects to .xml file and reading them back from the text file.

How to use:

  • you have a file with text objects in one or multiple scenes
  • have text objects
  • export to an xml file (file->export->text to-from xml )
  • edit text outside blender save the .xml
  • in the first .blend file (with the objects and scenes ) use the addon to update text objects from the .xml file ( this time select read_objects in the left side of the file browser )

enter image description here


install: from the addons tab click install from file and select this txt_xml.py file check the check-box to activate


UPDATE the addon file txt_xml.py

bl_info = {
    "name" : "text objects to-from xml",
    "author" : "chebhou",
    "version" : (1, 0),
    "blender" : (2, 7, 3),
    "location" : "file->export->text to-from xml",
    "discription" : "copys an text objectx from-to xml file",
    "warning" : "this add-on is extremly unusefull",
    "wiki_url" : " [email protected]",
    "tracker_url" : "[email protected]",
    "category" : "Import-Export"
    }

import bpy
from bpy.types import Operator
from bpy_extras.io_utils import ExportHelper
from bpy.props import EnumProperty, BoolProperty
from xml.dom import minidom
from xml.dom.minidom import Document

def txt_sync(filepath):
    dom = minidom.parse(filepath)
    scenes =dom.getElementsByTagName('scene')
    for scene in scenes:
        scene_name=scene.getAttribute('name')
        print("\n",scene_name)
        bl_scene = bpy.data.scenes[scene_name]
        txt_objs =scene.getElementsByTagName('object')
        for obj in txt_objs:
            obj_name = obj.getAttribute('name')
            obj_body = obj.childNodes[0].nodeValue 
            bl_obj = bl_scene.objects[obj_name].data.body = obj_body
            print(obj_name,"  ",obj_body)

def txt_export(filepath):

    doc = Document()
    root = doc.createElement('data')
    doc.appendChild(root)

    for sce in bpy.data.scenes :
        #create a scene
        scene = doc.createElement('scene')
        scene.setAttribute('name', sce.name)
        root.appendChild(scene)

        for obj in sce.objects :
            if obj.type == 'FONT':   
                #add object element
                object = doc.createElement('object')
                object.setAttribute('name', obj.name)
                txt_node = doc.createTextNode(obj.data.body)
                object.appendChild(txt_node) 
                scene.appendChild(object)

    #write to a file
    file_handle = open(filepath,"wb")

    file_handle.write(bytes(doc.toprettyxml(indent='\t'), 'UTF-8'))
    file_handle.close()


class   text_export(Operator, ExportHelper):  

    """write and read text objects to a file"""        
    bl_idname = "export_scene.text_xml"  
    bl_label = "text from-to xml"     
    bl_options = {'REGISTER', 'UNDO'}    #should remove undo ? 

    # ExportHelper mixin class uses this
    filename_ext = ".xml"

    #parameters and variables
    convert = EnumProperty(
                name="Convert",
                description="Choose conversion",
                items=(('W', "write objects", "write text objects to xml"),
                       ('R', "read objects", "read text objects from xml")),
                default='W',
                )

    #main function
    def execute(self, context): 
        bpy.ops.object.mode_set(mode = 'OBJECT')
        if self.convert == 'W':
            txt_export(self.filepath)
        else:
            txt_sync(self.filepath)

        bpy.context.scene.update()
        self.report({'INFO'},"Conversion is Done")
        return {'FINISHED'}


def menu_func_export(self, context):
    self.layout.operator(text_export.bl_idname, text="Text to-from xml")


def register():
    bpy.utils.register_class(text_export)
    bpy.types.INFO_MT_file_export.append(menu_func_export)


def unregister():
    bpy.utils.unregister_class(text_export)
    bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":  
    register()
$\endgroup$
3
  • $\begingroup$ I am receiving following message ! [error_msg] (pasteall.org/pic/show.php?id=86059). Am I doing something wrong? (I've also tried with blender 2.74) $\endgroup$
    – markac
    Mar 31, 2015 at 11:38
  • $\begingroup$ @markac I'll check and be back $\endgroup$
    – Chebhou
    Mar 31, 2015 at 11:58
  • $\begingroup$ @markac i have updated the code (the fault was in my code ) remove the old addon and install this one (export fresh file and import it to be sure it is working ) $\endgroup$
    – Chebhou
    Apr 1, 2015 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .