-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
842 lines (667 loc) · 21.9 KB
/
utils.py
File metadata and controls
842 lines (667 loc) · 21.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import bpy
import bmesh
import numpy as np
import mathutils as mu
LONG = True
LONG = False
if LONG:
D_TYPE_F = np.float64
D_TYPE_I = np.int64
else:
D_TYPE_F = np.float32
D_TYPE_I = np.int32
#=======================#
# WEB ------------------#
#=======================#
# web
def open_browser(link=None):
import webbrowser
if link == "paypal":
# subscribe with paypal:
link = "https://www.paypal.com/webapps/billing/plans/subscribe?plan_id=P-8V2845643T4460310MYYRCZA"
if link == "gumroad":
# subscribe with gumroad:
link = "https://richcolburn.gumroad.com/l/wooww"
if link == "patreon":
# subscribe with patreon:
link = "https://www.patreon.com/checkout/TheologicalDarkWeb?rid=23272750"
if link == "donate":
# paypal donate:
link = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4T4WNFQXGS99A"
webbrowser.open(link)
#=======================#
# DEBUG ----------------#
#=======================#
# debug
def popup_error(msg):
def oops(self, context):
return
bpy.context.window_manager.popup_menu(oops, title=msg, icon='ERROR')
#=======================#
# PYTHON ---------------#
#=======================#
# python
def flatten_list(li, depth=1):
"""Takes an Nd list and returns it flat"""
for _ in range(depth):
li = [i for j in li for i in j]
return li
# python
def r_print(r, m=''):
"""Prints numpy arrays
and lists rounded off."""
print()
print(m + " --------")
if isinstance(r, list):
for v in r:
print(np.round(v, 3), m)
return
print(np.round(r, 3), m)
#=======================#
# VERTEX GROUPS --------#
#=======================#
# vertex groups
def assign_vert_group(ob, v_list, name, weight=1.0):
"""Does what you might imagine
(makes me a sandwich)."""
if name not in ob.vertex_groups:
nvg = ob.vertex_groups.new(name=name)
else:
nvg = ob.vertex_groups[name]
nvg.add(v_list, weight, 'REPLACE')
# vertex groups
def get_weight(ob, v, group):
"""Try to get single weight. If vert not in group
retun 0.0"""
vertex_group = ob.vertex_groups[group]
try:
weight = vertex_group.weight(v)
except:
return 0.0
return weight
# vertex groups
def get_weights(ob, verts, group):
"""Try to get list of weights. If vert not in group
set that vert to 0.0"""
vertex_group = ob.vertex_groups[group]
weights = np.zeros(len(verts), dtype=np.float32)
for e, v in enumerate(verts):
try:
weights[e] = vertex_group.weight(v)
except:
weights[e] = 0.0
return weights
# vertex groups
def get_vertex_weights_overwrite(ob, group_name, default=1.0):
"""Get weights of the group. if it's not
in the group set the weight of that
vertex to default"""
if group_name not in ob.vertex_groups:
ob.vertex_groups.new(name=group_name)
vertex_group = ob.vertex_groups[group_name]
all_vertices = range(len(ob.data.vertices))
weights = []
for idx in all_vertices:
try:
weight = vertex_group.weight(idx)
weights.append(weight)
except RuntimeError:
weights.append(default)
vertex_weights = np.array(weights, dtype=np.float32)
return vertex_weights
# vertex groups
def vertex_groups_to_dict(ob, verts=None, offset=None):
"""Create a dictionary of groups for every vert
or a list of specific verts if "vert" is
an array or list of numbers.
"offset" is for breaking verts out into a new object"""
vgroup_names = {vgroup.index: vgroup.name for vgroup in ob.vertex_groups}
if verts is None:
vgroups = {v.index: [vgroup_names[g.group] for g in v.groups] for v in ob.data.vertices}
return vgroups
if offset is None:
vgroups = {
ob.data.vertices[v].index: [vgroup_names[g.group] for g in ob.data.vertices[v].groups] for v in verts
}
return vgroups
vgroups = {
ob.data.vertices[v].index - offset[v]: [vgroup_names[g.group] for g in ob.data.vertices[v].groups]
for v in verts
}
return vgroups
#=======================#
# BMESH ----------------#
#=======================#
# bmesh
def get_bmesh(ob=None, refresh=False, mesh=None, copy=False):
"""gets bmesh in editmode or object mode
by checking the mode"""
if ob.data.is_editmode:
obm = bmesh.from_edit_mesh(ob.data)
return obm
obm = bmesh.new()
m = ob.data
if mesh is not None:
m = mesh
obm.from_mesh(m)
if refresh:
obm.verts.ensure_lookup_table()
obm.edges.ensure_lookup_table()
obm.faces.ensure_lookup_table()
if copy:
return obm.copy()
return obm
# bmesh
def nearest_points_on_bmesh(coords, obm, return_all=False):
"""Uses blender closest_point... with
a bmesh."""
bvh = mu.bvhtree.BVHTree.FromBMesh(obm)
faces = []
locs = []
norms = []
dists = []
for co in coords:
loc, norm, face_index, dist = bvh.find_nearest(co)
faces += [face_index]
locs += [loc]
if return_all:
norms += [norm]
dists += [dist]
if return_all:
return faces, locs, norms, dists
return faces, locs
# bmesh
def get_tridex(ob, teidx=False, tmesh=False, tobm=None, refresh=False, cull_verts=None, free=False):
"""Return an index for viewing the
verts as triangles using a mesh and
foreach_get.
Return tridex and triobm"""
if tobm is None:
tobm = get_bmesh(ob)
if cull_verts is not None:
tobm.verts.ensure_lookup_table()
cv = [tobm.verts[v] for v in cull_verts]
bmesh.ops.delete(tobm, geom=cv, context='VERTS')
bmesh.ops.triangulate(tobm, faces=tobm.faces)
me = bpy.data.meshes.new('tris')
tobm.to_mesh(me)
p_count = len(me.polygons)
tridex = np.empty((p_count, 3), dtype=D_TYPE_I)
me.polygons.foreach_get('vertices', tridex.ravel())
three_edges = np.empty((len(tobm.faces), 3), dtype=D_TYPE_I)
for e, f in enumerate(tobm.faces):
eid = [ed.index for ed in f.edges]
three_edges[e] = eid
if free:
edge_normal_keys = []
for e in tobm.edges:
if len(e.link_faces) == 2:
e_key = [e.link_faces[0].index, e.link_faces[1].index]
elif len(e.link_faces) == 1:
e_key = [e.link_faces[0].index, e.link_faces[0].index]
else:
e_key = [-1, -1]
edge_normal_keys += [e_key]
eidx = np.array(me.edge_keys, dtype=D_TYPE_I)
bpy.data.meshes.remove(me)
tobm.free()
return tridex, eidx, three_edges, np.array(edge_normal_keys, dtype=D_TYPE_I)
if tmesh:
return tridex, tobm, me
if teidx:
eidx = np.array(me.edge_keys, dtype=D_TYPE_I)
# clear unused tri mesh
bpy.data.meshes.remove(me)
return tridex, eidx, tobm
# clear unused tri mesh
bpy.data.meshes.remove(me)
if refresh:
tobm.verts.ensure_lookup_table()
tobm.edges.ensure_lookup_table()
tobm.faces.ensure_lookup_table()
return tridex, tobm, None
# bmesh
def get_tridex_2(ob, mesh=None): # faster than get_tridex()
"""Return an index for viewing the
verts as triangles using a mesh and
foreach_get. Faster than get_tridex()"""
if mesh is not None:
tobm = bmesh.new()
tobm.from_mesh(mesh)
bmesh.ops.triangulate(tobm, faces=tobm.faces)
me = bpy.data.meshes.new('tris')
tobm.to_mesh(me)
p_count = len(me.polygons)
tridex = np.empty((p_count, 3), dtype=np.int32)
me.polygons.foreach_get('vertices', tridex.ravel())
# clear unused tri mesh
bpy.data.meshes.remove(me)
if ob == 'p':
return tridex, tobm
tobm.free()
return tridex
if ob.data.is_editmode:
ob.update_from_editmode()
tobm = bmesh.new()
tobm.from_mesh(ob.data)
bmesh.ops.triangulate(tobm, faces=tobm.faces[:])
me = bpy.data.meshes.new('tris')
tobm.to_mesh(me)
p_count = len(me.polygons)
tridex = np.empty((p_count, 3), dtype=np.int32)
me.polygons.foreach_get('vertices', tridex.ravel())
# clear unused tri mesh
bpy.data.meshes.remove(me)
return tridex, tobm
#=======================#
# GEOMETRY -------------#
#=======================#
# geometry
def get_tridex_with_edges(ob, teidx=False, tmesh=False, tobm=None, refresh=False, cull_verts=None):
"""Return an index for viewing the
verts as triangles using a mesh and
foreach_get.
Return tridex and triobm"""
if tobm is None:
tobm = get_bmesh(ob)
if cull_verts is not None:
tobm.verts.ensure_lookup_table()
cv = [tobm.verts[v] for v in cull_verts]
bmesh.ops.delete(tobm, geom=cv, context='VERTS')
bmesh.ops.triangulate(tobm, faces=tobm.faces)
me = bpy.data.meshes.new('tris')
tobm.to_mesh(me)
p_count = len(me.polygons)
tridex = np.empty((p_count, 3), dtype=D_TYPE_I)
me.polygons.foreach_get('vertices', tridex.ravel())
if tmesh:
return tridex, tobm, me
if teidx:
eidx = np.array(me.edge_keys, dtype=D_TYPE_I)
# clear unused tri mesh
bpy.data.meshes.remove(me)
return tridex, eidx, tobm
# clear unused tri mesh
bpy.data.meshes.remove(me)
if refresh:
tobm.verts.ensure_lookup_table()
tobm.edges.ensure_lookup_table()
tobm.faces.ensure_lookup_table()
return tridex, tobm, None
# geometry
def inside_triangles(tris, points, margin=0.0): # , cross_vecs):
"""Checks if points are inside triangles"""
origins = tris[:, 0]
cross_vecs = tris[:, 1:] - origins[:, None]
v2 = points - origins
# ---------
v0 = cross_vecs[:, 0]
v1 = cross_vecs[:, 1]
d00_d11 = np.einsum('ijk,ijk->ij', cross_vecs, cross_vecs)
d00 = d00_d11[:, 0]
d11 = d00_d11[:, 1]
d01 = np.einsum('ij,ij->i', v0, v1)
d02 = np.einsum('ij,ij->i', v0, v2)
d12 = np.einsum('ij,ij->i', v1, v2)
div = 1 / (d00 * d11 - d01 * d01)
u = (d11 * d02 - d01 * d12) * div
v = (d00 * d12 - d01 * d02) * div
w = 1 - (u + v)
# !!!! needs some thought
# margin = 0.0
# !!!! ==================
weights = np.array([w, u, v]).T
# check = (u >= margin) & (v >= margin) & (w >= margin)
check = (u >= margin) & (v >= margin) & (w >= margin)
# check = ~np.any(weights > 1.0, axis=1)
return check, weights
# geometry
def closest_point_edges(vecs, origins, p):
'''Returns the location of the point on the edges'''
vec2 = p - origins
d = np.einsum('ij,ij->i', vecs, vec2) / np.einsum('ij,ij->i', vecs, vecs)
cp = origins + vecs * d[:, None]
return cp, d
# geometry
def np_closest_point_mesh(co, ob=None, t_data=None, return_weights=False):
"""Returns closest point on mesh
and type v, f, or e depending
on if the closest place was
on a vert, a face, or an edge."""
face = False
edge = False
edge_weight = None
tri_weight = None
# faces
if t_data is None:
tridex, teidx, tobm = get_tridex_with_edges(ob, teidx=True, tmesh=False)
else:
tridex, teidx, tobm = t_data['tridex'], t_data['teidx'], t_data['tobm']
if ob is None:
obco = np.array([v.co for v in tobm.verts], dtype=D_TYPE_F)
else:
obco = get_co(ob)
tris = obco[tridex]
check, weights = inside_triangles(tris, co, margin=0.0)
checked_weights = weights[check]
if np.any(check):
face = True
tidx = np.arange(tris.shape[0])[check]
weight_plot = tris[check] * weights[check][:, :, None]
locs = np.sum(weight_plot, axis=1)
dif = co - locs
dots = np.einsum('ij,ij->i', dif, dif)
f_min = np.argmin(dots)
f_loc = locs[f_min]
tid = tidx[f_min]
tri_weight = checked_weights[f_min]
# edges
teco = obco[teidx]
vecs = teco[:, 1] - teco[:, 0]
cpoes, dots = closest_point_edges(vecs, teco[:, 0], co)
check = (dots > 0.0) & (dots < 1.0)
if np.any(check):
edge = True
check_dots = dots[check]
eidc = np.arange(check.shape[0])[check]
checked = cpoes[check]
dif = co - checked
dots = np.einsum('ij,ij->i', dif, dif)
emin = np.argmin(dots)
e_loc = checked[emin]
eid = eidc[emin]
edge_weight = check_dots[emin]
# verts
dif = co - obco
dots = np.einsum('ij,ij->i', dif, dif)
vmin = np.argmin(dots)
v_loc = obco[vmin]
trifecta = [v_loc]
type = ['v']
ind = [vmin]
tobm.verts.ensure_lookup_table()
tobm.verts[vmin].normal
if face:
trifecta += [f_loc]
type += ['f']
ind += [tid]
if edge:
trifecta += [e_loc]
type += ['e']
ind += [eid]
npl = np.array(trifecta, dtype=D_TYPE_F)
dif = co - npl
dots = np.einsum('ij,ij->i', dif, dif)
amin = np.argmin(dots)
loc = npl[amin] # and all the chistians said "amin."
t = type[amin]
index = np.array(ind, dtype=D_TYPE_I)[amin]
# location, type, index
if return_weights:
return loc, t, index, tri_weight, tridex, teidx, edge_weight
return loc, t, index
# geometry
def closest_face_normal(co, t_data=None):
"""Manages v f and e types to get
normal from np_closest_point_mesh()"""
location, type, index = np_closest_point_mesh(co, t_data=t_data)
obm_ex = t_data["tobm"]
if type == 'f':
return np.array(obm_ex.faces[index].normal, dtype=D_TYPE_F)
if type == 'v':
return np.array(obm_ex.verts[index].normal, dtype=D_TYPE_F)
if type == 'e':
eco = np.array([f.normal for f in obm_ex.edges[index].link_faces], dtype=D_TYPE_F)
return np.mean(eco, axis=0)
# geometry
def u_vecs(vecs, dist=False):
length = np.sqrt(np.einsum('ij,ij->i', vecs, vecs))
u_v = vecs / length[:, None]
if dist:
return u_v, length
return u_v
# geometry
def compare_vecs(v1, v2):
return np.einsum('ij,ij->i', v1, v2)
# geometry
def measure_vecs(vecs, out=None, out2=None):
return np.sqrt(np.einsum('ij,ij->i', vecs, vecs, out=out), out=out2)
# geometry
def fastest_cross_product(a, b, c=None):
if c is None:
c = np.empty_like(a)
c[:, 0], c[:, 1], c[:, 2] = (
a[:, 1] * b[:, 2] - a[:, 2] * b[:, 1],
a[:, 2] * b[:, 0] - a[:, 0] * b[:, 2],
a[:, 0] * b[:, 1] - a[:, 1] * b[:, 0]
)
return c
# geometry
def matrix_from_verts(verts):
"""Takes N sets of 3 coordinates,
generates a 3x3 matrix using c1 - c0
as X then the normal as Z.
Y is the cross of X and the normal ("Obviously," he said with a condescending sigh)"""
count = verts.shape[0]
M = np.empty((count, 3, 3), dtype=np.float32)
x = verts[:, 1] - verts[:, 0]
vec2 = verts[:, 2] - verts[:, 0]
cross = fastest_cross_product(x, vec2)
normal = u_vecs(cross)
ux = u_vecs(x)
y = u_vecs(fastest_cross_product(normal, ux))
M[:, 0] = ux
M[:, 1] = y
M[:, 2] = normal
return M
# geometry
def match_pairs(trico1, trico2, tco1, tco2):
"""Takes two sets of triangle Coords
(left and right pairs of triangles)
and rotates them so they are planar
mirrors of each other aligned on
the first edge."""
M1 = matrix_from_verts(trico1)
M2 = matrix_from_verts(trico2)
NR1a = trico1 @ np.transpose(M1, axes=(0, 2, 1))
NR1 = tco1 @ np.transpose(M1, axes=(0, 2, 1))
NR2a = trico2 @ np.transpose(M2, axes=(0, 2, 1))
NR2 = tco2 @ np.transpose(M2, axes=(0, 2, 1))
eye = np.eye(3)
eye[1] *= -1
eyes = np.zeros((tco1.shape[0], 3, 3), dtype=np.float32)
eyes[:] = eye
NR3 = NR2 @ eyes
NR3a = NR2a @ eyes
mids1 = NR1a[:, :2]
vec = mids1[:, 1] - mids1[:, 0]
new1 = mids1[:, 0] + vec * 0.5
mids2 = NR3a[:, :2]
vec2 = mids2[:, 1] - mids2[:, 0]
new2 = mids2[:, 0] + vec2 * 0.5
mid_dif = ((new2 - new1) * 0.5)[:, None]
NR1 += mid_dif
NR3 -= mid_dif
return NR1, NR3
def testing_matrix_from_verts():
ob = bpy.context.object
tridex = np.empty((len(ob.data.polygons), 3), dtype=np.int32)
ob.data.polygons.foreach_get('vertices', tridex.ravel())
co = np.empty((len(ob.data.vertices), 3), dtype=np.float32)
ob.data.vertices.foreach_get('co', co.ravel())
tridex[0] = [1,2,0]
tridex[1] = [3,4,5]
tridex[2] = [9,10,11]
tridex[3] = [7,8,6]
trico = co[tridex]
#if False:
if True:
co1, co2 = match_pairs(trico[::2], trico[1::2])
print(co1.shape)
print(trico[::2].shape)
co[tridex[::2]] = co1
co[tridex[1::2]] = co2
ob.data.vertices.foreach_set('co', co.ravel())
ob.data.update()
return
M = matrix_from_verts(trico)
#print(M)
#print(M.shape)
#M[1] *= np.array([1, -1, 1.0])
#print(M)
#print(M[1] @ M[1].T)
meh = co[tridex[1]] @ M[1].T
print()
co[tridex[1]] = meh
eye = np.eye(3)
eye[1] *= -1
#print(co[tridex[1]])
#print(eye)
mehh = co[tridex[1]] @ eye
co[tridex[1]] = mehh
meh2 = co[tridex[0]] @ M[0].T
co[tridex[0]] = meh2
ob.data.vertices.foreach_set('co', co.ravel())
ob.data.update()
#testing_matrix_from_verts()
#=======================#
# GET/SET --------------#
#=======================#
# universal ---------------
def get_proxy_co(ob, co=None, proxy=None, return_proxy=False):
"""Gets co with modifiers like cloth"""
if proxy is None:
dg = bpy.context.evaluated_depsgraph_get()
prox = ob.evaluated_get(dg)
proxy = prox.to_mesh()
if co is None:
vc = len(proxy.vertices)
co = np.empty((vc, 3), dtype=np.float32)
proxy.vertices.foreach_get('co', co.ravel())
if return_proxy:
return co, proxy, prox
ob.to_mesh_clear()
return co
# get/set
def absolute_co(ob, co=None):
"""Get proxy vert coords in world space"""
co, proxy, prox = get_proxy_co(ob, co, return_proxy=True)
m = np.array(ob.matrix_world, dtype=np.float32)
mat = m[:3, :3].T # rotates backwards without T
loc = m[:3, 3]
return co @ mat + loc, proxy, prox
# get/set
def get_co(ob):
"""Returns Nx3 cooridnate set as numpy array"""
co = np.empty((len(ob.data.vertices), 3), dtype=D_TYPE_F)
ob.data.vertices.foreach_get("co", co.ravel())
return co
# get/set
def get_shape_co(ob, shape='Key_1', co=None):
if co is None:
co = np.empty((len(ob.data.vertices), 3), dtype=D_TYPE_F)
ob.data.shape_keys.key_blocks[shape].data.foreach_get('co', co.ravel())
return co
# get/set
def local_co(ob, obs):
"""Currently using deformed co to return coords
of objects in obs in the local space of ob."""
OBCOS = []
WMDS = []
for sob in obs:
OBCOS += [deform_co(sob)]
WMDS += [sob.matrix_world] # why monsters don't sweat
mesh_matrix = ob.matrix_world
local_matrix = np.linalg.inv(mesh_matrix) @ np.array(WMDS, dtype=np.float32)
local_co = np.empty((0, 3), dtype=np.float32)
for i in range(len(OBCOS)):
ob_space = OBCOS[i] @ local_matrix[i][:3, :3].T
ob_space += local_matrix[i][:3, 3]
local_co = np.concatenate((local_co, ob_space), axis=0)
return local_co
# get/set
def deform_co(ob, return_prox=False, triangulate=False, return_tobm=False):
"""Uses a list of mods that only deform,
creates a proxy turning of every other
mod then returns the proxy coords."""
deform_mods = ['ARMATURE',
'CAST',
'CURVE',
'DISPLACE',
'HOOK',
'LAPLACIANDEFORM',
'LATTICE',
'MESH_DEFORM',
'SHRINKWRAP',
'SIMPLE_DEFORM',
'SMOOTH',
'CORRECTIVE_SMOOTH',
'LAPLACIANSMOOTH',
'SURFACE_DEFORM',
'WARP',
'WAVE',
'VOLUME_DISPLACE',
'CLOTH',
'SOFT_BODY',
'VERTEX_WEIGHT_EDIT',
'VERTEX_WEIGHT_MIX',
'VERTEX_WEIGHT_PROXIMITY',
]
display = [m.show_viewport for m in ob.modifiers]
for m in ob.modifiers:
if m.type not in deform_mods:
m.show_viewport = False
prox = prox_object(ob)#, triangulate=True)
if triangulate:
tobm = get_bmesh(prox, refresh=True)
bmesh.ops.triangulate(tobm, faces=tobm.faces)
co = get_co(prox)
for e, m in enumerate(ob.modifiers):
m.show_viewport = display[e]
if return_prox:
return co, prox
if return_tobm:
return co, tobm
return co
#=======================#
# BLENDER OBJECTS ------#
#=======================#
# blender objects
def prox_object(ob):
"""Returns object including modifier and
shape key effects."""
dg = bpy.context.evaluated_depsgraph_get()
return ob.evaluated_get(dg)
# notes and such ----------
# list of mods other than deforming mods
('DATA_TRANSFER',
'MESH_CACHE',
'MESH_SEQUENCE_CACHE',
'NORMAL_EDIT',
'WEIGHTED_NORMAL',
'UV_PROJECT',
'UV_WARP',
'GREASE_PENCIL_COLOR',
'GREASE_PENCIL_TINT',
'GREASE_PENCIL_OPACITY',
'ARRAY',
'BEVEL',
'BOOLEAN',
'BUILD',
'DECIMATE',
'EDGE_SPLIT',
'NODES', 'MASK', 'MIRROR',
'MESH_TO_VOLUME',
'MULTIRES', 'REMESH',
'SCREW', 'SKIN',
'SOLIDIFY',
'SUBSURF',
'TRIANGULATE',
'VOLUME_TO_MESH',
'WELD', 'WIREFRAME',
'GREASE_PENCIL_SUBDIV',
'GREASE_PENCIL_MIRROR',
'GREASE_PENCIL_NOISE', 'GREASE_PENCIL_OFFSET', 'GREASE_PENCIL_SMOOTH', 'GREASE_PENCIL_THICKNESS',
'COLLISION', 'DYNAMIC_PAINT', 'EXPLODE', 'FLUID', 'OCEAN', 'PARTICLE_INSTANCE',
'PARTICLE_SYSTEM',
'SURFACE')