-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
95 lines (78 loc) · 3.5 KB
/
__init__.py
File metadata and controls
95 lines (78 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import bpy
def get_selected_armatures(context):
return [o for o in context.selected_objects if o.type == 'ARMATURE']
# ---------- Operator: Batch add Copy Transforms ----------
class POSE_OT_sync_common_bones(bpy.types.Operator):
bl_idname = "pose.sync_common_bones"
bl_label = "Sync Common Bones"
bl_description = ("Add Copy Transforms constraints to bones with matching "
"names between the last two selected Armatures")
clear_existing: bpy.props.BoolProperty(
name="Clear old constraints",
default=True,
description="Remove previous SyncFrom_ constraints before adding new ones")
def execute(self, context):
sel = get_selected_armatures(context)
if len(sel) < 2:
self.report({'ERROR'}, "Select at least TWO Armatures (source then target)")
return {'CANCELLED'}
# Use order of selection: last is active (target), previous is source
dst = sel[-1]
src = sel[-2]
synced = 0
for pb in dst.pose.bones:
if self.clear_existing:
for c in [c for c in pb.constraints
if c.type == 'COPY_TRANSFORMS' and c.name.startswith("SyncFrom_")]:
pb.constraints.remove(c)
if pb.name in src.pose.bones:
con = pb.constraints.new(type='COPY_TRANSFORMS')
con.name = f"SyncFrom_{src.name}"
con.target = src
con.subtarget = pb.name
con.owner_space = 'POSE'
con.target_space = 'POSE'
synced += 1
self.report({'INFO'}, f"Synced {synced} bones from {src.name} ➜ {dst.name}")
return {'FINISHED'}
# ---------- Operator: Bake & Clear ----------
class POSE_OT_bake_synced(bpy.types.Operator):
bl_idname = "pose.bake_synced_bones"
bl_label = "Bake Synced Bones"
bl_description = "Bake the Copy Transforms results into keyframes on the active Armature"
frame_start: bpy.props.IntProperty(default=1, description="Start frame")
frame_end: bpy.props.IntProperty(default=250, description="End frame")
def execute(self, context):
active = context.object
if not active or active.type != 'ARMATURE':
self.report({'ERROR'}, "Active object must be an Armature to bake")
return {'CANCELLED'}
bpy.ops.nla.bake(frame_start=self.frame_start,
frame_end=self.frame_end,
only_selected=False,
visual_keying=True,
clear_constraints=True,
bake_types={'POSE'})
self.report({'INFO'}, f"Baked frames {self.frame_start}-{self.frame_end} on {active.name}")
return {'FINISHED'}
# ---------- UI Panel ----------
class VIEW3D_PT_sync_common_bones(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Bone Sync'
bl_label = 'Sync & Bake'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.label(text="1. Select SRC then DST Armatures")
col.operator("pose.sync_common_bones", icon='CONSTRAINT_BONE')
col.separator()
col.label(text="2. Bake (optional)")
col.operator("pose.bake_synced_bones", icon='ACTION')
# ---------- Registration ----------
classes = (
POSE_OT_sync_common_bones,
POSE_OT_bake_synced,
VIEW3D_PT_sync_common_bones,
)
register, unregister = bpy.utils.register_classes_factory(classes)