forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHYSICS_PT_rigid_body_constraint.py
215 lines (186 loc) · 8.1 KB
/
PHYSICS_PT_rigid_body_constraint.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
# 「プロパティ」エリア > 「物理演算」タブ > 「リジッドボディコンストレイント」パネル
# "Propaties" Area > "Physics" Tab > "Rigid Body Constraint" Panel
import bpy
from bpy.props import *
################
# オペレーター #
################
class CopyConstraintSetting(bpy.types.Operator):
bl_idname = "rigidbody.copy_constraint_setting"
bl_label = "Copy Rigid Body Constraint Setting"
bl_description = "Copy active object's Rigid Body Constraint settings to other selected objects"
bl_options = {'REGISTER', 'UNDO'}
copy_target_objects : BoolProperty(name="Copy Targeted Objects", default=False)
@classmethod
def poll(cls, context):
if 2 <= len(context.selected_objects):
if context.active_object:
if context.active_object.rigid_body_constraint:
return True
return False
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, 'copy_target_objects')
def execute(self, context):
active_ob = context.active_object
for ob in context.selected_objects:
if ob.name == active_ob.name:
continue
if not ob.rigid_body_constraint:
bpy.context.view_layer.objects.active = ob
bpy.ops.rigidbody.constraint_add()
for val_name in dir(ob.rigid_body_constraint):
if not self.copy_target_objects:
if (val_name in ['object1', 'object2']):
continue
if val_name[0] != '_' and 'rna' not in val_name:
value = active_ob.rigid_body_constraint.__getattribute__(val_name)
try:
ob.rigid_body_constraint.__setattr__(val_name, value[:])
except TypeError:
try:
ob.rigid_body_constraint.__setattr__(val_name, value)
except AttributeError:
pass
except AttributeError:
pass
bpy.context.view_layer.objects.active = active_ob
return {'FINISHED'}
class ClearConstraintLimits(bpy.types.Operator):
bl_idname = "rigidbody.clear_constraint_limits"
bl_label = "Initialize Rigid Body Constraint's Limit Setting"
bl_description = "Initialize 'Limits' settings of the active object's rigid body constraint"
bl_options = {'REGISTER', 'UNDO'}
mode : StringProperty(name="Mode", default='', options={'SKIP_SAVE', 'HIDDEN'})
skip_invoke : BoolProperty(name="Skip invoke_dialog", default=False, options={'SKIP_SAVE', 'HIDDEN'})
is_lin_x : BoolProperty(name="X", default=True, options={'SKIP_SAVE'})
is_lin_y : BoolProperty(name="Y", default=True, options={'SKIP_SAVE'})
is_lin_z : BoolProperty(name="Z", default=True, options={'SKIP_SAVE'})
is_ang_x : BoolProperty(name="X", default=True, options={'SKIP_SAVE'})
is_ang_y : BoolProperty(name="Y", default=True, options={'SKIP_SAVE'})
is_ang_z : BoolProperty(name="Z", default=True, options={'SKIP_SAVE'})
def invoke(self, context, event):
if self.skip_invoke:
return self.execute(context)
else:
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
box = self.layout.box()
box.label(text="Settings to Initialize")
row = box.row()
row.label(text="Linear")
row.prop(self, 'is_lin_x')
row.prop(self, 'is_lin_y')
row.prop(self, 'is_lin_z')
row = box.row()
row.label(text="Angular")
row.prop(self, 'is_ang_x')
row.prop(self, 'is_ang_y')
row.prop(self, 'is_ang_z')
def execute(self, context):
rigid_const = context.active_object.rigid_body_constraint
if (self.mode != ''):
rigid_const.type = self.mode
for axis in ['x', 'y', 'z']:
if self.__getattribute__('is_lin_' + axis):
rigid_const.__setattr__('use_limit_lin_' + axis, True)
rigid_const.__setattr__('limit_lin_' + axis + '_lower', 0.0)
rigid_const.__setattr__('limit_lin_' + axis + '_upper', 0.0)
if self.__getattribute__('is_ang_' + axis):
rigid_const.__setattr__('use_limit_ang_' + axis, True)
rigid_const.__setattr__('limit_ang_' + axis + '_lower', 0.0)
rigid_const.__setattr__('limit_ang_' + axis + '_upper', 0.0)
return {'FINISHED'}
class ReverseConstraintLimits(bpy.types.Operator):
bl_idname = "rigidbody.reverse_constraint_limits"
bl_label = "Invert Rigid Body Constraint's Limit Setting"
bl_description = "Invert Minimum and Maximum values in the 'Limits' settings of the active object's rigid body constraint"
bl_options = {'REGISTER', 'UNDO'}
is_lin_x : BoolProperty(name="X", default=False, options={'SKIP_SAVE'})
is_lin_y : BoolProperty(name="Y", default=False, options={'SKIP_SAVE'})
is_lin_z : BoolProperty(name="Z", default=False, options={'SKIP_SAVE'})
is_ang_x : BoolProperty(name="X", default=False, options={'SKIP_SAVE'})
is_ang_y : BoolProperty(name="Y", default=False, options={'SKIP_SAVE'})
is_ang_z : BoolProperty(name="Z", default=False, options={'SKIP_SAVE'})
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
box = self.layout.box()
box.label(text="Settings to Invert")
row = box.row()
row.label(text="Linear")
row.prop(self, 'is_lin_x')
row.prop(self, 'is_lin_y')
row.prop(self, 'is_lin_z')
row = box.row()
row.label(text="Angular")
row.prop(self, 'is_ang_x')
row.prop(self, 'is_ang_y')
row.prop(self, 'is_ang_z')
def execute(self, context):
rigid_const = context.active_object.rigid_body_constraint
for axis in ['x', 'y', 'z']:
if self.__getattribute__('is_lin_' + axis):
lower = rigid_const.__getattribute__('limit_lin_' + axis + '_lower')
upper = rigid_const.__getattribute__('limit_lin_' + axis + '_upper')
rigid_const.__setattr__('limit_lin_' + axis + '_lower', -upper)
rigid_const.__setattr__('limit_lin_' + axis + '_upper', -lower)
if self.__getattribute__('is_ang_' + axis):
lower = rigid_const.__getattribute__('limit_ang_' + axis + '_lower')
upper = rigid_const.__getattribute__('limit_ang_' + axis + '_upper')
rigid_const.__setattr__('limit_ang_' + axis + '_lower', -upper)
rigid_const.__setattr__('limit_ang_' + axis + '_upper', -lower)
return {'FINISHED'}
################
# クラスの登録 #
################
classes = [
CopyConstraintSetting,
ClearConstraintLimits,
ReverseConstraintLimits
]
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])):
if context.active_object:
if context.active_object.rigid_body_constraint:
if context.active_object.rigid_body_constraint.type in ['GENERIC', 'GENERIC_SPRING']:
row = self.layout.box().row(align=True)
row.label(text="Limits Settings")
row.operator(ClearConstraintLimits.bl_idname, icon='RECOVER_LAST', text="Initialize")
row.operator(ReverseConstraintLimits.bl_idname, icon='ARROW_LEFTRIGHT', text="Invert")
elif context.active_object.rigid_body_constraint.type == 'FIXED':
row = self.layout.box().split(factor=0.25, align=True)
row.label(text="Change Type")
op1 = row.operator(ClearConstraintLimits.bl_idname, icon='IPO_LINEAR', text="Generic")
op1.mode, op1.skip_invoke = ['GENERIC', True]
op2 = row.operator(ClearConstraintLimits.bl_idname, icon='DRIVER', text="Generic Spring")
op2.mode, op2.skip_invoke = ['GENERIC_SPRING', True]
row = self.layout.split(factor=0.4)
row.use_property_split = False
row.operator(CopyConstraintSetting.bl_idname, icon='COPY_ID', text="Copy Setting")
if context.scene.rigidbody_world:
if context.scene.rigidbody_world.point_cache:
row_item = row.row(align=True)
row_item.prop(context.scene.rigidbody_world.point_cache, 'frame_start')
row_item.prop(context.scene.rigidbody_world.point_cache, 'frame_end')
row_item.operator('rigidbody.sync_frames', icon='LINKED', text="")# SCENE_PT_rigid_body_world.py で定義
if (context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
self.layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]