Apply rotation and translation to bones
<p>Edit*
I asked the question on blender.stackexchange.com.
<a href="https://blender.stackexchange.com/q/58524/27834">https://blender.stackexchange.com/q/58524/27834</a></p>

<hr>

<p>I'm using blender 2.49 because the script was originally written for that version.</p>

<p>I'm trying to create animations using some transform data.</p>

<p>The original data contains a sequence of joints. Each joint has its own rotation and translation values, it may or may not have a parent joint. This sounds pretty much like bones in blender, right?</p>

<p><strong>To set up the skeleton:</strong></p>

<pre><code>#pseudocode
for joint in joints:
    v = Vector(0,0,0)
    j=joint
    while j:
        v = j.rotation*v
        v = v + j.location
        j = j.parent
    joint.position = v
</code></pre>

<p><strong>To process a certain frame:</strong></p>

<pre><code>#pseudocode
for joint in joints:
    v = Vector(0,0,0)
    j=joint
    while j:
        rot=j.rotation+j.curFrame.dRotation
        loc=j.location+j.curFrame.dLocation
        v = rot*v
        v = v + loc
        j = j.parent
    joint.position = v
</code></pre>

<hr>

<p>To make things simple, I convert each joint into a small bone.</p>

<pre><code>    for i in range(0, len(bones)):
        bone = Blender.Armature.Editbone()
        bone.head = Mathutils.Vector(0,0,0)
        bone.tail = Mathutils.Vector(0,1,0)
        bone.name = 'Bone.' + str(i)
        bone.parent = armature.bones['Bone.' + str(bones[i].parent)]
        armature.bones[bone.name] = bone
</code></pre>

<p>Then I move the bones to their desired locations, using the posebone trick to create the skeleton:</p>

<pre><code>    pose=armObject.getPose()
    pose_bones=pose.bones
    #pose the bones
    for i in range(0, len(bones)):
        name = 'Bone.'+str(i)
        tran = transforms[i]
        pose.bones[name].quat = Mathutils.Euler(tran.rotX,tran.rotY,tran.rotZ).toQuat()
        pose.bones[name].loc = Mathutils.Vector(tran.locX, tran.locY, tran.locZ)
    pose.update()
    #update editbones
    armature.makeEditable()
    for i in range(0, len(bones)):
        name = 'Bone.'+str(i)
        armature.bones[name].matrix = pose.bones[name].poseMatrix
    armature.update()
    #reset pose
    for i in range(0, len(bones)):
        name = 'Bone.'+str(i)
        pose.bones[name].quat = Mathutils.Euler(0,0,0).toQuat()
        pose.bones[name].loc = Mathutils.Vector(0,0,0)
    pose.update()
</code></pre>

<p>So far so good. But when I apply the transform to a certain frame, the results are incorrect.</p>

<pre><code>    for i in range(0, len(bones)):
        name = 'Bone.'+str(i)
        tran = frame.dTransforms[i]
        pose.bones[name].quat = Mathutils.Euler(tran.rotX,tran.rotY,tran.rotZ).toQuat()
        pose.bones[name].loc = Mathutils.Vector(tran.locX, tran.locY, tran.locZ)
</code></pre>

<p>By incorrect I mean the rotations seem correct, but the translations are all wrong. I guess it is because the transforms should be applied to the posebone's local space(the skeleton is correct because all transforms start from the center )? But how do I do that?</p>