forked from bnpr/Abnormal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_modal.py
2160 lines (1590 loc) · 73.3 KB
/
functions_modal.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
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
from mathutils import Vector, Matrix
from mathutils.bvhtree import BVHTree
from mathutils.geometry import intersect_line_plane, intersect_point_tri_2d
import numpy as np
import os
from bpy_extras import view3d_utils
from .functions_general import *
from .functions_drawing import *
from .functions_modal_keymap import *
from .classes import *
from .keymap import addon_keymaps
def match_loops_vecs(source_vecs, target_vecs, target_inds):
#
# Match the list of vectors with the target list
# matches by smallest angle
# Source vecs should be a 2d array Nx3 of just 3d vectors
# Target vecs should be a 3d array NxIx3 where it is a list of lists of 3d vectors for the source vec to be tested against
# Each I of the NxIx3 array is a list of vectors to test against the N vectors of source
#
# Get the dot product from the matched cos tangents to the original loops tangent
# and filter out -1 loops from the matched co
dots = np.sum(source_vecs[:, np.newaxis] * target_vecs, axis=2)
dots *= -1
dots[target_inds < 0] = np.nan
sort = np.argsort(dots)[:, 0]
indeces = np.arange(sort.size)
return target_inds[indeces, sort]
#
#
def set_new_normals(modal):
modal._object.data.edges.foreach_set(
'use_edge_sharp', modal._container.og_sharp)
# Lerp between cached and new normals by the filter weights
if modal._container.filter_mask.any():
modal._container.new_norms[:] = modal._container.cache_norms * (
1.0-modal._container.filter_weights[:, None]) + modal._container.new_norms * modal._container.filter_weights[:, None]
# Get the scale factor to normalized new normals
scale = 1 / np.sqrt(np.sum(np.square(modal._container.new_norms), axis=1))
modal._container.new_norms = modal._container.new_norms*scale[:, None]
if modal._mirror_x:
sel_norms = modal._container.new_norms[modal._container.sel_status]
sel_norms[:, 0] *= -1
modal._container.new_norms[modal.mir_loops_x[modal._container.sel_status]] = sel_norms
if modal._mirror_y:
sel_norms = modal._container.new_norms[modal._container.sel_status]
sel_norms[:, 1] *= -1
modal._container.new_norms[modal.mir_loops_y[modal._container.sel_status]] = sel_norms
if modal._mirror_z:
sel_norms = modal._container.new_norms[modal._container.sel_status]
sel_norms[:, 2] *= -1
modal._container.new_norms[modal.mir_loops_z[modal._container.sel_status]] = sel_norms
# modal._container.new_norms.shape = [len(modal._object.data.loops), 3]
modal._object.data.normals_split_custom_set(modal._container.new_norms)
modal.redraw = True
return
def mirror_normals(modal, axis):
sel_norms = modal._container.new_norms[modal._container.sel_status]
sel_norms[:, axis] *= -1
if axis == 0:
modal._container.new_norms[modal.mir_loops_x[modal._container.sel_status]] = sel_norms
if axis == 1:
modal._container.new_norms[modal.mir_loops_y[modal._container.sel_status]] = sel_norms
if axis == 2:
modal._container.new_norms[modal.mir_loops_z[modal._container.sel_status]] = sel_norms
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def incremental_rotate_vectors(modal, axis, direction):
modal.translate_mode = 2
modal.translate_axis = axis
modal._container.cache_norms[:] = modal._container.new_norms
rotate_vectors(modal, math.radians(direction * modal._rot_increment))
modal.translate_mode = 0
modal.translate_axis = 2
modal.redraw = True
return
def rotate_vectors(modal, angle):
if modal.translate_axis == 0:
axis = 'X'
if modal.translate_axis == 1:
axis = 'Y'
if modal.translate_axis == 2:
axis = 'Z'
rot = np.array(Matrix.Rotation(angle, 3, axis))
# Viewspace rotation matrix
if modal.translate_mode == 0:
persp_mat = bpy.context.region_data.view_matrix.to_3x3().normalized()
ob_mat = modal._object.matrix_world.to_3x3().normalized()
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat) @ modal._container.cache_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
persp_mat) @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (
rot @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
persp_mat.inverted()) @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat.inverted()) @ modal._container.new_norms[modal._container.sel_status].T).T
# World space rotation matrix
elif modal.translate_mode == 1:
ob_mat = modal._object.matrix_world.to_3x3().normalized()
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat) @ modal._container.cache_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (
rot @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat.inverted()) @ modal._container.new_norms[modal._container.sel_status].T).T
# Local space roatation matrix
elif modal.translate_mode == 2:
if modal.gizmo_click:
orb_mat = modal._orbit_ob.matrix_world.to_3x3().normalized()
ob_mat = modal._object.matrix_world.to_3x3().normalized()
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat) @ modal._container.cache_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
orb_mat.inverted()) @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (
rot @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (
np.array(orb_mat) @ modal._container.new_norms[modal._container.sel_status].T).T
modal._container.new_norms[modal._container.sel_status] = (np.array(
ob_mat.inverted()) @ modal._container.new_norms[modal._container.sel_status].T).T
else:
modal._container.new_norms[modal._container.sel_status] = (
rot @ modal._container.cache_norms[modal._container.sel_status].T).T
set_new_normals(modal)
return
#
# AXIS ALIGNMENT
#
def flatten_normals(modal, axis):
norms = modal._container.new_norms[modal._container.sel_status]
norms[:, axis] = 0.0
# Check for zero length vector after flattening
# If zero then set it back to 1.0 on flattened axis
zero_len = np.sum(np.absolute(norms), axis=1) == 0.0
# All vecs are zero length so no change occurs
if zero_len.all():
return
if zero_len.any():
norms[zero_len] = modal._container.new_norms[modal._container.sel_status][zero_len]
modal._container.new_norms[modal._container.sel_status] = norms
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def align_to_axis_normals(modal, axis, dir):
vec = [0, 0, 0]
vec[axis] = dir
modal._container.new_norms[modal._container.sel_status] = vec
set_new_normals(modal)
add_to_undostack(modal, 1)
return
#
# MANIPULATE NORMALS
#
def average_vertex_normals(modal):
sel_pos = get_selected_points(modal, any_selected=True)
sel_loops = modal._container.vert_link_ls[sel_pos]
loop_status = modal._container.sel_status[sel_loops]
loop_status[sel_loops < 0] = False
cur_norms = modal._container.new_norms[sel_loops]
cur_norms[~loop_status] = np.nan
cur_norms = np.nanmean(cur_norms, axis=1)[:, np.newaxis]
new_norms = modal._container.new_norms[sel_loops]
new_norms[:] = cur_norms
sel_loops = sel_loops[loop_status]
new_norms = new_norms[loop_status]
modal._container.new_norms[sel_loops] = new_norms
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def average_selected_normals(modal):
avg_norm = np.mean(
modal._container.new_norms[modal._container.sel_status], axis=0)
modal._container.new_norms[modal._container.sel_status] = avg_norm
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def smooth_normals(modal, fac):
sel_pos = get_selected_points(modal, any_selected=True)
conn_pos = modal._container.vert_link_vs[sel_pos]
sel_loops = modal._container.vert_link_ls[sel_pos]
sel_mask = sel_loops >= 0
for i in range(modal._smooth_iterations):
conn_norms = modal._container.new_norms[modal._container.vert_link_ls[conn_pos]]
conn_norms[modal._container.vert_link_ls[conn_pos] < 0] = np.nan
conn_norms[conn_pos < 0] = np.nan
conn_norms = np.nanmean(conn_norms, axis=(1, 2))[:, np.newaxis]
conn_norms = modal._container.new_norms[sel_loops] * (
1.0-fac*modal._smooth_strength) + conn_norms*(fac*modal._smooth_strength)
conn_norms = conn_norms[sel_mask]
modal._container.new_norms[sel_loops[sel_mask]] = conn_norms
set_new_normals(modal)
add_to_undostack(modal, 1)
return
#
# NORMAL DIRECTION
#
def flip_normals(modal):
modal._container.new_norms[modal._container.sel_status] *= -1
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def set_outside_inside(modal, direction):
if modal._object_smooth:
sel_pos = get_selected_points(modal, any_selected=True)
sel_loops = modal._container.vert_link_ls[sel_pos]
f_norms = modal._container.face_normals[modal._container.loop_faces[sel_loops]]
f_norms[sel_loops < 0] = np.nan
loop_status = modal._container.sel_status[sel_loops]
loop_status[sel_loops < 0] = False
f_norms[~loop_status] = np.nan
f_norms = np.nanmean(f_norms, axis=1)[:, np.newaxis]
new_norms = modal._container.new_norms[sel_loops]
new_norms[:] = f_norms
sel_loops = sel_loops[loop_status]
new_norms = new_norms[loop_status] * direction
modal._container.new_norms[sel_loops] = new_norms
else:
modal._container.new_norms[modal._container.sel_status] = modal._container.face_normals[
modal._container.loop_faces[modal._container.sel_status]]
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def reset_normals(modal):
modal._container.new_norms[modal._container.sel_status] = modal._container.og_norms[modal._container.sel_status]
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def set_normals_from_faces(modal):
# Get all faces that have all loops selected
sel_fs = modal._container.sel_status[modal._container.face_link_ls]
sel_fs[modal._container.face_link_ls < 0] = True
sel_fs = sel_fs.all(axis=1).nonzero()[0]
# Get the unique vertices of these faces in a flat array
face_vs = modal._container.face_link_vs[sel_fs].ravel()
face_vs = np.unique(face_vs[face_vs >= 0])
# Get the loops of each vert
vert_ls = modal._container.vert_link_ls[face_vs]
# Get the faces fand face normals of these loops
loop_fs = modal._container.loop_faces[vert_ls]
face_l_norms = modal._container.face_normals[loop_fs]
# Find which of these loops is valid based on if its face is apart of the fully selected faces
loop_status = np.in1d(loop_fs, sel_fs)
loop_status.shape = [face_l_norms.shape[0], face_l_norms.shape[1]]
loop_status[vert_ls < 0] = False
# Remove loop face normals for non valid loops and average per vertex
face_l_norms[~loop_status] = np.nan
face_l_norms = np.nanmean(face_l_norms, axis=1)
# Create array of vertex averaged normals for all of the loops connected to the verts
new_norms = modal._container.new_norms[vert_ls]
new_norms[:] = face_l_norms[:, np.newaxis]
# Filter out loops that should not be set
# Filter by verts if individual loops is off and by loops if it is on
if modal._individual_loops:
new_norms = new_norms[loop_status]
vert_ls = vert_ls[loop_status]
else:
new_norms = new_norms[vert_ls >= 0]
vert_ls = vert_ls[vert_ls >= 0]
modal._container.new_norms[vert_ls] = new_norms
set_new_normals(modal)
add_to_undostack(modal, 1)
return
#
# COPY/PASTE
#
def copy_active_to_selected(modal):
act_loops = modal._container.act_status.nonzero()[0]
# 1 active loop so paste it onto all selected
if act_loops.size == 1:
modal._container.new_norms[modal._container.sel_status] = modal._container.new_norms[act_loops[0]]
# Find active po and match the tangents of this po to the selected loops
else:
act_po = get_active_point(modal)
act_ls = modal._container.vert_link_ls[act_po]
act_ls = act_ls[act_ls >= 0]
sel_loops = modal._container.sel_status
sel_loops[act_ls] = False
match_pos = [act_po]*sel_loops.nonzero()[0].size
target_tans = modal._container.loop_tangents[modal._container.vert_link_ls[match_pos]]
loop_matches = match_loops_vecs(
modal._container.loop_tangents[sel_loops], target_tans, modal._container.vert_link_ls[match_pos])
modal._container.new_norms[sel_loops] = modal._container.new_norms[loop_matches]
set_new_normals(modal)
add_to_undostack(modal, 1)
return
def store_active_normal(modal):
act_loops = modal._container.act_status.nonzero()[0]
# 1 active loop so paste it onto all selected
if act_loops.size == 1:
modal._copy_normals = act_loops
# Find active po and match the tangents of this po to the selected loops
else:
act_po = get_active_point(modal)
act_ls = modal._container.vert_link_ls[act_po]
act_ls = act_ls[act_ls >= 0]
modal._copy_normals = act_ls
return
def paste_normal(modal):
if modal._copy_normals.size > 0:
# 1 active loop so paste it onto all selected
if modal._copy_normals.size == 1:
modal._container.new_norms[modal._container.sel_status] = modal._container.new_norms[modal._copy_normals[0]]
# Find active po and match the tangents of this po to the selected loops
else:
sel_loops = modal._container.sel_status
sel_loops[modal._copy_normals] = False
target_tans = np.tile(
modal._container.loop_tangents[modal._copy_normals], (sel_loops.nonzero()[0].size, 1, 1))
target_inds = np.tile(modal._copy_normals,
(sel_loops.nonzero()[0].size, 1))
loop_matches = match_loops_vecs(
modal._container.loop_tangents[sel_loops], target_tans, target_inds)
modal._container.new_norms[sel_loops] = modal._container.new_norms[loop_matches]
set_new_normals(modal)
add_to_undostack(modal, 1)
return
#
#
def translate_axis_draw(modal):
mat = None
if modal.translate_mode == 0:
modal.translate_draw_line.clear()
elif modal.translate_mode == 1:
mat = generate_matrix(Vector((0, 0, 0)), Vector(
(0, 0, 1)), Vector((0, 1, 0)), False, True)
mat.translation = modal._mode_cache[0]
elif modal.translate_mode == 2:
mat = modal._object.matrix_world.normalized()
mat.translation = modal._mode_cache[0]
if mat is not None:
modal.translate_draw_line.clear()
if modal.translate_axis == 0:
vec = Vector((1000, 0, 0))
if modal.translate_axis == 1:
vec = Vector((0, 1000, 0))
if modal.translate_axis == 2:
vec = Vector((0, 0, 1000))
modal.translate_draw_line.append(mat @ vec)
modal.translate_draw_line.append(mat @ -vec)
modal.batch_translate_line = batch_for_shader(
modal.shader_3d, 'LINES', {"pos": modal.translate_draw_line})
return
def clear_translate_axis_draw(modal):
modal.batch_translate_line = batch_for_shader(
modal.shader_3d, 'LINES', {"pos": []})
return
def translate_axis_change(modal, text, axis):
if modal.translate_axis != axis:
modal.translate_axis = axis
modal.translate_mode = 1
else:
modal.translate_mode += 1
if modal.translate_mode == 3:
modal.translate_mode = 0
modal.translate_axis = 2
if modal.translate_mode == 0:
modal._window.set_status('VIEW ' + text)
elif modal.translate_mode == 1:
modal._window.set_status('GLOBAL ' + text)
else:
modal._window.set_status('LOCAL ' + text)
translate_axis_draw(modal)
return
def translate_axis_side(modal):
view_vec = view3d_utils.region_2d_to_vector_3d(
modal.act_reg, modal.act_rv3d, modal._mouse_reg_loc)
if modal.translate_mode == 1:
mat = generate_matrix(Vector((0, 0, 0)), Vector(
(0, 0, 1)), Vector((0, 1, 0)), False, True)
else:
mat = modal._object.matrix_world.normalized()
pos_vec = Vector((0, 0, 0))
neg_vec = Vector((0, 0, 0))
pos_vec[modal.translate_axis] = 1.0
neg_vec[modal.translate_axis] = -1.0
pos_vec = (mat @ pos_vec) - mat.translation
neg_vec = (mat @ neg_vec) - mat.translation
if pos_vec.angle(view_vec) < neg_vec.angle(view_vec):
side = -1
else:
side = 1
# if modal.translate_axis == 1:
# side *= -1
return side
#
# MODAL
#
def cache_point_data(modal):
modal._object.data.calc_normals_split()
vert_amnt = len(modal._object.data.vertices)
edge_amnt = len(modal._object.data.edges)
loop_amnt = len(modal._object.data.loops)
face_amnt = len(modal._object.data.polygons)
modal._container.og_sharp = np.zeros(edge_amnt, dtype=bool)
modal._object.data.edges.foreach_get(
'use_edge_sharp', modal._container.og_sharp)
modal._container.og_seam = np.zeros(edge_amnt, dtype=bool)
modal._object.data.edges.foreach_get('use_seam', modal._container.og_seam)
modal._container.og_norms = np.zeros(loop_amnt*3, dtype=np.float32)
modal._object.data.loops.foreach_get('normal', modal._container.og_norms)
modal._container.og_norms.shape = [loop_amnt, 3]
modal._container.new_norms = modal._container.og_norms.copy()
modal._container.cache_norms = modal._container.og_norms.copy()
max_link_eds = max([len(v.link_edges) for v in modal._object_bm.verts])
max_link_loops = max([len(v.link_loops) for v in modal._object_bm.verts])
max_link_f_vs = max([len(f.verts) for f in modal._object_bm.faces])
max_link_f_loops = max([len(f.loops) for f in modal._object_bm.faces])
max_link_f_eds = max([len(f.edges) for f in modal._object_bm.faces])
#
link_vs = []
link_ls = []
link_fs = [None for i in range(loop_amnt)]
for v in modal._object_bm.verts:
l_v_inds = [-1] * max_link_eds
l_l_inds = [-1] * max_link_loops
for e, ed in enumerate(v.link_edges):
l_v_inds[e] = ed.other_vert(v).index
for l, loop in enumerate(v.link_loops):
l_l_inds[l] = loop.index
link_fs[loop.index] = loop.face.index
link_vs += l_v_inds
link_ls += l_l_inds
modal._container.loop_verts = np.zeros(loop_amnt, dtype=np.int32)
modal._object.data.loops.foreach_get(
'vertex_index', modal._container.loop_verts)
modal._container.loop_edges = np.zeros(loop_amnt, dtype=np.int32)
modal._object.data.loops.foreach_get(
'edge_index', modal._container.loop_edges)
modal._container.vert_link_vs = np.array(link_vs, dtype=np.int32)
modal._container.vert_link_vs.shape = [vert_amnt, max_link_eds]
modal._container.vert_link_ls = np.array(link_ls, dtype=np.int32)
modal._container.vert_link_ls.shape = [vert_amnt, max_link_loops]
modal._container.loop_faces = np.array(link_fs, dtype=np.int32)
modal._container.filter_weights = np.zeros(loop_amnt, dtype=np.float32)
modal._container.filter_mask = np.zeros(loop_amnt, dtype=bool)
#
link_f_vs = []
link_f_ls = []
link_f_eds = []
face_normals = []
l_tangents = [None for i in range(loop_amnt)]
for f in modal._object_bm.faces:
l_v_inds = [-1] * max_link_f_vs
l_l_inds = [-1] * max_link_f_loops
l_e_inds = [-1] * max_link_f_eds
for v, vert in enumerate(f.verts):
l_v_inds[v] = vert.index
for l, loop in enumerate(f.loops):
l_l_inds[l] = loop.index
l_tangents[loop.index] = loop.calc_tangent()
l_e_inds[l] = loop.edge.index
face_normals.append(f.normal)
link_f_vs += l_v_inds
link_f_ls += l_l_inds
link_f_eds += l_e_inds
modal._container.face_link_vs = np.array(link_f_vs, dtype=np.int32)
modal._container.face_link_vs.shape = [face_amnt, max_link_f_vs]
modal._container.face_link_ls = np.array(link_f_ls, dtype=np.int32)
modal._container.face_link_ls.shape = [face_amnt, max_link_f_loops]
modal._container.face_link_eds = np.array(link_f_eds, dtype=np.int32)
modal._container.face_link_eds.shape = [face_amnt, max_link_f_eds]
modal._container.loop_tangents = np.array(l_tangents, dtype=np.float32)
modal._container.loop_tangents.shape = [loop_amnt, 3]
modal._container.face_normals = np.array(face_normals, dtype=np.float32)
modal._container.face_normals.shape = [face_amnt, 3]
#
link_ed_f_inds = []
link_ed_fs = []
link_ed_vs = []
for ed in modal._object_bm.edges:
link_ed_vs.append([ed.verts[0].index, ed.verts[1].index])
for f in ed.link_faces:
link_ed_f_inds.append(ed.index)
link_ed_fs.append(f.index)
modal._container.edge_link_vs = np.array(link_ed_vs, dtype=np.int32)
modal._container.edge_link_vs.shape = [edge_amnt, 2]
modal._container.edge_link_f_inds = np.array(
link_ed_f_inds, dtype=np.int32)
modal._container.edge_link_fs = np.array(link_ed_fs, dtype=np.int32)
#
modal._container.po_coords = np.array(
[v.co for v in modal._object_bm.verts], dtype=np.float32)
modal._container.loop_coords = np.array(
[modal._object_bm.verts[l.vertex_index].co for l in modal._object.data.loops], dtype=np.float32)
loop_tri_cos = [[] for i in range(loop_amnt)]
for v in modal._object_bm.verts:
ed_inds = [ed.index for ed in v.link_edges]
for loop in v.link_loops:
loop_cos = [v.co+v.normal*.001]
for ed in loop.face.edges:
if ed.index in ed_inds:
ov = ed.other_vert(v)
vec = (ov.co+ov.normal*.001) - (v.co+v.normal*.001)
loop_cos.append((v.co+v.normal*.001) + vec * 0.5)
loop_tri_cos[loop.index] = loop_cos
modal._container.loop_tri_coords = np.array(loop_tri_cos, dtype=np.float32)
#
#
loop_sel = [False] * loop_amnt
loop_hide = [True] * loop_amnt
loop_act = [False] * loop_amnt
for v in modal._object_bm.verts:
for loop in v.link_loops:
# Vertex selection
if bpy.context.tool_settings.mesh_select_mode[0]:
loop_sel[loop.index] = v.select
loop_hide[loop.index] = v.hide
# Edge selection
if bpy.context.tool_settings.mesh_select_mode[1]:
for ed in modal._object_bm.edges:
if ed.select:
for v in ed.verts:
for loop in v.link_loops:
loop_sel[loop.index] = True
# Face selection
if bpy.context.tool_settings.mesh_select_mode[2]:
for f in modal._object_bm.faces:
if f.select:
if modal._individual_loops:
for loop in f.loops:
loop_sel[loop.index] = True
else:
for v in f.verts:
for loop in v.link_loops:
loop_sel[loop.index] = True
modal._container.hide_status = np.array(loop_hide, dtype=bool)
modal._container.sel_status = np.array(loop_sel, dtype=bool)
modal._container.act_status = np.array(loop_act, dtype=bool)
cache_mirror_data(modal)
return
def cache_mirror_data(modal):
mat = np.array(modal._object.matrix_world)
mat_inv = np.array(modal._object.matrix_world.inverted())
loc = mat[:3, 3]
mir_cos = (mat_inv[:3, :3] @ (modal._container.po_coords-loc).T).T
kd = create_kd_from_np(modal._container.po_coords)
modal.mir_loops_x = find_coord_mirror(modal, mir_cos.copy(), 0, mat, kd)
modal.mir_loops_y = find_coord_mirror(modal, mir_cos.copy(), 1, mat, kd)
modal.mir_loops_z = find_coord_mirror(modal, mir_cos.copy(), 2, mat, kd)
return
def find_coord_mirror(modal, mir_coords, mir_axis, mat, kd):
#
# Match the list of loops with their mirror on the set axis
# detect the proper loop on the mirror point by getting the smallest angle
#
# Get the loop coords on the mirrored axis
mir_coords[:, mir_axis] *= -1
l_coords = (mat[:3, :3] @ mir_coords.T).T+mat[:3, 3]
# ORIGINAL VERSION SUPER SLOW FOR A 12K VERT MESH 10 SECONDS PER AXIS
# # Test distance for nearest points from the loops mirroed coord
# po_matches = get_np_vecs_ordered_dists(
# modal._container.po_coords, l_coords)[:, 0]
# TEST VERSION USING get_np_vec_ordered_dists SLIGHTLY FASTER THAN get_np_vecs_ordered_dists
# match_inds = []
# for co in l_coords:
# match = get_np_vec_ordered_dists(modal._container.po_coords, co)[0]
# match_inds.append(match)
# po_matches = np.array(match_inds)
# Use kdtree to find nearest co on the mirror axis
# Far far far faster than current implementation of get_np_vecs_ordered_dists
# Despite that it is using a loop it cuts the time down immensely
po_matches = []
for co in l_coords:
res = kd.find(co)
po_matches.append(res[1])
po_matches = np.array(po_matches)
match_loops = modal._container.vert_link_ls[po_matches[modal._container.loop_verts]]
# Get the tangents of the matched mirror coord
tans = modal._container.loop_tangents[match_loops]
mir_tangs = modal._container.loop_tangents.copy()
mir_tangs[:, mir_axis] *= -1
# Test the dot products for the smallest of the current loop to the matched points loops
# filters out -1 mathc loop indices
match_tans = match_loops_vecs(mir_tangs, tans, match_loops)
return match_tans
def init_nav_list(modal):
modal.nav_list = []
names = ['Zoom View', 'Rotate View', 'Pan View', 'Dolly View',
'View Selected', 'View Camera Center', 'View All', 'View Axis',
'View Orbit', 'View Roll', 'View Persp/Ortho', 'View Camera', 'Frame Selected']
key_settings = []
config = bpy.context.window_manager.keyconfigs.user
if config:
for item in config.keymaps['3D View'].keymap_items:
if item.name in names:
if [item.name, item.type, item.value, item.any, item.ctrl, item.shift, item.alt] not in key_settings:
key_settings.append(
[item.name, item.type, item.value, item.any, item.ctrl, item.shift, item.alt])
modal.nav_list.append(item)
# Add basic pass thru keys
for item in modal.keymap.keymap_items:
if 'Pass Thru' in item.name:
modal.nav_list.append(item)
return
def ob_data_structures(modal, ob):
if ob.data.shape_keys is not None:
for sk in ob.data.shape_keys.key_blocks:
modal._objects_sk_vis.append(sk.mute)
sk.mute = True
bm = create_simple_bm(modal, ob)
bvh = BVHTree.FromBMesh(bm)
kd = create_kd(bm)
return bm, kd, bvh
def add_to_undostack(modal, stack_type):
if modal._history_position > 0:
while modal._history_position > 0:
if modal._history_stack[0] == 0:
modal._history_select_stack.pop(0)
modal._history_select_position -= 1
elif modal._history_stack[0] == 1:
modal._history_normal_stack.pop(0)
modal._history_normal_position -= 1
elif modal._history_stack[0] == 2:
modal._history_filter_stack.pop(0)
modal._history_filter_position -= 1
modal._history_stack.pop(0)
modal._history_position -= 1
if len(modal._history_stack)+1 > modal._history_steps:
if modal._history_stack[-1] == 0:
modal._history_select_stack.pop(-1)
elif modal._history_stack[-1] == 1:
modal._history_normal_stack.pop(-1)
elif modal._history_stack[-1] == 2:
modal._history_filter_stack.pop(-1)
modal._history_stack.pop(-1)
# Selection status
if stack_type == 0:
sel_status = modal._container.sel_status.nonzero()[0]
vis_status = modal._container.hide_status.nonzero()[0]
act_status = modal._container.act_status.nonzero()[0]
modal._history_stack.insert(0, stack_type)
modal._history_select_stack.insert(
0, [sel_status, vis_status, act_status])
modal.redraw = True
update_orbit_empty(modal)
# Normals status
elif stack_type == 1:
cur_normals = modal._container.new_norms.copy()
modal._history_stack.insert(0, stack_type)
modal._history_normal_stack.insert(0, cur_normals)
# Filter mask weights status
elif stack_type == 2:
cur_mask = modal._container.filter_mask.copy()
cur_weights = modal._container.filter_weights.copy()
modal._history_stack.insert(0, stack_type)
modal._history_filter_stack.insert(0, [cur_mask, cur_weights])
# Initial status
elif stack_type == 3:
sel_status = modal._container.sel_status.nonzero()[0]
vis_status = modal._container.hide_status.nonzero()[0]
act_status = modal._container.act_status.nonzero()[0]
cur_normals = modal._container.new_norms.copy()
cur_mask = modal._container.filter_mask.copy()
cur_weights = modal._container.filter_weights.copy()
modal._history_stack.insert(0, stack_type)
modal._history_select_stack.insert(
0, [sel_status, vis_status, act_status])
modal._history_normal_stack.insert(0, cur_normals)
modal._history_filter_stack.insert(0, [cur_mask, cur_weights])
modal.redraw = True
update_orbit_empty(modal)
return
def move_undostack(modal, dir):
if (dir > 0 and len(modal._history_stack)-1 > modal._history_position) or (dir < 0 and modal._history_position > 0):
if dir > 0:
state_type = modal._history_stack[modal._history_position]
modal._history_position += dir
else:
modal._history_position += dir
state_type = modal._history_stack[modal._history_position]
if state_type == 0:
modal._history_select_position += dir
state = modal._history_select_stack[modal._history_select_position]
modal._container.sel_status[:] = False
modal._container.sel_status[state[0]] = True
modal._container.hide_status[:] = False
modal._container.hide_status[state[1]] = True
modal._container.act_status[:] = False
modal._container.act_status[state[2]] = True
update_orbit_empty(modal)
modal.redraw = True
elif state_type == 1:
modal._history_normal_position += dir
state = modal._history_normal_stack[modal._history_normal_position]
modal._container.new_norms[:] = state
set_new_normals(modal)
modal.redraw = True
if state_type == 2:
modal._history_select_position += dir
modal._history_normal_position += dir
sel_state = modal._history_select_stack[modal._history_select_position]
norm_state = modal._history_normal_stack[modal._history_normal_position]
modal._container.sel_status[:] = False
modal._container.sel_status[sel_state[0]] = True
modal._container.hide_status[:] = False
modal._container.hide_status[sel_state[1]] = True
modal._container.act_status[:] = False
modal._container.act_status[sel_state[2]] = True
modal._container.new_norms[:] = norm_state
set_new_normals(modal)
update_orbit_empty(modal)
modal.redraw = True
return
def img_load(img_name, path):
script_file = os.path.realpath(path)
directory = os.path.dirname(script_file)
img_fp = directory.replace('/', '\\') + '\\' + img_name
not_there = True
for img in bpy.data.images:
if img.filepath == img_fp:
not_there = False
break
if not_there:
img = bpy.data.images.load(img_fp)
try:
img.colorspace_settings.name = 'Raw'
except:
pass
if img.gl_load():
raise Exception()
return img
def finish_modal(modal, restore):
modal._behavior_prefs.rotate_gizmo_use = modal._use_gizmo
modal._display_prefs.gizmo_size = modal._gizmo_size
modal._display_prefs.normal_size = modal._normal_size
modal._display_prefs.line_brightness = modal._line_brightness
modal._display_prefs.point_size = modal._point_size
modal._display_prefs.loop_tri_size = modal._loop_tri_size
modal._display_prefs.selected_only = modal._selected_only
modal._display_prefs.draw_weights = modal._draw_weights
modal._display_prefs.selected_scale = modal._selected_scale
modal._behavior_prefs.individual_loops = modal._individual_loops
modal._display_prefs.ui_scale = modal._ui_scale
modal._display_prefs.display_wireframe = modal._use_wireframe_overlay
if bpy.context.area is not None: