forked from ChristopheSeux/boneWidget
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperators.py
More file actions
364 lines (283 loc) · 11.9 KB
/
operators.py
File metadata and controls
364 lines (283 loc) · 11.9 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import bpy
from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty
from .functions import createWidget, boneMatrix, copyWidget, findMatchBones, fromWidgetFindBone, \
symmetrizeWidget_helper, editWidget, returnToArmature, addRemoveWidgets, readWidgets, getCollection, \
getViewLayerCollection, deleteUnusedWidgets, clearBoneWidgets, resyncWidgetNames, addObjectAsWidget
class BONEWIDGET_OT_createWidget(bpy.types.Operator):
"""Creates a widget for selected bone"""
bl_idname = "bonewidget.create_widget"
bl_label = "Create"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object and context.object.mode == 'POSE'
relative_size: BoolProperty(
name="Scale to Bone length",
default=True,
description="Scale Widget to bone length"
)
global_size: FloatProperty(
name="Global Size",
default=1.0,
description="Global Size"
)
slide: FloatProperty(
name="Slide",
default=0.0,
subtype='NONE',
unit='NONE',
description="Slide widget along y axis"
)
rotation: FloatVectorProperty(
name="Rotation",
description="Rotate the widget",
default=(0.0, 0.0, 0.0),
subtype='EULER',
unit='ROTATION',
precision=1,
)
def draw(self, context):
layout = self.layout
layout.use_property_split = True
col = layout.column()
row = col.row(align=True)
row.prop(self, "relative_size")
row = col.row(align=True)
row.prop(self, "global_size", expand=False)
row = col.row(align=True)
row.prop(self, "slide")
row = col.row(align=True)
row.prop(self, "rotation", text="Rotation")
def execute(self, context):
wgts = readWidgets()
for bone in bpy.context.selected_pose_bones:
createWidget(bone, wgts[context.scene.widget_list], self.relative_size, self.global_size, [
1, 1, 1], self.slide, self.rotation, getCollection(context))
return {'FINISHED'}
class BONEWIDGET_OT_editWidget(bpy.types.Operator):
"""Edit the widget for selected bone"""
bl_idname = "bonewidget.edit_widget"
bl_label = "Edit"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE'
and context.active_pose_bone and context.active_pose_bone.custom_shape)
def execute(self, context):
active_bone = context.active_pose_bone
try:
editWidget(active_bone)
except KeyError:
self.report({'INFO'}, 'This widget is not in the Widget Collection')
return {'FINISHED'}
class BONEWIDGET_OT_copyWidget(bpy.types.Operator):
"""Copy the widget of active bone to all selected bones"""
bl_idname = "bonewidget.copy_widget"
bl_label = "Copy"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE'
and context.active_pose_bone and context.active_pose_bone.custom_shape
and len(context.selected_pose_bones) > 1)
def execute(self, context):
active_bone = context.active_pose_bone
copyWidget(active_bone, context.selected_pose_bones)
return {'FINISHED'}
class BONEWIDGET_OT_returnToArmature(bpy.types.Operator):
"""Switch back to the armature"""
bl_idname = "bonewidget.return_to_armature"
bl_label = "Return to armature"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'MESH'
and context.object.mode in ['EDIT', 'OBJECT'])
def execute(self, context):
b = bpy.context.object
if fromWidgetFindBone(bpy.context.object):
returnToArmature(bpy.context.object)
else:
self.report({'INFO'}, 'Object is not a bone widget')
return {'FINISHED'}
class BONEWIDGET_OT_matchBoneTransforms(bpy.types.Operator):
"""Match the widget to the bone transforms"""
bl_idname = "bonewidget.match_bone_transforms"
bl_label = "Match bone transforms"
def execute(self, context):
if bpy.context.mode == "POSE":
for bone in bpy.context.selected_pose_bones:
boneMatrix(bone.custom_shape, bone)
else:
for ob in bpy.context.selected_objects:
if ob.type == 'MESH':
matchBone = fromWidgetFindBone(ob)
if matchBone:
boneMatrix(ob, matchBone)
return {'FINISHED'}
class BONEWIDGET_OT_matchSymmetrizeShape(bpy.types.Operator):
"""Symmetrize to the opposite side ONLY if it is named with a .L or .R (default settings)"""
bl_idname = "bonewidget.symmetrize_shape"
bl_label = "Symmetrize"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
try:
# collection = getCollection(context)
widget = bpy.context.active_pose_bone.custom_shape
collection = getViewLayerCollection(context, widget)
widgetsAndBones = findMatchBones()[0]
activeObject = findMatchBones()[1]
widgetsAndBones = findMatchBones()[0]
if not activeObject:
self.report({"INFO"}, "No active bone or object")
return {'FINISHED'}
for bone in widgetsAndBones:
symmetrizeWidget_helper(bone, collection, activeObject, widgetsAndBones)
except Exception as e:
self.report({'INFO'}, "There is nothing to mirror to")
# pass
return {'FINISHED'}
class BONEWIDGET_OT_addWidgets(bpy.types.Operator):
"""Add selected mesh object to Bone Widget Library"""
bl_idname = "bonewidget.add_widgets"
bl_label = "Add Widgets"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'MESH' and context.object.mode == 'OBJECT'
and context.active_object is not None)
def execute(self, context):
objects = []
if bpy.context.mode == "POSE":
for bone in bpy.context.selected_pose_bones:
objects.append(bone.custom_shape)
else:
for ob in bpy.context.selected_objects:
if ob.type == 'MESH':
objects.append(ob)
if not objects:
self.report({'INFO'}, 'Select Meshes or Pose_bones')
addRemoveWidgets(context, "add", bpy.types.Scene.widget_list.keywords['items'], objects)
return {'FINISHED'}
class BONEWIDGET_OT_removeWidgets(bpy.types.Operator):
"""Remove selected widget object from the Bone Widget Library"""
bl_idname = "bonewidget.remove_widgets"
bl_label = "Remove Widgets"
def execute(self, context):
objects = bpy.context.scene.widget_list
unwantedList = addRemoveWidgets(context, "remove", bpy.types.Scene.widget_list.keywords['items'], objects)
return {'FINISHED'}
class BONEWIDGET_OT_toggleCollectionVisibility(bpy.types.Operator):
"""Show/hide the bone widget collection"""
bl_idname = "bonewidget.toggle_collection_visibilty"
bl_label = "Collection Visibilty"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE')
def execute(self, context):
collection = getViewLayerCollection(context)
collection.hide_viewport = not collection.hide_viewport
collection.exclude = False
return {'FINISHED'}
class BONEWIDGET_OT_deleteUnusedWidgets(bpy.types.Operator):
"""Delete unused objects in the WGT collection"""
bl_idname = "bonewidget.delete_unused_widgets"
bl_label = "Delete Unused Widgets"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE')
def execute(self, context):
deleteUnusedWidgets()
return {'FINISHED'}
class BONEWIDGET_OT_clearBoneWidgets(bpy.types.Operator):
"""Clear widgets from selected pose bones"""
bl_idname = "bonewidget.clear_widgets"
bl_label = "Clear Widgets"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE')
def execute(self, context):
clearBoneWidgets()
return {'FINISHED'}
class BONEWIDGET_OT_resyncWidgetNames(bpy.types.Operator):
"""Clear widgets from selected pose bones"""
bl_idname = "bonewidget.resync_widget_names"
bl_label = "Resync Widget Names"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE')
def execute(self, context):
resyncWidgetNames()
return {'FINISHED'}
'''
class BONEWIDGET_OT_selectObject(bpy.types.Operator):
"""Select object as widget for selected bone"""
bl_idname = "bonewidget.select_object"
bl_label = "Select Object as Widget"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'ARMATURE' and context.object.mode == 'POSE')
def active_armature(self, context):
ob = context.object
ob = str(ob).split('"')
ob = ob[1]
return ob
def active_bone(self, context):
ob = context.active_bone
ob = str(ob).split('"')
ob = ob[1]
return ob
def execute(self, context):
active_armature = self.active_armature(context)
active_bone = self.active_bone(context)
writeTemp(active_armature, active_bone)
logOperation("info", 'Write armature name: "{}" and bone name: "{}" to file temp.txt'.format(active_armature, active_bone))
selectObject()
return {'FINISHED'}
class BONEWIDGET_OT_confirmWidget(bpy.types.Operator):
"""Set selected object as widget for selected bone"""
bl_idname = "bonewidget.confirm_widget"
bl_label = "Confirm selected Object as widget shape"
@classmethod
def poll(cls, context):
return (context.object and context.object.type == 'MESH' and context.object.mode == 'OBJECT')
def execute(self, context):
arm_bone = readTemp().split(",")
active_armature = arm_bone[0]
active_bone = arm_bone[1]
active_bone = bpy.data.objects[active_armature].pose.bones[active_bone]
active_armature = bpy.data.objects[active_armature]
print(active_armature, active_bone)
cW = confirmWidget(context, active_bone, active_armature)
logOperation("info", 'Duplicate Object "{}" and set duplicate as custom shape for Bone "{}" in Armature "{}".'.format(cW, active_bone, active_armature))
return {'FINISHED'}
'''
class BONEWIDGET_OT_addObjectAsWidget(bpy.types.Operator):
"""Add selected object as widget for active bone."""
bl_idname = "bonewidget.add_as_widget"
bl_label = "Confirm selected Object as widget shape"
@classmethod
def poll(cls, context):
return (len(context.selected_objects) == 2 and context.object.mode == 'POSE')
def execute(self, context):
addObjectAsWidget(context, getCollection(context))
return {'FINISHED'}
classes = (
BONEWIDGET_OT_removeWidgets,
BONEWIDGET_OT_addWidgets,
BONEWIDGET_OT_matchSymmetrizeShape,
BONEWIDGET_OT_matchBoneTransforms,
BONEWIDGET_OT_returnToArmature,
BONEWIDGET_OT_editWidget,
BONEWIDGET_OT_copyWidget,
BONEWIDGET_OT_createWidget,
BONEWIDGET_OT_toggleCollectionVisibility,
BONEWIDGET_OT_deleteUnusedWidgets,
BONEWIDGET_OT_clearBoneWidgets,
BONEWIDGET_OT_resyncWidgetNames,
BONEWIDGET_OT_addObjectAsWidget,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)