Exporting Scene Transforms

2014/12/21 19:53

A quick and simple script to export the transforms for all objects in blender under a certain branch of the scene tree. Call ExportTransforms(“transforms.xml”,”root”), replacing “root” with whatever your root node is named.

Using the below source the XML will be hierarchical (in that a child node will appear under it’s parents) with a little change you can make it a single layer and have each node contain it’s parent’s id as an attribute. The transforms obtained from Python all appear to be in world space. You can change the code again to make them all relative to their parents.

GetChildren(ob): is a very useful script for getting all the immediate children of a given object in Blender.


import Mathutils
from Mathutils import *
 
def GetChildren(ob):
    return [ob_child for ob_child in bpy.data.objects if ob_child.parent == ob]
#def
 
#---------------------------------------------------
# MatrixToString(mtx):
#---------------------------------------------------
#   objs: list of objects
#   fs: file stream
# Description
#   recursively print out mesh data
#---------------------------------------------------
def MatrixToString(mtx):   
    mtx = Matrix(mtx)
    rot = mtx.to_quat()
    trans = mtx.translation_part()
    scale = mtx.scale_part()   
     
    strTrans = str(rot.x) + "," + str(rot.y) + "," + str(rot.z) + "," + str(rot.w) + ","
    strTrans = strTrans + str(trans[0]) + "," + str(trans[1]) + "," +str(trans[2]) + ","
    strTrans = strTrans + str(scale[0]) + "," + str(scale[1]) + "," +str(scale[2])
     
    return '<transform type="QuatTransScale">' + strTrans + '</transform>'
#end def
 
#---------------------------------------------------
# PrintCharacterData(obs, fs):
#---------------------------------------------------
#   objs: list of objects
#   fs: file stream
# Description
#   recursively print out mesh data
#---------------------------------------------------
def PrintTransformData(objs, fs):
    idIndex = 0
    for ob in objs:
     
        #--------------------------------------------------
        # HEADER
        # Print out the main data
        #--------------------------------------------------                        
        fs.write('<transform name="'+ob.name+'" id="'+str(idIndex)+'">')
        fs.write(MatrixToString(ob.matrix))        
                 
        #--------------------------------------------------
        #if we have any mesh children print them out too           
        #--------------------------------------------------
        children = [child for child in GetChildren(ob)]
        if len(children) > 0:
            PrintTransformData(children, fs)
        #endif
         
        #-----------------------------------------------
        # FOOTER
        #-----------------------------------------------
        fs.write('</transform>')
        idIndex = idIndex + 1      
    #endfor            
#end def
 
 
#---------------------------------------------------
# WriteTransforms(obs, fs):
#---------------------------------------------------
def WriteTransforms(strName, fs):
 
    obs = []   
     
    #Add the parent to the head
    obs.insert(0,bpy.data.objects[strName])
     
    fs.write('<transform_data>\n')
    PrintTransformData(obs, fs)
    fs.write('</transform_data>')
#enddef
 
def ExportTransforms(strFileName, strNodeName):
    fs = open(strFileName,'w') 
         
    WriteTransforms(strNodeName,fs)        
     
    fs.close()
    print("Transforms: "+strFileName+" Done")
#enddef
 
#run this...
ExportTransforms("e:/temp/transforms.xml","root")