1
$\begingroup$

I used to export armatures in 2.49 using the following code:

# armature
for bone in data.bones.values():
    pariente = bone.parent
    if (pariente):
        matriz_p = Blender.Mathutils.Matrix(pariente.matrix['ARMATURESPACE'])

    matriz = Blender.Mathutils.Matrix(bone.matrix['ARMATURESPACE'])
    if (pariente):
        matriz = matriz*matriz_p.invert()

    trans = matriz.translationPart()
    rot = matriz.toQuat()
    rot = rot.normalize()

    buf = "\n" + str(trans.x) + " " + str(trans.y) + " " + str(trans.z)
    buf += "\n" + str(rot.x) + " " + str(rot.y) + " " + str(rot.z) + " " + str(rot.w)
    file.write(buf)


#keyframes
for bone in pose.bones.values():        

        buf = "\n" + str(bone.loc.x) + " " + str(bone.loc.y) + " " + str(bone.loc.z)
        buf += "\n" + str(bone.quat.x) + " " + str(bone.quat.y) + " " + str(bone.quat.z) + " " + str(bone.quat.w)
        buf += "\n" + str(bone.size.x) + " " + str(bone.size.y) + " " + str(bone.size.z)            

        file.write(buf)

I tried to convert it for use in 2.69 and i got this:

for bone in obj.data.bones:
pariente = bone.parent

if (pariente):
    matriz_p = mathutils.Matrix(pariente.matrix_local)

matriz = mathutils.Matrix(bone.matrix_local)
if (pariente):
    matriz_p_inv = matriz_p
    matriz_p_inv.invert()
    matriz = matriz_p_inv * matriz

trans = matriz.to_translation()
rot =  matriz.to_quaternion()
rot.normalize()

buf = str(trans.x) + " " + str(trans.y) + " " + str(trans.z) + "\n"
buf += str(rot.x) + " " + str(rot.y) + " " + str(rot.z) + " " + str(rot.w) + "\n"
file.write(buf)

buf = str( int(obj.animation_data.action.frame_range[1]) ) + "\n"
file.write(buf)

Scene = bpy.context.scene

for bone in obj.pose.bones:
for Frame in range( int(obj.animation_data.action.frame_range[0]), int(obj.animation_data.action.frame_range[1]) + 1 ):
    Scene.frame_set(Frame)

    pariente = bone.parent
    if (pariente):
        matriz_p = mathutils.Matrix(pariente.matrix_channel)

    matriz_l = mathutils.Matrix(bone.bone.matrix_local)
    matriz_l.invert()
    matriz = mathutils.Matrix(bone.matrix)
    if (pariente):
        matriz_p_inv = matriz_p
        matriz_p_inv.invert()
        matriz = (matriz_l*matriz_p_inv) * matriz
    else:
        matriz = matriz_l * matriz

    trans = matriz.to_translation()
    rot =  matriz.to_quaternion()
    rot.normalize()

    buf = str(trans.x) + " " + str(trans.y) + " " + str(trans.z) + "\n"
    buf += str(rot.x) + " " + str(rot.y) + " " + str(rot.z) + " " + str(rot.w) + "\n"
    file.write(buf) 

I based it on the way the milkshape exporter works, but it seems the keyframe matrices are wrong because it gives me an incorrect rotation and scale.

So my question is: how do i get the same pose matrix data as 2.49 in 2.69?

Thanks in advance and sorry for the long post.

$\endgroup$
  • $\begingroup$ The order matters in vector/matrix multipliction, and there was a change in 2.62 from column-major to row-major IIRC. Maybe that is causing you trouble? BTW: you can do this buf = "\n" + str(bone.loc.x) + " " + str(bone.loc.y) + " " + str(bone.loc.z) more readable like buf = "\n%s %s %s" % bone.loc[:] (although I recommend %f, e.g. %.6f to always use 6 decimal places). $\endgroup$ – CodeManX Jan 24 '14 at 16:23
  • $\begingroup$ Thanks! I'm not very good with python and i didn't know you could format the text output, so thanks for that again. I tried switching the order of the multiplications but it just got worse. Also i'm not really sure what is the difference between "matrix_channel", "matrix_basis" and "matrix" in the pose bones. I also tried exchanging them but it didn't work either. $\endgroup$ – Miguel Jan 24 '14 at 17:11
  • $\begingroup$ The matrix attribute explanations here: blender.org/documentation/blender_python_api_2_69_9/… $\endgroup$ – CodeManX Jan 24 '14 at 17:15
  • $\begingroup$ I solved it. The problem wasn't in the matrices but in the model's vertex weights. Thanks anyway. $\endgroup$ – Miguel Jan 31 '14 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.