forked from Mystfit/IK-FK-Snapping-for-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
364 lines (334 loc) · 17.6 KB
/
ui.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
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 . import operators
from . import utilities
#━━━━━━━━━━━━━━━━━━━━
# Tool Panel
#━━━━━━━━━━━━━━━━━━━━
class ToolPanel(bpy.types.Panel):
bl_idname = 'TOOL_PT_panel'
bl_label = 'Snapping Tool'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
# Draw the preset menu button in the panel header
def draw_header_preset(self, _context):
operators.PRESET_PT_Panel.draw_panel_header(self.layout)
def draw(self, context):
layout = self.layout
col = layout.column()
scene = bpy.context.scene
obj = bpy.context.active_object
# Check if an armature is active
if obj and obj.type == 'ARMATURE':
# Bones needed for IK to FK snapping on left arm
IKtoFK_ArmL_Bones = {
obj.pose.bones.get(scene.IK_Arm_Pole_Target_L),
obj.pose.bones.get(scene.IK_Arm_Target_L),
obj.pose.bones.get(scene.FK_Arm_End_L),
obj.pose.bones.get(scene.IK_Arm_Pole_L),
obj.pose.bones.get(scene.IK_Arm_Control_L),
obj.pose.bones.get(scene.IK_Arm_End_L)
}
# Bones needed for FK to IK snapping on left arm
FKtoIK_ArmL_Bones = {
obj.pose.bones.get(scene.IK_Arm_Upper_L),
obj.pose.bones.get(scene.IK_Arm_Lower_L),
obj.pose.bones.get(scene.IK_Arm_End_L),
obj.pose.bones.get(scene.FK_Arm_Upper_L),
obj.pose.bones.get(scene.FK_Arm_Lower_L),
obj.pose.bones.get(scene.FK_Arm_End_L)
}
# Bones needed for IK to FK snapping on right arm
IKtoFK_ArmR_Bones = {
obj.pose.bones.get(scene.IK_Arm_Pole_Target_R),
obj.pose.bones.get(scene.IK_Arm_Target_R),
obj.pose.bones.get(scene.FK_Arm_End_R),
obj.pose.bones.get(scene.IK_Arm_Pole_R),
obj.pose.bones.get(scene.IK_Arm_Control_R),
obj.pose.bones.get(scene.IK_Arm_End_R)
}
# Bones needed for FK to IK snapping on right arm
FKtoIK_ArmR_Bones = {
obj.pose.bones.get(scene.IK_Arm_Upper_R),
obj.pose.bones.get(scene.IK_Arm_Lower_R),
obj.pose.bones.get(scene.IK_Arm_End_R),
obj.pose.bones.get(scene.FK_Arm_Upper_R),
obj.pose.bones.get(scene.FK_Arm_Lower_R),
obj.pose.bones.get(scene.FK_Arm_End_R)
}
# Bones needed for IK to FK snapping on left leg
IKtoFK_LegL_Bones = {
obj.pose.bones.get(scene.IK_Leg_Pole_Target_L),
obj.pose.bones.get(scene.IK_Leg_Target_L),
obj.pose.bones.get(scene.FK_Leg_End_L),
obj.pose.bones.get(scene.IK_Leg_Pole_L),
obj.pose.bones.get(scene.IK_Leg_Control_L),
obj.pose.bones.get(scene.IK_Leg_End_L)
}
# Bones needed for FK to IK snapping on left leg
FKtoIK_LegL_Bones = {
obj.pose.bones.get(scene.IK_Leg_Upper_L),
obj.pose.bones.get(scene.IK_Leg_Lower_L),
obj.pose.bones.get(scene.IK_Leg_End_L),
obj.pose.bones.get(scene.FK_Leg_Upper_L),
obj.pose.bones.get(scene.FK_Leg_Lower_L),
obj.pose.bones.get(scene.FK_Leg_End_L)
}
# Bones needed for IK to FK snapping on right leg
IKtoFK_LegR_Bones = {
obj.pose.bones.get(scene.IK_Leg_Pole_Target_R),
obj.pose.bones.get(scene.IK_Leg_Target_R),
obj.pose.bones.get(scene.FK_Leg_End_R),
obj.pose.bones.get(scene.IK_Leg_Pole_R),
obj.pose.bones.get(scene.IK_Leg_Control_R),
obj.pose.bones.get(scene.IK_Leg_End_R)
}
# Bones needed for FK to IK snapping on right leg
FKtoIK_LegR_Bones = {
obj.pose.bones.get(scene.IK_Leg_Upper_R),
obj.pose.bones.get(scene.IK_Leg_Lower_R),
obj.pose.bones.get(scene.IK_Leg_End_R),
obj.pose.bones.get(scene.FK_Leg_Upper_R),
obj.pose.bones.get(scene.FK_Leg_Lower_R),
obj.pose.bones.get(scene.FK_Leg_End_R)
}
# Check if any relevant bones are found
if utilities.any_snapping_possible(IKtoFK_ArmL_Bones, FKtoIK_ArmL_Bones,
IKtoFK_ArmR_Bones, FKtoIK_ArmR_Bones,
IKtoFK_LegL_Bones, FKtoIK_LegL_Bones,
IKtoFK_LegR_Bones, FKtoIK_LegR_Bones):
# --- Arm L Snapping Tool ---
box = col.box()
box.label(text= "Arm L Snapping", icon= 'SNAP_ON')
# Check if all required bones are present for both snapping directions
if not all(IKtoFK_ArmL_Bones) and not all(FKtoIK_ArmL_Bones):
box.label(text= "Bone Incomplete", icon= 'ERROR')
else:
# Create a grid layout for buttons
grid = box.grid_flow(columns=2, align=True)
if all(IKtoFK_ArmL_Bones):
# Add the "Snap to FK" button and link it to the corresponding operator
arml_ikfk_oprator = grid.operator('opr.arml_ikfk', text='Snap to FK')
else:
box.label(text= "IK to FK Incomplete", icon= 'ERROR')
if all(FKtoIK_ArmL_Bones):
# Add the "Snap to IK" button and link it to the corresponding operator
arml_fkik_oprator = grid.operator('opr.arml_fkik', text='Snap to IK')
else:
box.label(text= "FK to IK Incomplete", icon= 'ERROR')
# --- Arm R Snapping Tool ---
box = col.box()
box.label(text= "Arm R Snapping", icon= 'SNAP_ON')
# Check if all required bones are present for both snapping directions
if not all(IKtoFK_ArmR_Bones) and not all(FKtoIK_ArmR_Bones):
box.label(text= "Bone Incomplete", icon= 'ERROR')
else:
# Create a grid layout for buttons
grid = box.grid_flow(columns=2, align=True)
if all(IKtoFK_ArmR_Bones):
# Add the "Snap to FK" button and link it to the corresponding operator
armr_ikfk_oprator = grid.operator('opr.armr_ikfk', text='Snap to FK')
else:
box.label(text= "IK to FK Incomplete", icon= 'ERROR')
if all(FKtoIK_ArmR_Bones):
# Add the "Snap to IK" button and link it to the corresponding operator
armr_fkik_oprator = grid.operator('opr.armr_fkik', text='Snap to IK')
else:
box.label(text= "FK to IK Incomplete", icon= 'ERROR')
# --- Leg L Snapping Tool ---
box = col.box()
box.label(text= "Leg L Snapping", icon= 'SNAP_ON')
# Check if all required bones are present for both snapping directions
if not all(IKtoFK_LegL_Bones) and not all(FKtoIK_LegL_Bones):
box.label(text= "Bone Incomplete", icon= 'ERROR')
else:
# Create a grid layout for buttons
grid = box.grid_flow(columns=2, align=True)
if all(IKtoFK_LegL_Bones):
# Add the "Snap to FK" button and link it to the corresponding operator
legl_ikfk_oprator = grid.operator('opr.legl_ikfk', text='Snap to FK')
else:
box.label(text= "IK to FK Incomplete", icon= 'ERROR')
if all(FKtoIK_LegL_Bones):
# Add the "Snap to IK" button and link it to the corresponding operator
legl_fkik_oprator = grid.operator('opr.legl_fkik', text='Snap to IK')
else:
box.label(text= "FK to IK Incomplete", icon= 'ERROR')
# --- Leg R Snapping Tool ---
box = col.box()
box.label(text= "Leg R Snapping", icon= 'SNAP_ON')
# Check if all required bones are present for both snapping directions
if not all(IKtoFK_LegR_Bones) and not all(FKtoIK_LegR_Bones):
box.label(text= "Bone Incomplete", icon= 'ERROR')
else:
# Create a grid layout for buttons
grid = box.grid_flow(columns=2, align=True)
if all(IKtoFK_LegR_Bones):
# Add the "Snap to FK" button and link it to the corresponding operator
armr_ikfk_oprator = grid.operator('opr.legr_ikfk', text='Snap to FK')
else:
box.label(text= "IK to FK Incomplete", icon= 'ERROR')
if all(FKtoIK_LegR_Bones):
# Add the "Snap to IK" button and link it to the corresponding operator
armr_fkik_oprator = grid.operator('opr.legr_fkik', text='Snap to IK')
else:
box.label(text= "FK to IK Incomplete", icon= 'ERROR')
else:
# If none of the bone sets have any bones (meaning the armature doesn't have the necessary bones for snapping)
col.label(text="Active Armature has no similar bones", icon='ERROR')
else:
# If the active object is not an armature
col.label(text="No active armature", icon='ERROR')
#━━━━━━━━━━━━━━━━━━━━━━━━
# Armature Panel
#━━━━━━━━━━━━━━━━━━━━━━━━
class Armature_MappingPanel(bpy.types.Panel):
bl_idname = 'ARMATURE_MAPPING_PT_panel'
bl_label = 'Armature Mapping'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
# Draw the preset menu button in the panel header
def draw_header_preset(self, _context):
operators.PRESET_PT_Panel.draw_panel_header(self.layout)
def draw(self, context):
col = self.layout.column()
# Add the "Open Preset Directory" button and link it to the corresponding operator
col.operator("opr.open_preset_directory", icon="FILEBROWSER")
# ------------------------------
# Panel for Arm L Bones
# ------------------------------
class ArmL_MappingPanel(bpy.types.Panel):
bl_idname = 'ARML_MAPPING_PT_panel'
bl_label = 'Arm L'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
bl_parent_id = "ARMATURE_MAPPING_PT_panel"
def draw(self, context):
obj = bpy.context.active_object
col = self.layout.column()
# Check if an armature is active
if obj and obj.type == 'ARMATURE':
# Create bone selection fields for each bone involved in Arm L snapping
col.prop_search(context.scene, "FK_Arm_Upper_L", obj.data, "bones")
col.prop_search(context.scene, "FK_Arm_Lower_L", obj.data, "bones")
col.prop_search(context.scene, "FK_Arm_End_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Upper_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Lower_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_End_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Control_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Pole_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Target_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Pole_Target_L", obj.data, "bones")
return
else:
# Display error if no armature is active
col.label(text= "No active armature", icon= 'ERROR')
# ------------------------------
# Panel for Arm R Bones
# ------------------------------
class ArmR_MappingPanel(bpy.types.Panel):
bl_idname = 'ARMR_MAPPING_PT_panel'
bl_label = 'Arm R'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
bl_parent_id = "ARMATURE_MAPPING_PT_panel"
def draw(self, context):
obj = bpy.context.active_object
col = self.layout.column()
# Check if an armature is active
if obj and obj.type == 'ARMATURE':
# Create bone selection fields for each bone involved in Arm L snapping
col.prop_search(context.scene, "FK_Arm_Upper_R", obj.data, "bones")
col.prop_search(context.scene, "FK_Arm_Lower_R", obj.data, "bones")
col.prop_search(context.scene, "FK_Arm_End_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Upper_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Lower_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_End_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Control_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Pole_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Target_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Arm_Pole_Target_R", obj.data, "bones")
return
else:
# Display error if no armature is active
col.label(text= "No active armature", icon= 'ERROR')
# ------------------------------
# Panel for Leg L Bones
# ------------------------------
class LegL_MappingPanel(bpy.types.Panel):
bl_idname = 'LEGL_MAPPING_PT_panel'
bl_label = 'Leg L'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
bl_parent_id = "ARMATURE_MAPPING_PT_panel"
def draw(self, context):
obj = bpy.context.active_object
col = self.layout.column()
# Check if an armature is active
if obj and obj.type == 'ARMATURE':
# Create bone selection fields for each bone involved in Arm L snapping
col.prop_search(context.scene, "FK_Leg_Upper_L", obj.data, "bones")
col.prop_search(context.scene, "FK_Leg_Lower_L", obj.data, "bones")
col.prop_search(context.scene, "FK_Leg_End_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Upper_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Lower_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_End_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Control_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Pole_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Target_L", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Pole_Target_L", obj.data, "bones")
return
else:
# Display error if no armature is active
col.label(text= "No active armature", icon= 'ERROR')
# ------------------------------
# Panel for Leg R Bones
# ------------------------------
class LegR_MappingPanel(bpy.types.Panel):
bl_idname = 'LEGR_MAPPING_PT_panel'
bl_label = 'Leg R'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'IK-FK Snap'
bl_parent_id = "ARMATURE_MAPPING_PT_panel"
def draw(self, context):
obj = bpy.context.active_object
col = self.layout.column()
# Check if an armature is active
if obj and obj.type == 'ARMATURE':
# Create bone selection fields for each bone involved in Arm L snapping
col.prop_search(context.scene, "FK_Leg_Upper_R", obj.data, "bones")
col.prop_search(context.scene, "FK_Leg_Lower_R", obj.data, "bones")
col.prop_search(context.scene, "FK_Leg_End_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Upper_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Lower_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_End_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Control_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Pole_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Target_R", obj.data, "bones")
col.prop_search(context.scene, "IK_Leg_Pole_Target_R", obj.data, "bones")
return
else:
# Display error if no armature is active
col.label(text= "No active armature", icon= 'ERROR')
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Register & Unregister
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
classes = [
ToolPanel,
Armature_MappingPanel,
ArmL_MappingPanel,
ArmR_MappingPanel,
LegL_MappingPanel,
LegR_MappingPanel
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)