How to modify keyframe rotation with python

I have an old model that is in a crappy t-pose. I want to adjust the t-pose so it better aligns with more modern t-poses like mixamo so I can apply some other animations to it, but when I alter the t-pose, the model’s animations get wrecked. I’ve tried various solutions i’ve found and none of them really seem to work. I’m a programmer, not an artist so I figured I’d go at it with python, and I got pretty far but I just don’t know what to do next. The idea is to loop every bone and keyframe and adjust the rotation by the inverted t-pose rotation. Then when I actually apply my t-pose as the new rest position I expect all the bones will offset and the animation will be correct again. Can anyone help with what to do next?

import bpy

# Get the armature object
armature =["MilkShape3D Skeleton"]
a = bpy.context.object.animation_data.action;


# Loop through all the bones in the armature
for bone in armature.pose.bones:
    data_path = 'pose.bones["%s"].rotation_quaternion'%(
    rotOffsetW = 1 - bone.rotation_quaternion[0]
    rotOffsetX = -1 * bone.rotation_quaternion[1]
    rotOffsetY = -1 * bone.rotation_quaternion[2]
    rotOffsetZ = -1 * bone.rotation_quaternion[3]
    print ('W: ' + str(bone.rotation_quaternion[0]) + ', ' + str(rotOffsetW))
    print ('X: ' + str(bone.rotation_quaternion[1]) + ', ' + str(rotOffsetX))
    print ('Y: ' + str(bone.rotation_quaternion[2]) + ', ' + str(rotOffsetY))
    print ('Z: ' + str(bone.rotation_quaternion[3]) + ', ' + str(rotOffsetZ))
    for fc in a.fcurves:
        if fc.data_path == data_path:
            for p in fc.keyframe_points:
                print(fc.data_path + ', ' + str(p.location))
                # TODO what do I do here to adjust the roation?

Welcome to BA :slight_smile:

It would be far easier to adjust the T pose and apply the new pose as the rest pose, the animations should automatically have the offset as animations are relative to the rest pose

So, the arms in the t-pose are like this /|. I’m trying to put them like this -|-. When I rotate them up, and apply it, the entire animation sequence has the arms rotated up so it looks like it’s walking with it’s arms sticking out. It’s lame, and I can’t believe how difficult it has been to correct so far.