-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstancecollOt.py
393 lines (276 loc) · 16 KB
/
instancecollOt.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Name : Smart Instance Collection
# Description : Converts Selected Objects into an Instance Collection
# Author : Neltulz (Neil V. Moore)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import bpy
import mathutils
from . import miscFunc
from . import miscLay
from bpy.props import (StringProperty, BoolProperty, IntProperty, FloatProperty, FloatVectorProperty, EnumProperty, PointerProperty)
from bpy.types import (Panel, Operator, AddonPreferences, PropertyGroup)
# -----------------------------------------------------------------------------
# Smart Unparent
# -----------------------------------------------------------------------------
class VIEW3D_OT_ntzbu_smart_instance_collection(Operator):
"""Tooltip"""
bl_idname = 'view3d.ntzbu_smart_instance_collection'
bl_label = 'NTZBU : Smart Instance Collection'
bl_description = 'Converts Selected Objects into an Instance Collection'
bl_options = {'REGISTER', 'UNDO',
#'PRESET'
}
showConfirmDiag : BoolProperty (
name = 'Show Confirm Dialog',
description = 'Shows Confirmation Dialog',
default = False,
)
confirmDiagReason : StringProperty (
name="Confirm Dialog Reason",
description="Reason for showing the confirm dialog",
default = '',
)
instCollName : StringProperty (
name="Instance Collection Name",
description="Name of the resulting instance collection",
default = 'Collection',
)
collSceneDest : StringProperty (
name="Scene Destination",
description="Name of the scene for the collection of objects to go to",
default = 'Assets',
)
modeAtBegin = "OBJECT"
cursorLocAtBegin = None
showOperatorOptions : BoolProperty (
name = 'Show Operator Options',
description = 'Shows additional operator options',
default = False,
)
instOrigin_List = [
("TFORMPIVOT", "Default", 'Cursor will be placed using the user chosen "Transform Pivot Point" method', "", 0),
None,
("CURSOR", "3D Cursor", "", "", 1),
("BOUNDING_BOX_CENTER", "Bounding Box Center", "", "", 2),
("MEDIAN_POINT", "Median Point", "", "", 3),
("ACTIVE_ELEMENT", "Active Element", "", "", 4),
("WORIGIN", "World Origin", "", "", 5),
]
instOrigin : EnumProperty (
items = instOrigin_List,
name = "Instance Pivot Location",
default = "TFORMPIVOT"
)
autoCollOffset : BoolProperty (
name="Auto Collection Offset",
description="Automatically calculate collection offset when moving objects to another scene",
default = True,
)
offsetMargin : FloatProperty (
name="Offset Margin",
description="Margin between nearby objects",
default = 10,
)
switchToDestScn : BoolProperty (
name="Switch to Destination Scene",
description="Switch to destination scene",
default = False,
)
@classmethod
def poll(cls, context):
return (context.mode == "OBJECT") and ( len(context.selected_objects) > 0 )
def invoke(self, context, event):
selObjs = context.selected_objects
self.confirmDiagReason = ''
self.showConfirmDiag = False #declare
for obj in selObjs:
if obj.instance_type == "COLLECTION":
self.showConfirmDiag = True
self.confirmDiagReason = 'COLLECTION_DETECTED'
break
addonPrefs = context.preferences.addons[__package__].preferences
scn = context.scene
'''
# BEGIN Retreive Operator properties from addon preferences
opPropNameList = ['emptyName', 'emptyLocation', 'emptySize']
miscFunc.retreive_op_props_from_addonPrefs(self, context, addonPrefs=addonPrefs, opPropNameList=opPropNameList, opPropPrefix='groupWithEmpty_')
# END Retreive Operator properties from addon preferences
'''
#EXPAND properties?
if addonPrefs.smartInstColl_showOperatorOptions == 'EXPAND':
self.showOperatorOptions = True
else:
self.showOperatorOptions = False
#store cursor location at begin
self.cursorLocAtBegin = mathutils.Vector(scn.cursor.location)
#store mode at begin
self.modeAtBegin = context.mode
if self.showConfirmDiag:
return context.window_manager.invoke_props_dialog(self, width=300)
else:
return context.window_manager.invoke_props_dialog(self)
#return self.execute(context)
# END invoke()
def draw(self, context):
scn = context.scene
lay = self.layout.column(align=True)
if self.showOperatorOptions:
box = lay.box().column(align=True)
miscLay.createPropOLD(self, context, labelText='Origin', data=self, propItem='instOrigin', layout=box)
box.separator()
miscLay.createPropOLD(self, context, propText='Auto Collection Offset', data=self, propItem='autoCollOffset', layout=box)
box.separator()
offsetMargin_row = box.row(align=True)
offsetMargin_row.active = self.autoCollOffset
miscLay.createPropOLD(self, context, labelText='Offset Margin', data=self, propItem='offsetMargin', layout=offsetMargin_row)
box.separator()
miscLay.createPropOLD(self, context, propText='Switch to Scene', data=self, propItem='switchToDestScn', layout=box)
lay.separator()
row = lay.row(align=False)
instCollName_col = row.column(align=True)
labelText = instCollName_col.row(align=True)
labelText.label(text='Collection Name:')
instCollName_row = instCollName_col.row(align=True)
instCollName_row.scale_y = 1.25
instCollName_row.prop(self, 'instCollName', text='')
collSceneDest_col = row.column(align=True)
collSceneDest_col.label(text='Scene Name:')
collSceneDest_row = collSceneDest_col.row(align=True)
collSceneDest_row.scale_y = 1.25
collSceneDest_row.prop(self, 'collSceneDest', text='')
optionsShowHide_row = row.column(align=True)
optionsShowHide_row.scale_y = 1
optionsShowHide_row.label(text='', icon="BLANK1")
vSpacer = optionsShowHide_row.row(align=True)
vSpacer.scale_y = 1
if self.showOperatorOptions: icon = 'TRIA_UP'
else: icon = 'TRIA_RIGHT'
optionsShowHide_btn = optionsShowHide_row.row(align=True)
optionsShowHide_btn.scale_y = 1.25
optionsShowHide_btn.prop(self, 'showOperatorOptions', text='', icon=icon)
if self.showConfirmDiag:
if self.confirmDiagReason == 'COLLECTION_DETECTED':
lay.separator()
numSelObjs = len(context.selected_objects)
box = lay.box().column(align=True)
box.separator()
row = box.row(align=True)
icon = row.column(align=True)
icon.scale_y = 1.5
icon.ui_units_x = 2
miscLay.forceJustifyText(lay=icon, text='', icon='ERROR', alignment='RIGHT')
row.separator()
paragraph = row.column(align=True)
paragraph.scale_y = 0.75
if numSelObjs == 1:
miscLay.forceJustifyText(lay=paragraph, text='The selected object is an', alignment='LEFT')
miscLay.forceJustifyText(lay=paragraph, text='"Instance Collection"', alignment='LEFT')
else:
miscLay.forceJustifyText(lay=paragraph, text='One (or more) of the selected', alignment='LEFT')
miscLay.forceJustifyText(lay=paragraph, text='objects is an "Instance Collection"', alignment='LEFT')
box.separator()
# END draw()
def execute(self, context):
self.showConfirmDiag = False #reset
self.confirmDiagReason = '' #reset
if context.mode == "OBJECT":
# BEGIN ensure one of the selected objects is an active object
# ------------------------------------------------------------
activeObjAtBegin = context.view_layer.objects.active
selObjs = context.selected_objects
if len(selObjs) > 0:
if (activeObjAtBegin is None) or (not activeObjAtBegin in selObjs):
context.view_layer.objects.active = selObjs[0]
activeObjAtBegin = selObjs[0]
# ------------------------------------------------------------
# END ensure active obj
scn = context.scene
# BEGIN Snap Cursor Based on 'self.instOrigin'
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tformPivotAtBegin = str(scn.tool_settings.transform_pivot_point)
if self.instOrigin == "TFORMPIVOT":
if tformPivotAtBegin == "BOUNDING_BOX_CENTER":
miscFunc.snapCursorToBoundingBoxCenter(self, context, activeObj=activeObjAtBegin, objMode=self.modeAtBegin, tformPivotPoint=tformPivotAtBegin)
elif tformPivotAtBegin == "MEDIAN_POINT":
miscFunc.snapCursorToMedianPoint(self, context, activeObj=activeObjAtBegin, objMode=self.modeAtBegin, tformPivotPoint=tformPivotAtBegin)
elif tformPivotAtBegin == "ACTIVE_ELEMENT":
bpy.ops.view3d.snap_cursor_to_active()
elif tformPivotAtBegin == "CURSOR":
pass
else:
miscFunc.snapCursorToMedianPoint(self, context, activeObj=activeObjAtBegin, objMode=self.modeAtBegin, tformPivotPoint=tformPivotAtBegin) #all other situations (e.g. individual origins, cursor, etc)
elif self.instOrigin == "DEFAULT":
bpy.ops.view3d.snap_cursor_to_selected()
elif self.instOrigin == "BOUNDING_BOX_CENTER":
scn.tool_settings.transform_pivot_point = self.instOrigin
miscFunc.snapCursorToBoundingBoxCenter(self, context, activeObj=activeObjAtBegin, objMode=self.modeAtBegin, tformPivotPoint=tformPivotAtBegin)
scn.tool_settings.transform_pivot_point = tformPivotAtBegin #reset
elif self.instOrigin == "MEDIAN_POINT":
scn.tool_settings.transform_pivot_point = self.instOrigin
miscFunc.snapCursorToMedianPoint(self, context, activeObj=activeObjAtBegin, objMode=self.modeAtBegin, tformPivotPoint=tformPivotAtBegin)
scn.tool_settings.transform_pivot_point = tformPivotAtBegin #reset
elif self.instOrigin == "ACTIVE_ELEMENT":
scn.tool_settings.transform_pivot_point = self.instOrigin
bpy.ops.view3d.snap_cursor_to_active()
scn.tool_settings.transform_pivot_point = tformPivotAtBegin #reset
elif self.instOrigin == "WORIGIN":
scn.cursor.location = (0,0,0)
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# END Snap Cursor Based on 'self.instOrigin'
#check if destination scene already exists
destScn = None #declare
for scn in bpy.data.scenes:
if scn.name == self.collSceneDest:
destScn = scn
if destScn is None:
#create a new scene with user specified name
destScn = bpy.data.scenes.new(name=self.collSceneDest)
#create a new collection in the new scene with user specified name
newColl = bpy.data.collections.new(self.instCollName)
destScn.collection.children.link(newColl)
context.window.scene = destScn #switch to destination scene
destObjs = [] #declare
for obj in destScn.objects:
destObjs.append(obj)
obj.select_set(False)
context.window.scene = scn #switch back to original scene
totalCollOffsetVec = mathutils.Vector( (0, 0, 0) ) #declare
if self.autoCollOffset:
#calculate dimension of selected objects
selDim = miscFunc.max_dim_from_objs_or_empties(self, context, minMaxAvg='MAX', objs=selObjs)
destDim = 0 #declare
if destObjs != []:
#calculate dimension of objects in the destination scene
destDim = miscFunc.max_dim_from_objs_or_empties(self, context, minMaxAvg='MAX', objs=destObjs)
#create vector from dimension of objects in the destination scene
destDimVec = mathutils.Vector( (destDim, 0, 0) )
#create vector from offset Margin
if destDim > 0:
offsetMarginVec = mathutils.Vector( (self.offsetMargin, 0, 0) )
else:
offsetMarginVec = mathutils.Vector( (0, 0, 0) )
#add the two together to create a total offset vector
totalCollOffsetVec = destDimVec + offsetMarginVec
for obj in selObjs:
#move objects to the origin, and then offset them by their own object location minus the 3D cursor location, and then offset the objects so that they do not overlap any existing objects in the scene
obj.location = mathutils.Vector( (0,0,0) ) + ( obj.location - scn.cursor.location ) + totalCollOffsetVec
#find all collections that the obj is currently linked to
objColls = obj.users_collection
#link selected object to the new collection in the new scene
newColl.objects.link(obj)
#unlink the object from all of the collections it's currently linked to
for objColl in objColls:
objColl.objects.unlink(obj)
#set instance collection offset so that the origin ends up where the user wanted it
newColl.instance_offset = totalCollOffsetVec
#create an instance collection of the selected objects in the original place of the selected objects
bpy.ops.object.collection_instance_add(collection=newColl.name, location=scn.cursor.location)
if self.switchToDestScn:
context.window.scene = destScn
bpy.ops.view3d.view_all('INVOKE_DEFAULT', use_all_regions=True)
scn.cursor.location = self.cursorLocAtBegin #reset
else:
self.report({'INFO'}, 'Unsupported mode detected. Please use "Object Mode".' )
return {'FINISHED'}
# END execute()
# END Operator()