What am I doing wrong here? (armatures/bones)

Hi everyone.

I’m trying to learn the blender armature API to write later some modding tools for a game.

So far I’m just trying to create an armature “manually” using the API. My problem is that while the bones get positioned right, blender becomes extremely slow (sluggish UI response, and sub-1 fps on the viewports) after loading the armature. Sometimes if I manage to get into edit mode, the dotted lines I assume are showing bone parenting are all wrong.

Here is my code, I’m using blender 2.37:


#!BPY

"""
Name: 'sktest...'
Blender: 237
Group: 'Import'
Tooltip: 'skellies'
"""

from Blender import *


armature = Armature.New()

	
bone = Armature.Bone.New("0")
bone.setHead(0,0,0)
bone.setTail(0.000298, -0.004835, 0.91041)
armature.addBone(bone)

bone1 = Armature.Bone.New("1")
armature.addBone(bone1)
bone1.setParent(bone)
bone1.setHead(0,0,0)
bone1.setTail(0.108937, 0.062864, 0.040887)

bone2 = Armature.Bone.New("2")
armature.addBone(bone2)
bone2.setParent(bone1)
bone2.setHead(0,0,0)
bone2.setTail(0.000628, 0.464914, 0.015248)

bone3 = Armature.Bone.New("3")
armature.addBone(bone3)
bone3.setParent(bone2)
bone3.setHead(0,0,0)
bone3.setTail(0, 0.472481, 0)

bone4 = Armature.Bone.New("4")
armature.addBone(bone4)
bone4.setParent(bone)
bone4.setHead(0,0,0)
bone4.setTail(-0.112097, 0.061595, 0.041196)

bone5 = Armature.Bone.New("5")
armature.addBone(bone5)
bone5.setParent(bone4)
bone5.setHead(0,0,0)
bone5.setTail(0.002596, 0.46697, 0.015169)

bone6 = Armature.Bone.New("6")
armature.addBone(bone6)
bone6.setParent(bone5)
bone6.setHead(0,0,0)
bone6.setTail(0, 0.469849, 0)

bone7 = Armature.Bone.New("7")
armature.addBone(bone7)
bone7.setParent(bone)
bone7.setHead(0,0,0)
bone7.setTail(0.001283, 0.197217, 0)

bone8 = Armature.Bone.New("8")
armature.addBone(bone8)
bone8.setParent(bone7)
bone8.setHead(0,0,0)
bone8.setTail(0.000184, 0.198524, 0)

bone9 = Armature.Bone.New("9")
armature.addBone(bone9)
bone9.setParent(bone8)
bone9.setHead(0,0,0)
bone9.setTail(0, 0.249982, -0)

bone10 = Armature.Bone.New("10")
armature.addBone(bone10)
bone10.setParent(bone8)
bone10.setHead(0,0,0)
bone10.setTail(0.078681, 0.172112, -0.001312)

bone11 = Armature.Bone.New("11")
armature.addBone(bone11)
bone11.setParent(bone10)
bone11.setHead(0,0,0)
bone11.setTail(0, 0.131228, 0)

bone12 = Armature.Bone.New("12")
armature.addBone(bone12)
bone12.setParent(bone11)
bone12.setHead(0,0,0)
bone12.setTail(0, 0.27752, 0)

bone13 = Armature.Bone.New("13")
armature.addBone(bone13)
bone13.setParent(bone12)
bone13.setHead(0,0,0)
bone13.setTail(-0.00011, 0.261443, -0.000857)

bone14 = Armature.Bone.New("14")
armature.addBone(bone14)
bone14.setParent(bone13)
bone14.setHead(0,0,0)
bone14.setTail(0.013755, 0.066674, -0.007564)

bone15 = Armature.Bone.New("15")
armature.addBone(bone15)
bone15.setParent(bone8)
bone15.setHead(0,0,0)
bone15.setTail(-0.08304, 0.176294, 0.002573)

bone16 = Armature.Bone.New("16")
armature.addBone(bone16)
bone16.setParent(bone15)
bone16.setHead(0,0,0)
bone16.setTail(-0, 0.130785, 0)

bone17 = Armature.Bone.New("17")
armature.addBone(bone17)
bone17.setParent(bone16)
bone17.setHead(0,0,0)
bone17.setTail(-0, 0.277457, 0)

bone18 = Armature.Bone.New("18")
armature.addBone(bone18)
bone18.setParent(bone17)
bone18.setHead(0,0,0)
bone18.setTail(0.00011, 0.271744, -0.000945)

bone19 = Armature.Bone.New("19")
armature.addBone(bone19)
bone19.setParent(bone18)
bone19.setHead(0,0,0)
bone19.setTail(-0.012878, 0.064701, -0.017021)


armObj = Object.New('Armature', "armat")
armObj.link(armature)
scn = Scene.getCurrent()
scn.link(armObj)

Ok, figured it out…

From the Armature API docs:

Warning: If a bone is added to the armature with no parent if will not be parented. You should set the parent of the bone before adding to the armature.

So I just put all the addBone lines after the setParent ones and it works beautifully.