-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOutliner_Menu_Entries.py
281 lines (220 loc) · 11.1 KB
/
Outliner_Menu_Entries.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
#https://blender.stackexchange.com/questions/64951/add-menu-entry-to-generic-right-click-menu
#https://stackoverflow.com/questions/27265915/get-list-of-selected-objects-as-string-blender-python
#https://blender.stackexchange.com/questions/132825/python-selecting-object-by-name-in-2-8/132829
#https://blenderartists.org/t/python-scripted-button-select-hierarchy-parent-and-children-at-same-time/1132109/4
# Add-on info
bl_info = {
"name": "Outliner Menu Entries",
"author": "APEC",
"version": (0, 1, 0),
"blender": (2, 92, 0),
"location": "Outliner > Objects RMB or Collections RMB",
"description": "Adds menu entries for objects and collections in Outliner",
"doc_url": "",
"tracker_url": "",
"category": "Outliner"
}
import bpy
from bpy.types import Operator
from collections import defaultdict
###########################################################################################
################################### Functions #############################################
###########################################################################################
class OUTLINER_OT_duplicate(Operator):
bl_idname = "outliner.duplicate"
bl_label = "Duplicate"
bl_description = '''Duplicate selected objects'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.duplicate()
#dupli_obj = bpy.context.object
return { 'FINISHED' }
class OUTLINER_OT_duplicate_hierarchy(Operator):
bl_idname = "outliner.duplicate_hierarchy"
bl_label = "Duplicate Hierarchy"
bl_description = '''Duplicate selected objects with respecting the hierarchy'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
selection_names = [obj.name for obj in bpy.context.selected_objects]
for o in selection_names:
obj = bpy.context.scene.objects.get(o)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.select_grouped(extend=True, type='CHILDREN_RECURSIVE')
bpy.ops.object.duplicate()
return { 'FINISHED' }
class OUTLINER_OT_duplicate_instance(Operator):
bl_idname = "outliner.duplicate_instance"
bl_label = "Duplicate Instance"
bl_description = '''Duplicate selected objects with instances'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.duplicate(linked=True)
#dupli_obj = bpy.context.object
return { 'FINISHED' }
class OUTLINER_OT_duplicate_instance_hierarchy(Operator):
bl_idname = "outliner.duplicate_instance_hierarchy"
bl_label = "Duplicate Instance Hierarchy"
bl_description = '''Duplicate selected objects with instances respecting the hierarchy'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
selection_names = [obj.name for obj in bpy.context.selected_objects]
for o in selection_names:
obj = bpy.context.scene.objects.get(o)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.select_grouped(extend=True, type='CHILDREN_RECURSIVE')
bpy.ops.object.duplicate(linked=True)
return { 'FINISHED' }
class OUTLINER_OT_select_hierarchy(Operator):
bl_idname = "outliner.select_hierarchy"
bl_label = "Select Hierarchy Multi"
bl_description = '''Selects hierarchy and parent for multiple objects'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
bpy.ops.object.mode_set(mode = 'OBJECT')
selection_names = [obj.name for obj in bpy.context.selected_objects]
for o in selection_names:
obj = bpy.context.scene.objects.get(o)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.select_grouped(extend=True, type='CHILDREN_RECURSIVE')
return { 'FINISHED' }
#https://blenderartists.org/t/how-to-select-all-objects-of-a-known-collection-with-python/1195742/3
class OUTLINER_OT_select_collection_objects(Operator):
bl_idname = "outliner.select_collection_objects"
bl_label = "Select Objects Multi"
bl_description = '''Selects all objects for multiple collections'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
collection_names = [col.name for col in bpy.context.selected_ids]
# use only collections whose names are in collection_names
collections = [col for col in bpy.data.collections if col.name in collection_names]
for col in collections:
for obj in col.all_objects:
obj.select_set(True)
return { 'FINISHED' }
def copy_objects(from_col, to_col, linked, dupe_lut):
for o in from_col.objects:
dupe = o.copy()
if not linked and o.data:
dupe.data = dupe.data.copy()
to_col.objects.link(dupe)
dupe_lut[o] = dupe
def copy(parent, collection, linked=False):
dupe_lut = defaultdict(lambda : None)
def _copy(parent, collection, linked=False):
cc = bpy.data.collections.new(collection.name)
copy_objects(collection, cc, linked, dupe_lut)
for c in collection.children:
_copy(cc, c, linked)
parent.children.link(cc)
_copy(parent, collection, linked)
print(dupe_lut)
for o, dupe in tuple(dupe_lut.items()):
parent = dupe_lut[o.parent]
if parent:
dupe.parent = parent
#https://blender.stackexchange.com/questions/157828/python-collection-duplicate-help
class OUTLINER_OT_duplicate_collections(Operator):
bl_idname = "outliner.duplicate_collections"
bl_label = "Duplicate Collection Multi"
bl_description = '''Duplicate multiple selected collections'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
context = bpy.context
scene = context.scene
collection_names = [col.name for col in bpy.context.selected_ids]
for col_name in collection_names:
col = bpy.data.collections.get(col_name)
print(col, scene.collection)
assert(col is not scene.collection)
parent_col = context.scene.collection
copy(scene.collection, col)
# and linked copy
#copy(scene.collection, col, linked=True)
return { 'FINISHED' }
class OUTLINER_OT_duplicate_collections_linked(Operator):
bl_idname = "outliner.duplicate_collections_linked"
bl_label = "Duplicate Linked Multi"
bl_description = '''Duplicate multiple selected collections with linked object data'''
bl_options = { 'REGISTER', 'UNDO' }
def execute(self, context):
context = bpy.context
scene = context.scene
collection_names = [col.name for col in bpy.context.selected_ids]
for col_name in collection_names:
col = bpy.data.collections.get(col_name)
print(col, scene.collection)
assert(col is not scene.collection)
parent_col = context.scene.collection
copy(scene.collection, col, linked=True)
return { 'FINISHED' }
###########################################################################################
##################################### UI ############################################
###########################################################################################
def duplicate_menu_outliner(self, context):
layout = self.layout
layout.separator()
layout.operator(OUTLINER_OT_duplicate.bl_idname)
layout.operator(OUTLINER_OT_duplicate_hierarchy.bl_idname)
layout.separator()
layout.operator(OUTLINER_OT_duplicate_instance.bl_idname)
layout.operator(OUTLINER_OT_duplicate_instance_hierarchy.bl_idname)
layout.separator()
layout.operator(OUTLINER_OT_select_hierarchy.bl_idname)
def select_colection_obects_menu_outliner(self, context):
layout = self.layout
layout.separator()
layout.operator(OUTLINER_OT_duplicate_collections.bl_idname)
layout.operator(OUTLINER_OT_duplicate_collections_linked.bl_idname)
layout.separator()
layout.operator(OUTLINER_OT_select_collection_objects.bl_idname)
###########################################################################################
##################################### Register ############################################
###########################################################################################
def register():
bpy.utils.register_class(OUTLINER_OT_duplicate)
bpy.utils.register_class(OUTLINER_OT_duplicate_hierarchy)
bpy.utils.register_class(OUTLINER_OT_duplicate_instance)
bpy.utils.register_class(OUTLINER_OT_duplicate_instance_hierarchy)
bpy.utils.register_class(OUTLINER_OT_select_hierarchy)
bpy.types.OUTLINER_MT_object.append(duplicate_menu_outliner)
bpy.utils.register_class(OUTLINER_OT_select_collection_objects)
bpy.utils.register_class(OUTLINER_OT_duplicate_collections)
bpy.utils.register_class(OUTLINER_OT_duplicate_collections_linked)
bpy.types.OUTLINER_MT_collection.append(select_colection_obects_menu_outliner)
def unregister():
bpy.utils.unregister_class(OUTLINER_OT_duplicate)
bpy.utils.unregister_class(OUTLINER_OT_duplicate_hierarchy)
bpy.utils.unregister_class(OUTLINER_OT_duplicate_instance)
bpy.utils.unregister_class(OUTLINER_OT_duplicate_instance_hierarchy)
bpy.utils.unregister_class(OUTLINER_OT_select_hierarchy)
bpy.types.OUTLINER_MT_object.remove(duplicate_menu_outliner)
bpy.utils.unregister_class(OUTLINER_OT_select_collection_objects)
bpy.utils.unregister_class(OUTLINER_OT_duplicate_collections)
bpy.utils.unregister_class(OUTLINER_OT_duplicate_collections_linked)
bpy.types.OUTLINER_MT_collection.remove(select_colection_obects_menu_outliner)
if __name__ == "__main__":
register()