-
Notifications
You must be signed in to change notification settings - Fork 23
/
operators.py
210 lines (164 loc) · 6.27 KB
/
operators.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
import bpy
from .functions import (
findMatchBones,
fromWidgetFindBone,
findMirrorObject,
symmetrizeWidget,
boneMatrix,
createWidget,
editWidget,
returnToArmature,
addRemoveWidgets,
readWidgets,
objectDataToDico,
get_collection,
)
from bpy.types import Operator
from bpy.props import FloatProperty, BoolProperty, FloatVectorProperty
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="Relative size",
default=True,
description="Widget size proportionnal to bone size"
)
global_size: FloatProperty(
name="Global Size",
default=1.0,
description="Global Size"
)
slide: FloatProperty(
name="Slide",
default=0.0,
subtype='DISTANCE',
unit='LENGTH',
description="Slide widget along y axis"
)
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(self, "relative_size")
row = layout.row(align=True)
row.prop(self, "global_size", expand=False)
row = layout.row(align=True)
row.prop(self, "slide")
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, get_collection(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.pose)
def execute(self, context):
editWidget(context.active_pose_bone)
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):
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:
if bone.custom_shape_transform and bone.custom_shape:
boneMatrix(bone.custom_shape, bone.custom_shape_transform)
elif bone.custom_shape:
boneMatrix(bone.custom_shape, bone)
else:
for ob in bpy.context.selected_objects:
if ob.type == 'MESH':
matchBone = fromWidgetFindBone(ob)
if matchBone:
if matchBone.custom_shape_transform:
boneMatrix(ob, matchBone.custom_shape_transform)
else:
boneMatrix(ob, matchBone)
return {'FINISHED'}
class BONEWIDGET_OT_matchSymmetrizeShape(bpy.types.Operator):
"""Symmetrize to the opposite side, if it is named with a .L or .R"""
bl_idname = "bonewidget.symmetrize_shape"
bl_label = "Symmetrize"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
collection = get_collection(context)
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:
if activeObject.name.endswith("L"):
if bone.name.endswith("L") and widgetsAndBones[bone]:
symmetrizeWidget(bone, collection)
elif activeObject.name.endswith("R"):
if bone.name.endswith("R") and widgetsAndBones[bone]:
symmetrizeWidget(bone, collection)
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"
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[1]['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
addRemoveWidgets(context, "remove", bpy.types.Scene.widget_list[1]['items'], objects)
return {'FINISHED'}
classes = (
BONEWIDGET_OT_removeWidgets,
BONEWIDGET_OT_addWidgets,
BONEWIDGET_OT_matchSymmetrizeShape,
BONEWIDGET_OT_matchBoneTransforms,
BONEWIDGET_OT_returnToArmature,
BONEWIDGET_OT_editWidget,
BONEWIDGET_OT_createWidget,
)
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)