importing animations in Blender, weird rotations/locations

Posted by user975135 on Game Development See other posts from Game Development or by user975135
Published on 2012-07-11T08:42:06Z Indexed on 2012/07/11 9:23 UTC
Read the original article Hit count: 288

This is for the Blender 2.6 API.

There are two problems:

1. When I import a single animation frame from my animation file to Blender, all bones look fine. But when I import multiple (all of the frames), just the first one looks right, seems like newer frames are affected by older ones, so you get slightly off positions/rotations.

This is true when both assigning PoseBone.matrix and PoseBone.matrix_basis.

bone_index = 0

# for each frame:
for frame_index in range(frame_count):
    # for each pose bone: add a key
    for bone_name in bone_names: # "bone_names" - a list of bone names I got earlier
        pose.bones[bone_name].matrix = animation_matrices[frame_index][bone_index] # "animation_matrices" - a nested list of matrices generated from reading a file

        # create the 'keys' for the Action from the poses
        pose.bones[bone_name].keyframe_insert('location', frame = frame_index+1)
        pose.bones[bone_name].keyframe_insert('rotation_euler', frame = frame_index+1)
        pose.bones[bone_name].keyframe_insert('scale', frame = frame_index+1)

        bone_index += 1

    bone_index = 0

Again, it seems like previous frames are affecting latter ones, because if I import a single frame from the middle of the animation, it looks fine.

2. I can't assign armature-space animation matrices read from a file to a skeleton with hierarchy (parenting).

In Blender 2.4 you could just assign them to PoseBone.poseMatrix and bones would deform perfectly whether the bones had a hierarchy or none at all.

In Blender 2.6, there's PoseBone.matrix_basis and PoseBone.matrix. While matrix_basis is relative to parent bone, matrix isn't, the API says it's in object space. So it should have worked, but doesn't. So I guess we need to calculate a local space matrix from our armature-space animation matrices from the files. So I tried multiplying it ( PoseBone.matrix ) with PoseBone.parent.matrix.inverted() in both possible orders with no luck, still weird deformations.

© Game Development or respective owner

Related posts about matrix

Related posts about blender