Absolute bone matrix to quaternion

Hello!

I’ve been trying to export the relative quaternion of each bone of a armature for a couple days now but only gotten weird results so far.
I’ve looked at a few examples, it seems right and I’ve checked my export and import scripts multiple times without finding the problem.
So if someone can confirm that this should work so I can focus on my import script i would be really happy!

for cbone in obj.data.bones:
    if cbone.parent is not None:
        my_mtx = cbone.parent.matrix_local.inverted() * cbone.matrix_local
    else:
        my_mtx = cbone.matrix_local
    my_quat = my_mtx.to_quaternion()

Note that I am only looking for the rotation part of the matrices, that’s why root bones aren’t multiplied by the armatures matrix.

Results:

It is the right way to do it, the problem I was having was that the coordinate systems were different.
I had to switch x to -x, y to z and z to y then rotate it in my program.