bone parent name and recursive problem

I futzed around with the bone.parent, and bone.hasParent() and tried stuffing a hash with a list of bone names and their parents, but ran into a snag trying to access the parent name :


1  scn= Scene.GetCurrent()
2  arm_ob= scn.getActiveObject()
3
4  bonehash = {} 	 	
5
6     for ob in scn.getChildren():
8      arm_data= arm_ob.getData()
9      bones= arm_data.bones.values()
10
11   for bone in bones:
12     bonehash[bone.name] = bone.parent
13
14  for b in bonehash:
15     print 'bone="',b,'" parent= "',bonehash<b>,'"',
16	parent = bonehash[b]
17	#pname = parent.name
18	
19	print 'parent = ',parent       # [B]"[Bone "Pelvis"]</b>
20	

21	#pname = parent[1]
22	#print 'parent name',pname
23	
24	if (bn == None):
25		print 'ROOT = ',b

Uncommenting line 17 gives 'AttributeError: ‘NoneType’ object has no attribute ‘name’, but if I do a ‘print dir(bn)’ it shows name as a valid attribute :confused:

Lines 21-22 give a ‘unscriptable object’ error.

How can I just get the ‘Pelvis’ (name) part of the bone?

I’m trying to print a ‘tree’ output listing of an armature, this has probably been done a few times, if anyone can point me towards a link, or post the code, that would be great.

i.e.

Pelvis
  Thigh.L
    Shin.L
     Foot.L
  Thigh.R
     Shin.R
       Foot.R
 .....

This is kinda working, except I’m not getting tabbed / indented columns.


def pchildren(bone):
	print '	'
	print 'pchildren: bone=',bone
	kids = {}
	kids = bone.getAllChildren()
	#print 'kids = ',kids
	if (kids):
		print '	'
		for b in kids:
			pchildren(b)
	print '==============================='

It’s called from :


for bone in bones:
	if not(bone.hasParent()):
		print '====ROOT====  "',bone.name,'"'
		pchildren(bone)			

It starts traversing correctly :


pchildren: bone= [Bone "Pelvis"]
pchildren: bone= [Bone "Hip.R"]
pchildren: bone= [Bone "Thigh.R"]
pchildren: bone= [Bone "Shin.R"]
pchildren: bone= [Bone "Foot.R"]

but then prints :

pchildren: bone= [Bone "Foot.R"]
pchildren: bone= [Bone "Shin.R"]
pchildren: bone= [Bone "Foot.R"]
pchildren: bone= [Bone "Foot.R"]

before correctly going on to :

pchildren: bone= [Bone "Hip.L"]
pchildren: bone= [Bone "Thigh.L"]
pchildren: bone= [Bone "Shin.L"]
pchildren: bone= [Bone "Foot.L"]

… then prints :

pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Shin.L"]
pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Thigh.L"]
pchildren: bone= [Bone "Shin.L"]
pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Shin.L"]
pchildren: bone= [Bone "Foot.L"]
pchildren: bone= [Bone "Foot.L"]

Mike