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("==&gt;&gt;")
        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.
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.