Previous Thread  Next Thread

chat icon NLA.NewAction.setActive() overwrites Animations

sabooakhter

Posted: Sun Jul 08, 2012 7:11 am
Joined: 08 Jul 2012
Posts: 1
Hi there,

I'm using blender 2.49b to import multiple animation files that are in a different file format. I found a script to import these animations but it constantly writes over my previous animation. Any help would be awesome, thanks.

Here's the excerpt of code I believe is giving the problem:

Code:

action_name = Blender.sys.basename(file_object.name)
action_name = action_name[0:action_name.find('.')]

action = Blender.Armature.NLA.NewAction(action_name)
action.setActive(armature_object)

for x in xrange(mesh_count):
     read_mesh_transformations(file_object, version, armature_object)

def ani_read_mesh_transformations(file_object, version, armature_object):
    key_frames = {}

    armature_pose = armature_object.getPose()

    armature_pose_bones = armature_pose.bones

    mesh_name = file_object.read(40)

    mesh_name = mesh_name[0:mesh_name.find('\0')]

    mesh_name = elu_to_blender_name(mesh_name)

    print "transform %s start 0x%x" % (mesh_name, file_object.tell() - 44)

    pm = struct.unpack('<16f', file_object.read(64))

    pose_matrix = Blender.Mathutils.Matrix([pm[0], pm[2], pm[1], pm[3]], \
                                           [pm[8], pm[10], pm[9], pm[11]], \
                                           [pm[4], pm[6], pm[5], pm[7]], \
                                           [pm[12], pm[14], pm[13], pm[15]])

    translation_count = struct.unpack('<I', file_object.read(4))[0]

#    print "translation count %d" % translation_count

    for x in xrange(translation_count):
        tx, ty, tz = struct.unpack('<3f', file_object.read(12))

        translation = Blender.Mathutils.Vector(tx, tz, ty)

        key_frame_second = int(struct.unpack('<I', file_object.read(4))[0] / 60)

        translation_matrix = Blender.Mathutils.TranslationMatrix(translation)

        key_frames[key_frame_second] = translation_matrix

    rotation_count = struct.unpack('<I', file_object.read(4))[0]

#    print "rotation count %d" % rotation_count

    for x in xrange(rotation_count):
        rotation_matrix = Blender.Mathutils.Matrix()

        rotation_matrix.identity()

        rx, ry, rz, rw = struct.unpack('<4f', file_object.read(16))

        if version >= 0x1003:
            rotation = Blender.Mathutils.Quaternion(-rw, rx, rz, ry)

            rotation_matrix *= rotation.toMatrix().resize4x4()
        else:
            rotation = Blender.Mathutils.Quaternion([rx, rz, ry], math.degrees(-rw))

            rotation_matrix *= rotation.toMatrix().resize4x4()

        key_frame_second = int(struct.unpack('<I', file_object.read(4))[0] / 60)

        if key_frame_second in key_frames:
            key_frames[key_frame_second] = rotation_matrix * key_frames[key_frame_second]
        else:
            parent_inverse_pose_matrix = Blender.Mathutils.Matrix()

            parent_inverse_pose_matrix.identity()

            if mesh_name in armature_pose_bones.keys():
                if armature_pose_bones[mesh_name].parent is not None:
                    parent_inverse_pose_matrix *= armature_pose_bones[mesh_name].parent.poseMatrix.copy()

            parent_inverse_pose_matrix.invert()

            key_frames[key_frame_second] = rotation_matrix * Blender.Mathutils.TranslationMatrix((pose_matrix * parent_inverse_pose_matrix).translationPart())

    if mesh_name in armature_pose_bones.keys():
        if len(key_frames) > 0:
            for key_frame_second, key_frame_transformation in key_frames.iteritems():
                if armature_pose_bones[mesh_name].parent is not None:
                    key_frame_transformation *= armature_pose_bones[mesh_name].parent.poseMatrix.copy()

                armature_pose_bones[mesh_name].poseMatrix = key_frame_transformation

                armature_pose.update()

                armature_pose_bones[mesh_name].insertKey(armature_object, key_frame_second + 1)

            armature_pose_bones[mesh_name].poseMatrix = pose_matrix

            armature_pose.update()
        else:
            armature_pose_bones[mesh_name].poseMatrix = pose_matrix

            armature_pose.update()

            armature_pose_bones[mesh_name].insertKey(armature_object, 1)
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group