import bpy

def transfer_body_data():
    source_name = "Body"
    target_names = ["BodyTop", "bodyBottom", "BodyFeet"]
    
    source_obj = bpy.data.objects.get(source_name)
    if not source_obj:
        print(f"Source object '{source_name}' not found!")
        return

    for name in target_names:
        target_obj = bpy.data.objects.get(name)
        if not target_obj:
            print(f"Target '{name}' not found, skipping...")
            continue

        # 1. FIX NORMALS & WEIGHTS (Data Transfer)
        # Required for Blender 4.1+: Enable Auto Smooth (Smooth by Angle)
        target_obj.data.use_auto_smooth = True 
        
        dt_mod = target_obj.modifiers.new(name="TR_DATA", type='DATA_TRANSFER')
        dt_mod.object = source_obj
        
        # Transfer Vertex Groups (Weights)
        dt_mod.use_vert_data = True
        dt_mod.data_types_verts = {'VGROUP_WEIGHTS'}
        dt_mod.vert_mapping = 'NEAREST'
        
        # Transfer Normals (Fixes Seams)
        dt_mod.use_loop_data = True
        dt_mod.data_types_loops = {'CUSTOM_NORMAL'}
        dt_mod.loop_mapping = 'NEAREST_POLYNOR'
        
        # Apply to bake the weights and normals
        bpy.context.view_layer.objects.active = target_obj
        bpy.ops.object.modifier_apply(modifier=dt_mod.name)

        # 2. TRANSFER SHAPE KEYS
        if source_obj.data.shape_keys:
            # Deselect all, then select source then target
            bpy.ops.object.select_all(action='DESELECT')
            source_obj.select_set(True)
            target_obj.select_set(True)
            bpy.context.view_layer.objects.active = target_obj
            
            # Joins shape keys from source to target
            bpy.ops.object.shape_key_transfer()

    print("Transfer complete: Normals, Weights, and Shape Keys synced.")

transfer_body_data()

