/
operators.py
304 lines (212 loc) · 10 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
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
from typing import Any
import bpy
from .property_groups import FamilySettings
from .functions import (
set_root_object_active, # type: ignore
)
class DuplicateMoveHierarchy(bpy.types.Operator):
"Duplicate selected objects with all their recursive children and move them"
bl_idname = "object.duplicate_move_hierarchy"
bl_label = "Duplicate Hierarchy"
bl_options = {"MACRO"}
def execute(self, context: bpy.types.Context):
family_settings: FamilySettings = getattr(context.scene, "family_settings")
take_hierarchy = family_settings.duplicate_hierarchy # type: ignore
selected_objects = context.selected_objects
if take_hierarchy:
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type: ignore
for target in targets:
target.select_set(True)
bpy.ops.object.duplicate_move() # type: ignore
set_root_object_active(context)
return {"FINISHED"}
def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
family_settings: FamilySettings = getattr(context.scene, "family_settings")
take_hierarchy = family_settings.duplicate_hierarchy # type: ignore
selected_objects = context.selected_objects
if take_hierarchy:
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type: ignore
for target in targets:
target.select_set(True)
bpy.ops.object.duplicate_move("INVOKE_DEFAULT") # type: ignore
set_root_object_active(context)
return {"FINISHED"}
class DuplicateMoveHierarchyLinked(bpy.types.Operator):
"Duplicate selected objects with all their recursive children, but not their object data, and move them"
bl_idname = "object.duplicate_move_hierarchy_linked"
bl_label = "Duplicate Hierarchy Linked"
bl_options = {"MACRO"}
def execute(self, context: bpy.types.Context):
family_settings: FamilySettings = getattr(context.scene, "family_settings")
take_hierarchy = family_settings.duplicate_hierarchy # type: ignore
selected_objects = context.selected_objects
if take_hierarchy:
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type: ignore
for target in targets:
target.select_set(True)
bpy.ops.object.duplicate_move_linked() # type: ignore
set_root_object_active(context)
return {"FINISHED"}
def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
family_settings: FamilySettings = getattr(context.scene, "family_settings")
take_hierarchy = family_settings.duplicate_hierarchy # type: ignore
selected_objects = context.selected_objects
if take_hierarchy:
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type: ignore
for target in targets:
target.select_set(True)
bpy.ops.object.duplicate_move_linked("INVOKE_DEFAULT") # type: ignore
set_root_object_active(context)
return {"FINISHED"}
class ShowDeleteMenu(bpy.types.Operator):
"Show options for performing deletion"
bl_idname = "object.show_delete_menu"
bl_label = "Show Delete Menu"
bl_options = {"MACRO"}
def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
bpy.ops.wm.call_menu(name="OBJECT_MT_delete_menu") # type: ignore
return {"FINISHED"}
class DeleteKeepChildrenTransformation(bpy.types.Operator):
"Delete selected objects and keep their children's transformation"
bl_idname = "object.delete_keep_children_transformation"
bl_label = "Delete and Keep Children's Transformation"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context: bpy.types.Context):
selected_objects = context.selected_objects
if len(selected_objects) == 0:
return {"PASS_THROUGH"}
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children) # type:ignore
for target in targets:
target.select_set(True)
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM") # type:ignore
for target in targets:
target.select_set(False)
for selected_object in selected_objects:
selected_object.select_set(True)
bpy.ops.object.delete() # type:ignore
return {"FINISHED"}
class DeleteHierarchy(bpy.types.Operator):
"Delete selected objects including all their recursive children"
bl_idname = "object.delete_hierarchy"
bl_label = "Delete Hierarchy"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context: bpy.types.Context):
selected_objects = context.selected_objects
if len(selected_objects) == 0:
return {"PASS_THROUGH"}
targets: set[bpy.types.Object] = set(selected_objects)
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type:ignore
for target in targets:
target.select_set(True)
bpy.ops.object.delete() # type:ignore
return {"FINISHED"}
class SelectRelated(bpy.types.Operator):
"Select objects up or down the hierarchy"
bl_idname = "object.select_hierarchy"
bl_label = "Select Related"
bl_options = {"REGISTER", "UNDO_GROUPED"}
direction_items: list[Any] = [
("CHILD", "Select Child", "", 1),
("PARENT", "Select Parent", "", 2),
]
direction: bpy.props.EnumProperty( # type:ignore
name="Direction",
items=direction_items, # type:ignore
default="CHILD",
)
extend: bpy.props.BoolProperty( # type:ignore
name="Extend",
default=True,
)
def execute(self, context: bpy.types.Context):
selected_objects = context.selected_objects
if len(selected_objects) == 0:
return {"PASS_THROUGH"}
if not self.extend: # type: ignore
for selected_object in selected_objects:
selected_object.select_set(False)
targets: set[bpy.types.Object] = set()
if self.direction == "CHILD": # type: ignore
for selected_object in selected_objects:
children: list[bpy.types.Object] = selected_object.children # type: ignore
if len(children) == 0:
targets.add(selected_object)
else:
targets.update(children)
elif self.direction == "PARENT": # type: ignore
for selected_object in selected_objects:
parent = selected_object.parent
if parent is None:
targets.add(selected_object)
else:
targets.add(parent)
for target in targets:
target.select_set(True)
set_root_object_active(context)
return {"FINISHED"}
class SelectHierarchy(bpy.types.Operator):
"Add all recursive children of selected objects to selection"
bl_idname = "object.select_real_hierarchy"
bl_label = "Select Hierarchy"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context: bpy.types.Context):
selected_objects = context.selected_objects
if len(selected_objects) == 0:
return {"PASS_THROUGH"}
targets: set[bpy.types.Object] = set()
for selected_object in selected_objects:
targets.update(selected_object.children_recursive) # type: ignore
for target in targets:
target.select_set(True)
set_root_object_active(context)
return {"FINISHED"}
class RelateObjects(bpy.types.Operator):
"Make parent-child relationship keeping children's transform if objects other than the active one are selected, otherwise remove parent-child relationship keeping children's transform"
bl_idname = "object.relate_objects"
bl_label = "Relate Objects"
bl_options = {"REGISTER", "UNDO_GROUPED"}
def execute(self, context: bpy.types.Context):
selected_objects = context.selected_objects
active_object = context.view_layer.objects.active # type: ignore
if active_object is None:
return {"PASS_THROUGH"}
create_relationship = False
for selected_object in selected_objects:
if selected_object is not active_object:
create_relationship = True
break
if create_relationship:
should_clear_parent_on_active = False
for selected_object in selected_objects:
if active_object in selected_object.children_recursive: # type: ignore
should_clear_parent_on_active = True
break
if should_clear_parent_on_active:
for selected_object in selected_objects:
selected_object.select_set(False)
active_object.select_set(True) # type: ignore
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM") # type: ignore
for selected_object in selected_objects:
selected_object.select_set(True)
bpy.ops.object.parent_set(keep_transform=True) # type: ignore
for selected_object in selected_objects:
selected_object.select_set(False)
active_object.select_set(True) # type: ignore
else:
active_object.select_set(False) # type: ignore
children: list[bpy.types.Object] = active_object.children # type: ignore
for child in children:
child.select_set(True)
bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM") # type: ignore
return {"FINISHED"}