﻿import bpy
import json

def apply_transformations_from_json(json_path, armature_name):
    # Load the JSON data
    with open(json_path, 'r', encoding='utf-8-sig') as file:
        data = json.load(file)

    # Check if 'Bones' key is present
    if 'Bones' not in data:
        print("Key 'Bones' not found in JSON data.")
        return

    bones_data = data['Bones']

    # Ensure we are in pose mode
    if bpy.context.object.mode != 'POSE':
        bpy.ops.object.mode_set(mode='POSE')

    armature = bpy.data.objects.get(armature_name)
    if not armature:
        print(f"Armature named '{armature_name}' not found.")
        return

    # Apply transformations to each bone
    for bone_name, transforms in bones_data.items():
        bone = armature.pose.bones.get(bone_name)
        if not bone:
            print(f"Bone named '{bone_name}' not found in armature.")
            continue

        # Apply translation
        bone.location = (
            transforms['Translation']['X'],
            transforms['Translation']['Y'],
            transforms['Translation']['Z']
        )

        # Apply rotation (in radians)
        bone.rotation_euler = (
            transforms['Rotation']['X'],
            transforms['Rotation']['Y'],
            transforms['Rotation']['Z']
        )

        # Apply scaling
        bone.scale = (
            transforms['Scaling']['X'],
            transforms['Scaling']['Y'],
            transforms['Scaling']['Z']
        )

    # Update the armature
    bpy.context.view_layer.update()

# Path to the JSON file
json_path = 'PUT YOUR .JSON HERE'
armature_name = 'n_root'

apply_transformations_from_json(json_path, armature_name)
