import bpy

def mirror_name(name: str) -> str:
    """Flip .L <-> .R in bone/vertex group names."""
    if name.endswith(".L"):
        return name[:-2] + ".R"
    elif name.endswith(".R"):
        return name[:-2] + ".L"
    return name

# Make sure we're in pose mode and have an armature selected
obj = bpy.context.object
if obj and obj.type == 'ARMATURE' and bpy.context.mode == 'POSE':
    for bone in bpy.context.selected_pose_bones:
        for constraint in bone.constraints:
            if constraint.subtarget:  # Only process if subtarget is set
                new_name = mirror_name(constraint.subtarget)
                if new_name != constraint.subtarget:
                    print(f"{bone.name}: {constraint.name} subtarget {constraint.subtarget} -> {new_name}")
                    constraint.subtarget = new_name
else:
    print("Please select an armature and enter Pose Mode with some bones selected.")
