0
$\begingroup$

I'm attempting to import animations from text files but I can't seem to figure out how to properly import it. If I make a list of positions in a text file like here

from mathutils import Vector, Quaternion, Matrix, Euler
bpy.context.scene.frame_set(1) 
M1 = Matrix.Translation(Vector((0, 0, 0)))
bpy.data.objects['Armature'].pose.bones[0].matrix = M1
bpy.data.objects['Armature'].pose.bones[0].keyframe_insert('location')
bpy.data.objects['Armature'].pose.bones[0].keyframe_insert('rotation_quaternion') 
M2 = Matrix.Translation(Vector((0, 0, 2))) 
bpy.data.objects['Armature'].pose.bones[1].matrix = M2
bpy.data.objects['Armature'].pose.bones[1].keyframe_insert('location')
bpy.data.objects['Armature'].pose.bones[1].keyframe_insert('rotation_quaternion')  
M3 = Matrix.Translation(Vector((0, 2, 4)))
bpy.data.objects['Armature'].pose.bones[2].matrix = M3
bpy.data.objects['Armature'].pose.bones[2].keyframe_insert('location')
bpy.data.objects['Armature'].pose.bones[2].keyframe_insert('rotation_quaternion')  

https://pastebin.com/HRp8pbTn

Full code I type in to console in the pastebin. Typing that gets me the animation as I exported with no issues that I can see. If I write a loop for it however like so

    bpy.ops.object.mode_set(mode = 'POSE')
    for frame in range(transform_count):
        current_frame = frame + 1
        bpy.context.scene.frame_set(current_frame)
        for node in node_list:
            node_rotation = processed_file[frame_index + node_index + 7].split()
            node_translation = processed_file[frame_index + node_index + 8].split()
            node_scale = processed_file[frame_index + node_index + 9]
            matrix_translation = Matrix.Translation(Vector((float(node_translation[0]), float(node_translation[1]), float(node_translation[2]))))
            matrix_rotation = Quaternion((float(node_rotation[3]), float(node_rotation[0]), float(node_rotation[1]), float(node_rotation[2]))).inverted().to_matrix().to_4x4()
            full_matrix =  matrix_translation @ matrix_rotation
            armature.pose.bones[node].matrix = full_matrix
            armature.pose.bones[node].keyframe_insert('location')
            armature.pose.bones[node].keyframe_insert('rotation_quaternion')
            frame_index += 3

        frame_set += 1

The position of the bones are all wrong. It's almost like the matrix is either being ignored or it's taking the matrix I give it and adding it the the pose bones current position. I want it to be exactly at the matrix I give it which is a global matrix. What am I doing wrong here?

$\endgroup$
0
$\begingroup$

Added

bpy.context.view_layer.update()

right after setting the matrix. That fixed my issue.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.