-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscFunc.py
406 lines (267 loc) · 11.6 KB
/
miscFunc.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
394
395
396
397
398
399
400
401
402
403
404
405
406
import bpy
import bmesh
from operator import itemgetter
from mathutils import Vector
import numpy
from itertools import chain
from itertools import permutations
def average(lst):
return sum(lst) / len(lst)
def meanVector(lst):
xLst = [x for x in lst[0]]
#yLst = [y for y in lst[1]]
#zLst = [z for z in lst[2]]
#vectorList = xLst + yLst + zLst
#result = (numpy.mean(xLst), numpy.mean(yLst), numpy.mean(zLst) )
#result = numpy.mean(vectorList)
arr = numpy.array(lst).reshape(len(lst), 3)
result = numpy.mean(arr, axis=0)
return result
def middleVector(lst):
xLst = [x[0] for x in lst]
yLst = [y[1] for y in lst]
zLst = [z[2] for z in lst]
xMinMax = [min(xLst), max(xLst)]
yMinMax = [min(yLst), max(yLst)]
zMinMax = [min(zLst), max(zLst)]
result = ( average( xMinMax ), average( yMinMax ), average( zMinMax ) )
return result
# -----------------------------------------------------------------------------
# Determine which mode is currently Selected (Vert, Edge, Face, etc)
# Returned: (0=Multiple modes, 1=Vertice Mode, 2=Edge Mode, 3=Face Mode)
# -----------------------------------------------------------------------------
def getCurrentSelectMode(self, context):
#Create empty list
tempList = []
#check current mesh select mode
for bool in bpy.context.tool_settings.mesh_select_mode:
tempList.append(bool)
#convert list into a tuple
tempTuple = tuple(tempList)
currentSelectMode = int()
if tempTuple == (True, False, False):
currentSelectMode = 1
elif tempTuple == (False, True, False):
currentSelectMode = 2
elif tempTuple == (False, False, True):
currentSelectMode = 3
else:
pass #(defaults currentSelectMode to 0)
return currentSelectMode
# END getCurrentSelectMode(self, context)
#Reset Operators to their default values:
def resetOperatorProps(self, context, propsToReset):
# EXAMPLE OF propsToReset - ADD THIS TO THE TOP OF THE execute() IN YOUR OPERATOR AND CUSTOMIZE
#
# propsToReset = [
# ["reset_allVisibilityModes", ["render", "realtime", "editmode", "cage"] ],
# ["reset_affect", ["affect"] ],
# ]
for item in propsToReset:
resetBoolPropName = item[0]
resetBool = getattr(self, resetBoolPropName)
if resetBool == True:
for propName in item[1]:
defaultVal = self.__annotations__[propName][1]['default']
setattr(self, propName, defaultVal)
setattr(self, resetBoolPropName, False)
def getSelObjs(self, context):
return bpy.context.selected_objects
def getScnObjs(self, context):
return context.scene.objects
def getUnselObjs(self, context):
allObjs = context.scene.objects
selObjs = context.selected_objects
return [obj for obj in allObjs if obj not in selObjs] #unselected objs
def findModifier(self, context, obj, modifierName):
foundModifier = None #declare
for modifier in obj.modifiers:
if modifier.name == modifierName:
foundModifier = modifier
break
return foundModifier
def findVertexGroup(self, context, obj, vertexGroupName):
foundVG = None #declare
for vg in obj.vertex_groups:
if vg.name == vertexGroupName:
foundVG = vg
break
return foundVG
def bboxFromSelection():
#returns the bounding box of a selection. Special thanks and Source: iceythe
all_vcos = []
# Get all vert cos from objects in edit mode
for o in bpy.context.objects_in_mode_unique_data:
bm = bmesh.from_edit_mesh(o.data)
mat = o.matrix_world
all_vcos.extend([mat @ v.co for v in bm.verts if v.select])
(x1, y1, z1,
x2, y2, z2) = [func(all_vcos, key=itemgetter(i))[i]
for func in (min, max) for i in range(3)]
bbox = (
(x1, y1, z1), (x1, y1, z2),
(x2, y1, z2), (x2, y1, z1),
# mirror other size
(x1, y2, z1), (x1, y2, z2),
(x2, y2, z2), (x2, y2, z1))
bbox_vecs = [Vector(i) for i in bbox]
return bbox_vecs
def dimensionsFromSelection(self, context, minMaxAvg='AVG'):
#Get dimension from selection - Special thanks & source: iceythe
all_vcos = []
# Get all vert cos from objects in edit mode
for o in bpy.context.objects_in_mode_unique_data:
bm = bmesh.from_edit_mesh(o.data)
mat = o.matrix_world
all_vcos.extend([(mat @ v.co)[:] for v in bm.verts if v.select])
it = numpy.fromiter(chain.from_iterable(all_vcos), dtype=float)
it.shape = (len(all_vcos), 3)
_min, _max = Vector(it.min(0).tolist()), Vector(it.max(0).tolist())
if minMaxAvg == "MIN":
result = min((_max - _min))
elif minMaxAvg == "MAX":
result = max((_max - _min))
elif minMaxAvg == "AVG":
result = average((_max - _min))
return result
def activateProp(self, context, propName=''):
addonPrefs = bpy.context.preferences.addons[__package__].preferences
if not addonPrefs.preventInfiniteRecursion:
addonPrefs.preventInfiniteRecursion = True
prop = getattr(addonPrefs, propName, None)
prop_active = getattr(addonPrefs, f'{propName}_active', None)
if prop_active is not None:
setattr(addonPrefs, f'{propName}_active', True)
addonPrefs.preventInfiniteRecursion = False
def deactivateProp(self, context, propName=''):
addonPrefs = bpy.context.preferences.addons[__package__].preferences
if not addonPrefs.preventInfiniteRecursion:
addonPrefs.preventInfiniteRecursion = True
prop = getattr(addonPrefs, propName, None)
defaultVal = self.__annotations__[propName][1]['default']
if prop is not None:
setattr(addonPrefs, propName, defaultVal)
addonPrefs.preventInfiniteRecursion = False
def retreive_op_props_from_addonPrefs(self, context, addonPrefs=None, opPropNameList=None, opPropPrefix=None):
if self.bUseOverridesFromAddonPrefs:
for opPropName in opPropNameList:
#get the value of the addonPrefs property
addonPropActive = getattr(addonPrefs, f'{opPropPrefix}{opPropName}_active', None)
if addonPropActive is not None:
if addonPropActive:
addonPropVal = getattr(addonPrefs, f'{opPropPrefix}{opPropName}', None)
#set the value of the operator property to the value of the addonPrefs property
setattr(self, opPropName, addonPropVal)
# END retreive_op_props_from_addonPrefs()
def getOutliners(self, context):
outliners = [] #declare
for win in context.window_manager.windows:
for area in win.screen.areas:
if area.type == 'OUTLINER':
outliners.append({'window' : win, 'area': area})
return outliners
# END getOutliners()
def expand_selected_objs_in_outliner(self, context, selObjs=None, outliners=None, activeObj=None):
if activeObj is None:
activeObj = context.view_layer.objects.active
if selObjs is None:
selObjs = context.selected_objects
if outliners is None:
outliners = getOutliners(self, context)
for obj in selObjs:
context.view_layer.objects.active = obj #set object as active object
for outliner in outliners:
win = outliner['window']
area = outliner['area']
contextOverride = {'window': win, 'screen': win.screen, 'area': area}
bpy.ops.outliner.show_active(contextOverride)
if activeObj is not None:
context.view_layer.objects.active = activeObj #reset Active Object
# END expand_selected_objs_in_outliner()
def redraw_all_outliners(self, context, outliners=None):
if outliners is None:
outliners = getOutliners(self, context)
for outliner in outliners:
outliner['area'].tag_redraw() #redraw outliner
# END redraw_all_outliners()
def snapCursorToMedianPoint(self, context, activeObj=None, objMode=None, tformPivotPoint=None):
if activeObj is None:
activeObj = context.view_layer.objects.active
if objMode is None:
objMode is context.object.mode
if tformPivotPoint is None:
tformPivotPoint = f'{context.scene.tool_settings.transform_pivot_point}'
if activeObj.type == "MESH":
if objMode == "OBJECT":
if tformPivotPoint == "ACTIVE_ELEMENT":
bpy.ops.view3d.snap_cursor_to_active()
elif tformPivotPoint == "INDIVIDUAL_ORIGINS":
context.scene.tool_settings.transform_pivot_point = 'MEDIAN_POINT'
bpy.ops.view3d.snap_cursor_to_selected()
context.scene.tool_settings.transform_pivot_point = tformPivotPoint #reset
else:
bpy.ops.view3d.snap_cursor_to_selected()
else:
bpy.ops.view3d.snap_cursor_to_selected()
else:
if tformPivotPoint == "ACTIVE_ELEMENT":
bpy.ops.view3d.snap_cursor_to_active()
else:
bpy.ops.view3d.snap_cursor_to_selected()
# END snapCursorToMedianPoint()
def snapCursorToBoundingBoxCenter(self, context, activeObj=None, objMode=None, tformPivotPoint=None, numObjsWithVertsSel=None):
if activeObj is None:
activeObj = context.view_layer.objects.active
if objMode is None:
objMode = context.object.mode
if tformPivotPoint is None:
tformPivotPoint = f'{context.scene.tool_settings.transform_pivot_point}'
if activeObj.type == "MESH":
if objMode == "OBJECT":
if tformPivotPoint == "ACTIVE_ELEMENT":
bpy.ops.view3d.snap_cursor_to_active()
else:
bpy.ops.view3d.snap_cursor_to_selected()
else:
if numObjsWithVertsSel is not None:
if numObjsWithVertsSel == 1:
bm = bmesh.from_edit_mesh(activeObj.data)
mat = activeObj.matrix_world.copy()
vco = [v.co for v in bm.verts if v.select]
arr = np.array(vco).reshape(len(vco), 3)
center = mat @ Vector((arr.min(axis=0) + arr.max(axis=0)) * 0.5)
context.scene.cursor.location = center
if numObjsWithVertsSel >= 2:
bpy.ops.view3d.snap_cursor_to_selected() #sloppy fallback
else:
if tformPivotPoint == "ACTIVE_ELEMENT":
bpy.ops.view3d.snap_cursor_to_active()
else:
bpy.ops.view3d.snap_cursor_to_selected()
# END snapCursorToBoundingBoxCenter()
def max_dim_from_objs_or_empties(self, context, minMaxAvg='AVG', objs=None):
#Get max dimension from objects and empties - Special thanks & source: iceythe
all_vcos = []
for o in objs:
mat_w = o.matrix_world
if o.type == 'EMPTY':
size = o.empty_display_size
all_vcos.extend([mat_w @ Vector(point) for point in
permutations((-size, size) * 2, 3)])
continue
all_vcos.extend([mat_w @ Vector(point[:])
for point in o.bound_box[:]])
it = numpy.fromiter(chain.from_iterable((all_vcos)), dtype=float)
it.shape = (len(all_vcos), 3)
_min, _max = Vector(it.min(0).tolist()), Vector(it.max(0).tolist())
if minMaxAvg == "MIN":
result = min((_max - _min))
elif minMaxAvg == "MAX":
result = max((_max - _min))
elif minMaxAvg == "AVG":
result = average((_max - _min))
#prevent zero result
if result == 0:
result = 1
return result
# END max_dim_from_objs_or_empties()