Attempting to write a utility function do convert a nested dict of bones, into an armature.
This will eventualy be used to makethe BVH importer support bones.
This script works in that it creates 3 bones, but I cant edit the bones or even see them.
Any ideas?
from Blender import Scene, Armature, Object, Mathutils
def dict2armatre(armDict):
'''Utility function that converts a dictionary to an armature
the armDict dictionary is a nested dictionary of items described below.
Each item is is a list formatted as follows.
[childrenDict, start, end, Bone=None]
* childrenDict is the child of the armDict.
* start and end are vectors representing the armature space locations of the bones rest position.
* Bone, starts off as None, but the 4th slot will havea bone added into it once the armature is created
Dictionary keys are all strings that are used to name the bones.
the example below shows 3 bones
armdct= {'hip':[{'knee':[{}, (0,0,1), (0,0,2), None]}, (0,0,0), (0,0,1), None], 'vehicle':[{}, (1,0,0), (2,0,0), None]}
..hip
....knee
..vehicle '''
def item2bone(name, data, selfdata, parentdata):
bone= selfdata[3]= Armature.Editbone()
bone.name= name
data.bones[bone.name]= bone
bone.head= Mathutils.Vector(selfdata[1])
bone.tail= Mathutils.Vector(selfdata[2])
if parentdata:
bone.parent= parentdata[3]
for childname, childdata in selfdata[0].iteritems():
# Make bones recursivly
item2bone(childname, data, childdata, selfdata)
armOb= Object.New('Armature')
armData= Armature.Armature('myArmature')
armOb.link(armData)
armData.makeEditable()
for childname, rootbonedata in armDict.iteritems():
item2bone(childname, armData, rootbonedata, None)
armData.update()
return armOb
if __name__ == '__main__':
v=Mathutils.Vector
armdct= {'hip':[{'knee':[{}, v(0,0,1), v(0,0,2), None]}, v(0,0,0), v(0,0,1), None], 'vehicle':[{}, v(1,0,0), v(2,0,0), None]}
ob= dict2armatre(armdct)
scn= Scene.GetCurrent()
scn.link(ob)
ob.sel= 1