-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathBoneAnimCopy_prototype.py
317 lines (233 loc) · 9.59 KB
/
BoneAnimCopy_prototype.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
bl_info = {
"name": "Bone Animation Copy Tool",
"description": "Copy animation between different armature by bone constrain",
"author": "Kumopult <[email protected]>",
"version": (1, 1),
"blender": (2, 83, 0),
"location": "View 3D > Toolshelf",
"doc_url": "https://github.com/kumopult/blender_BoneAnimCopy",
"tracker_url": "https://github.com/kumopult/blender_BoneAnimCopy/issues",
"category": "Armature",
}
import bpy
# UI
class BAC_PT_Panel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "BoneAnimCopy"
bl_label = "Bone Animation Copy Tool"
def draw(self, context):
layout = self.layout
if context.object != None and context.object.type == 'ARMATURE':
s = get_state()
split = layout.row().split(factor=0.244)
split.column().label(text='Target:')
split.column().label(text=context.object.name, icon='ARMATURE_DATA')
layout.prop(s, 'selected_source', text='Source', icon='ARMATURE_DATA')
layout.separator()
if s.source == None:
layout.label(text='Choose a source armature to continue', icon='INFO')
else:
layout.label(text='Bone Mappings')
if s.editing_mappings:
layout.label(text='Edit Bone Mappings:', icon='TOOL_SETTINGS')
row = layout.row()
row.template_list('BAC_UL_mappings', '', s, 'mappings', s, 'active_mapping')
col = row.column(align=True)
col.operator('kumopult_bac.list_action', icon='ADD', text='').action = 'ADD'
col.operator('kumopult_bac.list_action', icon='REMOVE', text='').action = 'REMOVE'
col.operator('kumopult_bac.list_action', icon='TRIA_UP', text='').action = 'UP'
col.operator('kumopult_bac.list_action', icon='TRIA_DOWN', text='').action = 'DOWN'
col.operator('kumopult_bac.child_mapping', icon='CON_SPLINEIK', text='')
layout.operator('kumopult_bac.constraint_apply', text='Done')
else:
layout.label(text='Bone Constraints:', icon='TOOL_SETTINGS')
layout.template_list('BAC_UL_constraints', '', s, 'mappings', s, 'active_mapping')
layout.operator('kumopult_bac.constraint_edit', text='Edit')
else:
layout.label(text='No armature selected', icon='ERROR')
# data
class BAC_BoneMapping(bpy.types.PropertyGroup):
source: bpy.props.StringProperty()
target: bpy.props.StringProperty()
roll: bpy.props.FloatProperty()
def is_valid(self):
return (self.source != None
and self.target != None
and len(self.source) > 0
and len(self.target) > 0)
def apply(self):
# apply mapping into constraint
s = get_state()
cr = self.get_cr()
rr = self.get_rr()
cr.target = s.source
cr.subtarget = self.source
rr.to_min_y_rot = self.roll
def save(self):
# save constraint roll into mapping
cr = self.get_cr()
rr = self.get_rr()
self.roll = rr.to_min_y_rot
def get_cr(self):
s = get_state()
tc = s.target.pose.bones[self.target].constraints
def new_cr():
cr = tc.new(type='COPY_ROTATION')
cr.name = 'BAC_ROT_COPY'
return cr
cr = tc.get('BAC_ROT_COPY') or new_cr()
return cr
def get_rr(self):
s = get_state()
tc = s.target.pose.bones[self.target].constraints
def new_rr():
rr = tc.new(type='TRANSFORM')
rr.name = 'BAC_ROT_ROLL'
rr.map_to = 'ROTATION'
rr.owner_space = 'LOCAL'
rr.to_euler_order = 'YXZ'
rr.target = bpy.data.objects['BAC_AXES']
return rr
rr = tc.get('BAC_ROT_ROLL') or new_rr()
return rr
def clear(self):
s = get_state()
c = s.target.pose.bones[self.target].constraints
c.remove(self.get_cr)
c.remove(self.get_rr)
class BAC_State(bpy.types.PropertyGroup):
selected_source: bpy.props.PointerProperty(
type=bpy.types.Object,
poll=lambda self, obj: obj.type == 'ARMATURE' and obj != bpy.context.object,
update=lambda self, ctx: get_state().update_source()
)
source: bpy.props.PointerProperty(type=bpy.types.Object)
target: bpy.props.PointerProperty(type=bpy.types.Object)
mappings: bpy.props.CollectionProperty(type=BAC_BoneMapping)
active_mapping: bpy.props.IntProperty()
editing_mappings: bpy.props.BoolProperty(default=False)
def update_source(self):
self.target = bpy.context.object
if self.selected_source == None:
return
self.source = self.selected_source
def get_source_armature(self):
return self.source.data
def get_target_armature(self):
return self.target.data
def add_mapping(self, target, source):
m = self.mappings.add()
m.target = target
m.source = source
self.active_mapping = len(self.mappings) - 1
return m
def remove_mapping(self, index):
self.mappings[index].clear()
self.mappings.remove(index)
# mapping
class BAC_UL_mappings(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index, flt_flag):
s = get_state()
layout.alert = not item.is_valid()
layout.prop_search(item, 'target', s.get_target_armature(), 'bones', text='', icon='BONE_DATA')
layout.label(icon='BACK')
layout.prop_search(item, 'source', s.get_source_armature(), 'bones', text='', icon='BONE_DATA')
def draw_filter(self, context, layout):
pass
def filter_items(self, context, data, propname):
flt_flags = []
flt_neworder = []
return flt_flags, flt_neworder
class BAC_UL_constraints(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index, flt_flag):
s = get_state()
layout.alert = not item.is_valid()
layout.label(text=item.target, icon='BONE_DATA')
layout.label(icon='FORWARD')
layout.prop(item.get_rr(), 'to_min_y_rot', text='')
def draw_filter(self, context, layout):
pass
def filter_items(self, context, data, propname):
flt_flags = []
flt_neworder = []
return flt_flags, flt_neworder
class BAC_OT_ListAction(bpy.types.Operator):
bl_idname = 'kumopult_bac.list_action'
bl_label = 'Apply'
action: bpy.props.StringProperty()
def execute(self, context):
s = get_state()
if self.action == 'ADD':
s.add_mapping('', '')
elif self.action == 'REMOVE':
if len(s.mappings) > 0:
s.remove_mapping(s.active_mapping)
s.active_mapping = min(s.active_mapping, len(s.mappings) - 1)
elif self.action == 'UP':
if s.active_mapping > 0:
s.mappings.move(s.active_mapping, s.active_mapping - 1)
elif self.action == 'DOWN':
if len(s.mappings) > s.active_mapping + 1:
s.mappings.move(s.active_mapping, s.active_mapping + 1)
return {'FINISHED'}
class BAC_OT_ChildMapping(bpy.types.Operator):
bl_idname = 'kumopult_bac.child_mapping'
bl_label = 'ChildMap'
def execute(self, context):
s = get_state()
m = s.mappings[s.active_mapping]
source_children = s.get_source_armature().bones[m.source].children
target_children = s.get_target_armature().bones[m.target].children
for i in range(0, min(len(source_children), len(target_children))):
s.add_mapping(target_children[i].name, source_children[i].name)
return {'FINISHED'}
class BAC_OT_Apply(bpy.types.Operator):
bl_idname = 'kumopult_bac.constraint_apply'
bl_label = 'Apply'
def execute(self, context):
# exit the edit mode, build or adjust the constraint by mappings
s = get_state()
s.editing_mappings = False
for mapping in s.mappings:
if mapping.is_valid():
mapping.apply()
return {'FINISHED'}
class BAC_OT_Edit(bpy.types.Operator):
bl_idname = 'kumopult_bac.constraint_edit'
bl_label = 'Edit'
def execute(self, context):
# enter the edit mode, save the roll value
s = get_state()
s.editing_mappings = True
for mapping in s.mappings:
if mapping.is_valid():
mapping.save()
return {'FINISHED'}
# utils
def get_state():
return bpy.context.object.kumopult_bac
classes = (
BAC_PT_Panel,
BAC_BoneMapping,
BAC_State,
BAC_UL_mappings,
BAC_UL_constraints,
BAC_OT_ListAction,
BAC_OT_ChildMapping,
BAC_OT_Apply,
BAC_OT_Edit
)
# register, unregister = bpy.utils.register_classes_factory(classes)
# Register
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Object.kumopult_bac = bpy.props.PointerProperty(type=BAC_State)
# Unregister
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Object.kumopult_bac
if __name__ == "__main__":
register()