# Blender Python - exporting bone matrices for animation relative to parent

I am making a homegrown 3d engine for my projects. So far, I am able to export:

• mesh
• materials
• Node hierarchy

Each node in my engine has it own mesh(optional), material(optional) and a PARENT-RELATIVE-TRANSFORMATION-MATRIX

While rendering, each node passes its own matrix down to each children. This matrix get multiplied with the child node's transformation matrix and gets passed down further.

This is working fine for static meshes with node hierarchy. However it is not working for Blender bones(I have a plugin for 3ds max also, and my engine is able to render bones correctly. So I think the issue is not on renderer side.)

I am getting all weird matrices for bone animations. Here is my script where I save the animation data for bones.

for node in nodes:
if node.pose_bone:
(start_frame, end_frame) = (0, 100)
animation = Animation(len(animations), int(start_frame), int(end_frame))
animations.append(animation)
node.animationIndex = animation.index

print("==>>")
for f in range(int(start_frame), int(end_frame) + 1):
bpy.context.scene.frame_set(f)
bpy.context.scene.update()

if node.pose_bone.parent is None:
bone_matrix = node.pose_bone.matrix_basis
else:
parent_matrix = node.pose_bone.parent.matrix_basis
bone_matrix = node.pose_bone.matrix_basis
bone_matrix = parent_matrix.inverted() * bone_matrix

m = bone_matrix

animation.translates.append( m.to_translation() )
animation.rotates.append( m.to_quaternion() )
animation.scales.append( m.to_scale() )


Please help me to get correct pose matrices for bones. In my engine these need to be relative to their parent.

• possible duplicate of How do you export bones relative to parent? – Ray Mairlot Aug 21 '14 at 13:18
• I had already tried the solution as in that link. But it wont work for me. – Valyannan KV Aug 21 '14 at 14:20
• 'wont work' because you couldn't get it to work or because there are specific requirements for your code? If the latter please add additional information to your question. Thanks :) – Ray Mairlot Aug 21 '14 at 14:30
• "because there are specific requirements for your code?" yes. I need to get data for animation. while the linked question is for bind pose, if I understood correctly. I was not able to figure out a solution from the linked question. It would be great help, if anybody can rephrase information from the linked question to solve my issue. – Valyannan KV Aug 21 '14 at 15:39

After a couple of days of frustration, I have been able to get skeletal animation working fine.

Here are the details:
1] Bind pose is obtained from bone.matrix_local

bone_matrix = bone.matrix_local
if pose_bone.parent:
bone_matrix = bone.matrix_local.inverted() *   bone.matrix_local


2] Inverse bindpose also is obtained from bone.matrix_local. But this time it is not relative to parent bone.

offset_matrix = bone.matrix_local.copy().inverted()


3] Now the animation data is obtained from pose_bone.matrix as follows:

bone_matrix = pose_bone.matrix
if pose_bone.parent:
bone_matrix = pose_bone.parent.matrix.inverted() * pose_bone.matrix


This code works fine for me.

• bone.matrix_local.inverted() * bone.matrix_local should always return an identity matrix... – user253751 Jul 12 '15 at 23:17
• i assume he meant to type bone.parent.matrix_local.inverted() – winduptoy Apr 1 '17 at 14:30