#Script that adjust bone positions of the last selected (active armature) to match the other selected armature.   # Make sure that the scale of the skeleton is applied for it to work.
#EDIT: For clarification purposes, this code moves the bone positions only. It doesn't rotate them. 
import bpy

SK = bpy.context.active_object
# ref = SK_CH_mar_bdf_higeuncle
for obj in bpy.context.selected_objects:
    if obj != SK:
        refSK = obj
        
obj = SK #context.object #self.Skeleton

bpy.ops.object.mode_set(mode='EDIT')

for bone in SK.data.edit_bones:
    
    bone.use_connect = False

#_________________Function taking SK and refSk_________________
#TODO: fix face and left hand---->Done
#TODO: Make it work for non-PSK and non-Tekken skeletons---->Done

bpy.ops.object.mode_set(mode='POSE')

for bone in obj.pose.bones:

    for refbone in refSK.pose.bones:
        
        if refbone.name == bone.name:
            
            Target = refbone.head
            
            Source =   bone.head
            
            Disp =  -Source + Target 
                
            AdjDisp = bone.matrix.to_3x3().inverted() @ Disp # Uninverted matrix actually gives the exact 3D cursor coordinates too

            bone.location = AdjDisp#.to_tuple()
            bpy.ops.object.posemode_toggle()
            bpy.ops.object.posemode_toggle()