Is there a way to transform animation data from a .lab file to blender?

  Kiến thức lập trình

I have an animation file made several years ago in a .lab file, I was able to get all the data from that file to a json file that describes all the information of the skeleton and the animations. I want to get that skeleton and animations now in blender using python and I was able to recreate the skeleton, but when I get to the animation part it is very confusing to know what to do with the data, everything I try leads to poses where the bones are so off from where they need to be and I don’t know why. This is the json data I could get from the .lab file (Reduced to 3 bones only because the data is huge). I was able to replicate the skeleton using the _base_seq data alongside the _invmat_seq data which is a matrix information and this worked fine. The _key_seq is an array of the size of the number of bones and each element in the array holds the data of the bone for the animation but this data has a pos_seq and a quat_seq (Both are arrays of size if the animation frames) that has information of the bones in each frame of the animation. In blender I tried using the bone.location and bone.rotation_quaternion but it does not work out of the box. Does anyonw has an idea of how I can do this? I am using blender 3.

JSON data:

{
    "_header": {
        "bone_num": 3,
        "frame_num": 3,
        "dummy_num": 2,
        "key_type": 3
    },
    "_base_seq": [
        {
            "name": [
                "B",
                "i",
                "p",
                "0",
                "1"
            ],
            "id": 0,
            "parent_id": 4294967295
        },
        {
            "name": [
                "B",
                "i",
                "p",
                "0",
                "1",
                " ",
                "F",
                "o",
                "o",
                "t",
                "s",
                "t",
                "e",
                "p",
                "s"
            ],
            "id": 1,
            "parent_id": 0
        },
        {
            "name": [
                "B",
                "i",
                "p",
                "0",
                "1",
                " ",
                "P",
                "e",
                "l",
                "v",
                "i",
                "s"
            ],
            "id": 2,
            "parent_id": 0
        },
    ],
    "_dummy_seq": [
        {
            "id": 0,
            "parent_bone_id": 1,
            "mat": {
                "m": [
                    1.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    1.0,
                    -2.38418579E-07,
                    0.0,
                    0.0,
                    2.38418579E-07,
                    1.0,
                    0.0,
                    0.000183062628,
                    0.0171241164,
                    -0.0006675273,
                    1.0
                ]
            }
        },
        {
            "id": 3,
            "parent_bone_id": 2,
            "mat": {
                "m": [
                    0.9997625,
                    0.0211330857,
                    0.00531362137,
                    0.0,
                    -0.020276323,
                    0.991515,
                    -0.128399879,
                    0.0,
                    -0.007982023,
                    0.128261656,
                    0.9917082,
                    0.0,
                    -0.00595185859,
                    -0.007266597,
                    1.20870256,
                    1.0
                ]
            }
        },
    ],
    "_invmat_seq": [
        {
            "m": [
                -0.0207842123,
                -0.9997837,
                -0.0008732394,
                0.0,
                0.998070538,
                -0.0207996983,
                0.05850328,
                0.0,
                -0.05850879,
                0.000344389846,
                0.998286843,
                0.0,
                0.0701802149,
                -0.000413089234,
                -1.19742656,
                1.0
            ]
        },
        {
            "m": [
                1.0,
                5.65769043E-10,
                2.190963E-12,
                0.0,
                -4.588161E-11,
                1.0,
                2.792756E-10,
                0.0,
                -1.840711E-11,
                5.036562E-10,
                1.0,
                0.0,
                1.34279241E-10,
                -6.57836274E-09,
                0.0131629827,
                1.0
            ]
        },
        {
            "m": [
                -0.00653891126,
                0.0207828227,
                0.9997626,
                0.0,
                0.05838448,
                -0.9980706,
                0.0211295113,
                0.0,
                0.9982727,
                0.05850879,
                0.00531289959,
                0.0,
                -1.19740963,
                -0.0751124844,
                -0.00637273164,
                1.0
            ]
        },],
    "_key_seq": [
        {
            "mat43_seq": null,
            "mat44_seq": null,
            "pos_seq": [
                {
                    "x": 0.0,
                    "y": 0.0,
                    "z": 1.19948149
                },
                {
                    "x": 0.05298707,
                    "y": 0.05217808,
                    "z": 1.21250737
                },
                {
                    "x": 0.05298707,
                    "y": 0.05217808,
                    "z": 1.21250737
                }],
            "quat_seq": [
                {
                    "x": -0.0207885262,
                    "y": 0.020601457,
                    "z": 0.71412015,
                    "w": 0.699411
                },
                {
                    "x": -0.015304883,
                    "y": 0.00245715142,
                    "z": 0.630636,
                    "w": 0.775923967
                },
                {
                    "x": -0.0153048858,
                    "y": 0.00245715142,
                    "z": 0.630636036,
                    "w": 0.7759239
                }]
            },
        {
            "mat43_seq": null,
            "mat44_seq": null,
            "pos_seq": [
                {
                    "x": 0.0701149851,
                    "y": -0.0004127053,
                    "z": -1.19631362
                },
                {
                    "x": 0.02800345,
                    "y": 0.0250173341,
                    "z": -1.2108103
                },
                {
                    "x": 0.0279860683,
                    "y": 0.0250018,
                    "z": -1.21005869
                },],
            "quat_seq": [
                {
                    "x": 0.0207885243,
                    "y": -0.02060146,
                    "z": -0.714120269,
                    "w": 0.699411
                },
                {
                    "x": 0.015304883,
                    "y": -0.00245715166,
                    "z": -0.630636,
                    "w": 0.775923967
                },
                {
                    "x": 0.0153048858,
                    "y": -0.002457151,
                    "z": -0.630636036,
                    "w": 0.7759239
                },]
            },
        {
            "mat43_seq": null,
            "mat44_seq": null,
            "pos_seq": [
                {
                    "x": -0.004932272,
                    "y": 2.25743972E-11,
                    "z": 5.43251026E-08
                },
                {
                    "x": -0.00493227458,
                    "y": 3.50603679E-09,
                    "z": -8.736353E-08
                },
                {
                    "x": -0.004932275,
                    "y": -2.3036153E-09,
                    "z": -8.7526E-08
                },],
            "quat_seq": [
                {
                    "x": 0.498580962,
                    "y": -0.498581618,
                    "z": 0.501414359,
                    "w": 0.5014151
                },
                {
                    "x": -0.5218716,
                    "y": 0.521872342,
                    "z": -0.477126062,
                    "w": -0.477126718
                },
                {
                    "x": -0.5218716,
                    "y": 0.521872342,
                    "z": -0.477126062,
                    "w": -0.477126718
                },]
                }]
}

Thank you in advance.

I already tried to create the animation by reading the data from the json file but it is not working, this is the code for the animation that I tried:

import bpy
import mathutils
import json
import math
from mathutils import Vector, Quaternion, Matrix

# Load JSON data
with open(r"C:UsersmartiOneDriveDocumentsGithubLGO_OBJ_ConverterBlender000.json", "r") as file:
    data = json.load(file)
base_seq = data["_base_seq"]
dummy_seq = data["_dummy_seq"]
invmat_seq = data["_invmat_seq"]
animation_data = data["_key_seq"]
header_data = data["_header"]

# Apply animation data
frame_current = bpy.context.scene.frame_current
bpy.context.scene.frame_set(0)
#for frame_number in range(header_data.get("frame_num")):
for frame_number in range(100):
    # Set the current frame
    bpy.context.scene.frame_set(frame_number)
    print("Frame number:", frame_number)
    
    for i in range(len(bones)):
        pos_data = animation_data[i]["pos_seq"][frame_number]
        rot_data = animation_data[i]["quat_seq"][frame_number]

        print( "Bone Name:", list(bones.keys())[i])
        bone = armature_obj.pose.bones[list(bones.keys())[i]]
        bone.location = (pos_data["x"], pos_data["y"], pos_data["z"])
        bone.rotation_quaternion = (rot_data["x"], rot_data["y"], rot_data["z"],rot_data["w"])

        bone.keyframe_insert("location", frame=frame_number)
        bone.keyframe_insert("rotation_quaternion", frame=frame_number)
    
    # Move to the next frame
    bpy.context.scene.frame_set(frame_number + 1)

        
# Reset the current frame to its original value
bpy.context.scene.frame_set(frame_current)

# Switch back to object mode
bpy.ops.object.mode_set(mode="OBJECT")

I was expecting to have a correct animated skeleton but I get an animated skeleton with the bones super off from where they need to be

LEAVE A COMMENT