Help exporting armatures

I need some help understanding the relationship between bones in an armature so I can write an export script.

In my target file I want to have an hierarchy starting at the armature position and going through each bone.

So for example supose I have this hierarchy

Armature: default transform (loc 0,0,0 rot 1,0,0,0 sca 1,1,1)
Root bone: loc ( -1.921, 0, 0 ), rot and sca still default
Child bone of “root”: loc (0, 0, -947), rot and sca still default

Now doing a little research I found out that for each bone calling bone.matrix_local will return the transform relative to the armature. What I really want is the transform relative to the parent bone, so I tried this.

mat = root_bone.matrix_local.inverted() * child_bone.matrix_local

First problem is doing a decompose on this matrix will give me this relative location: (1.921, -0.947, 0)

Now I can understand the translation at X being 1.921 as the child bone is offseted 1.921 blender units on the X axis relative to the root. But why is it offseted -0.947 on the Y axis? Neither the root bone or the child bone are translated at the Y axis and the child is translated on the Z axis but that axis doesn’t show up on the resulting matrix.

Am I doing something wrong? Is there some extra knowledge I’m missing when doing this?

This is just an example, when trying to understand rotations I also keep messing something up so maybe there is some major mistake on my part.

Keep in mind I’m pretty new to this and am writing this script to learn both Blender API and matrices. My goal is to export a Blender model to a game I intend to write.

Thanks for your help.

Armature-space should be Object-space, not the space of the root bone in the armature (there can be multiple roots btw.)

Maybe this function helps?
http://www.blender.org/documentation/blender_python_api_2_68_release/bpy.types.Object.html#bpy.types.Object.convert_space

I looked into this function, but to my understanding I’m not really converting spaces. What I’m doing there is converting a transform matrix relative to the armature to a transform matrix relative to the parent of a given bone.

This function can be useful though to convert from pose space to local space, so thanks for the tip.