forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBONE_PT_inverse_kinematics.py
303 lines (269 loc) · 9.91 KB
/
BONE_PT_inverse_kinematics.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
# 「プロパティ」エリア > 「ボーン」タブ > 「インバースキネマティクス (IK)」パネル
# "Propaties" Area > "Bone" Tab > "Inverse Kinematics" Panel
import bpy
from bpy.props import *
################
# オペレーター #
################
class CopyIkSettings(bpy.types.Operator):
bl_idname = "pose.copy_ik_settings"
bl_label = "Copy IK Setting"
bl_description = "Copy active bone's IK settings to other selected bones"
bl_options = {'REGISTER', 'UNDO'}
lock_ik_x : BoolProperty(name="Lock", default=True)
ik_stiffness_x : BoolProperty(name="Stiffness", default=True)
use_ik_limit_x : BoolProperty(name="Limit", default=True)
ik_min_x : BoolProperty(name="Minimum", default=True)
ik_max_x : BoolProperty(name="Maximum", default=True)
lock_ik_y : BoolProperty(name="Lock", default=True)
ik_stiffness_y : BoolProperty(name="Stiffness", default=True)
use_ik_limit_y : BoolProperty(name="Limit", default=True)
ik_min_y : BoolProperty(name="Minimum", default=True)
ik_max_y : BoolProperty(name="Maximum", default=True)
lock_ik_z : BoolProperty(name="Lock", default=True)
ik_stiffness_z : BoolProperty(name="Stiffness", default=True)
use_ik_limit_z : BoolProperty(name="Limit", default=True)
ik_min_z : BoolProperty(name="Minimum", default=True)
ik_max_z : BoolProperty(name="Maximum", default=True)
ik_stretch : BoolProperty(name="Stretch", default=True)
@classmethod
def poll(cls, context):
if context.selected_pose_bones:
if (2 <= len(context.selected_pose_bones)):
return True
return False
def draw(self, context):
self.layout.prop(self, 'ik_stretch')
for axis in ['x', 'y', 'z']:
box = self.layout.box()
row = box.row()
row.label(text=f"{axis.upper()}")
row.prop(self, 'lock_ik_'+axis)
row.prop(self, 'ik_stiffness_'+axis)
row.prop(self, 'use_ik_limit_'+axis)
row = box.box().row()
row.label(text="Limit Rotation")
row.prop(self, 'ik_min_'+axis)
row.prop(self, 'ik_max_'+axis)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
source = context.active_pose_bone
for target in context.selected_pose_bones[:]:
if (source.name != target.name):
for axis in ['x', 'y', 'z']:
if (self.__getattribute__('lock_ik_'+axis)):
target.__setattr__('lock_ik_'+axis, source.__getattribute__('lock_ik_'+axis))
if (self.__getattribute__('ik_stiffness_'+axis)):
target.__setattr__('ik_stiffness_'+axis, source.__getattribute__('ik_stiffness_'+axis))
if (self.__getattribute__('use_ik_limit_'+axis)):
target.__setattr__('use_ik_limit_'+axis, source.__getattribute__('use_ik_limit_'+axis))
if (self.__getattribute__('ik_min_'+axis)):
target.__setattr__('ik_min_'+axis, source.__getattribute__('ik_min_'+axis))
if (self.__getattribute__('ik_max_'+axis)):
target.__setattr__('ik_max_'+axis, source.__getattribute__('ik_max_'+axis))
if (self.ik_stretch):
target.ik_stretch = source.ik_stretch
return {'FINISHED'}
class ReverseIkMinMax(bpy.types.Operator):
bl_idname = "pose.reverse_ik_min_max"
bl_label = "Reverse Minimum and maximum Angles"
bl_description = "Choose axes and reverse their minimum and maximum angles for IK Limit"
bl_options = {'REGISTER', 'UNDO'}
is_x : BoolProperty(name="X axis", default=False)
is_y : BoolProperty(name="Y axis", default=False)
is_z : BoolProperty(name="Z axis", default=False)
@classmethod
def poll(cls, context):
bone = context.active_pose_bone
if (bone):
for axis in ['x', 'y', 'z']:
diff = bone.__getattribute__('ik_min_' + axis) + bone.__getattribute__('ik_max_' + axis)
if (diff != 0):
return True
return False
return False
def invoke(self, context, event):
bone = context.active_pose_bone
for axis in ['x', 'y', 'z']:
diff = bone.__getattribute__('ik_min_' + axis) + bone.__getattribute__('ik_max_' + axis)
if (diff != 0):
self.__setattr__('is_' + axis, True)
else:
self.__setattr__('is_' + axis, False)
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
row = self.layout.row()
row.label(text="")
for p in ['is_x','is_y','is_y']:
row.prop(self, p)
def execute(self, context):
bone = context.active_pose_bone
if (self.is_x):
ik_min = bone.ik_min_x
ik_max = bone.ik_max_x
bone.ik_min_x = -ik_max
bone.ik_max_x = -ik_min
if (self.is_y):
ik_min = bone.ik_min_y
ik_max = bone.ik_max_y
bone.ik_min_y = -ik_max
bone.ik_max_y = -ik_min
if (self.is_z):
ik_min = bone.ik_min_z
ik_max = bone.ik_max_z
bone.ik_min_z = -ik_max
bone.ik_max_z = -ik_min
for area in context.screen.areas:
area.tag_redraw()
return {'FINISHED'}
class CopyIkAxisSetting(bpy.types.Operator):
bl_idname = "pose.copy_ik_axis_setting"
bl_label = "Copy IK Settings to other axes"
bl_description = "Choose an axis and copy its IK settings to other axes"
bl_options = {'REGISTER', 'UNDO'}
items = [
('x', "X Axis", "", 1),
('y', "Y Axis", "", 2),
('z', "Z Axis", "", 3),
]
source_axis : EnumProperty(items=items, name="Original Axis")
target_x : BoolProperty(name="X Axis", default=True)
target_y : BoolProperty(name="Y Axis", default=True)
target_z : BoolProperty(name="Z Axis", default=True)
lock_ik : BoolProperty(name="Lock", default=True)
ik_stiffness : BoolProperty(name="Stiffness", default=True)
use_ik_limit : BoolProperty(name="Limit", default=True)
ik_min : BoolProperty(name="Minimum", default=True)
ik_max : BoolProperty(name="Maximum", default=True)
@classmethod
def poll(cls, context):
bone = context.active_pose_bone
if (bone):
for setting in ['lock_ik', 'ik_stiffness', 'use_ik_limit', 'ik_min', 'ik_max']:
x = bone.__getattribute__(setting + '_x')
y = bone.__getattribute__(setting + '_y')
z = bone.__getattribute__(setting + '_z')
if (x == y == z):
pass
else:
return True
return False
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
sp = self.layout.split(factor=0.35)
spitem = sp.split(factor=0.6)
spitem.prop(self, 'source_axis', text="")
spitem.label(text="to")
rowitem = sp.box().row(align=True)
rowitem.prop(self, 'target_x')
rowitem.prop(self, 'target_y')
rowitem.prop(self, 'target_z')
box = self.layout.box()
box.label(text="Settings to copy")
row = box.row()
row.prop(self, 'lock_ik')
row.prop(self, 'ik_stiffness')
row.prop(self, 'use_ik_limit')
row = box.box().row()
row.label(text="Limit Rotation")
row.prop(self, 'ik_min')
row.prop(self, 'ik_max')
def execute(self, context):
bone = context.active_pose_bone
source_axis = self.source_axis
for axis in ['x', 'y', 'z']:
if (source_axis == axis):
continue
if (self.__getattribute__('target_' + axis)):
if (self.lock_ik):
value = bone.__getattribute__('lock_ik_' + source_axis)
bone.__setattr__('lock_ik_' + axis, value)
if (self.ik_stiffness):
value = bone.__getattribute__('ik_stiffness_' + source_axis)
bone.__setattr__('ik_stiffness_' + axis, value)
if (self.use_ik_limit):
value = bone.__getattribute__('use_ik_limit_' + source_axis)
bone.__setattr__('use_ik_limit_' + axis, value)
if (self.ik_min):
value = bone.__getattribute__('ik_min_' + source_axis)
bone.__setattr__('ik_min_' + axis, value)
if (self.ik_max):
value = bone.__getattribute__('ik_max_' + source_axis)
bone.__setattr__('ik_max_' + axis, value)
for area in context.screen.areas:
area.tag_redraw()
return {'FINISHED'}
class ResetIkSettings(bpy.types.Operator):
bl_idname = "pose.reset_ik_settings"
bl_label = "Reset IK Settings"
bl_description = "Reset all of IK settings"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
bone = context.active_pose_bone
if bone:
for axis in ['x', 'y', 'z']:
if getattr(bone, 'lock_ik_' + axis) != False:
return True
if getattr(bone, 'ik_stiffness_' + axis) != 0.0:
return True
if getattr(bone, 'use_ik_limit_' + axis) != False:
return True
if -3.1415927410125 <= getattr(bone, 'ik_min_' + axis):
return True
if getattr(bone, 'ik_max_' + axis) <= 3.1415927410125:
return True
if bone.ik_stretch != 0.0:
return True
return False
def execute(self, context):
bone = context.active_pose_bone
for axis in ['x', 'y', 'z']:
setattr(bone, 'lock_ik_' + axis, False)
setattr(bone, 'ik_stiffness_' + axis, 0.0)
setattr(bone, 'use_ik_limit_' + axis, False)
setattr(bone, 'ik_min_' + axis, -3.1415927410125732)
setattr(bone, 'ik_max_' + axis, 3.1415927410125732)
bone.ik_stretch = 0.0
for area in context.screen.areas:
area.tag_redraw()
return {'FINISHED'}
################
# クラスの登録 #
################
classes = [
CopyIkSettings,
ReverseIkMinMax,
CopyIkAxisSetting,
ResetIkSettings
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
################
# メニュー追加 #
################
# メニューのオン/オフの判定
def IsMenuEnable(self_id):
for id in bpy.context.preferences.addons[__name__.partition('.')[0]].preferences.disabled_menu.split(','):
if (id == self_id):
return False
else:
return True
# メニューを登録する関数
def menu(self, context):
if (IsMenuEnable(__name__.split('.')[-1])):
spl = self.layout.split(factor=0.75)
row = spl.row(align=True)
row.operator(ReverseIkMinMax.bl_idname, icon='ARROW_LEFTRIGHT', text="Reverse min & max")
row.operator(CopyIkAxisSetting.bl_idname, icon='LINKED', text="Copy to other axes")
spl.operator(ResetIkSettings.bl_idname, icon='X', text="Initialize")
if 2 <= len(context.selected_pose_bones):
self.layout.operator(CopyIkSettings.bl_idname, icon='COPY_ID', text="Copy IK Setting to selected bones")
if (context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
self.layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]