-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
subtree.py
2651 lines (2077 loc) · 106 KB
/
subtree.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, re
from . import lib, Modifier, MaskModifier
from .common import *
from .node_arrangements import *
from .node_connections import *
def check_layer_divider_alpha(layer, tree=None):
if not tree: tree = get_source_tree(layer)
if layer.divide_rgb_by_alpha and layer.type in {'IMAGE', 'VCOL'}:
divider_alpha = check_new_mix_node(tree, layer, 'divider_alpha', 'Spread Fix')
divider_alpha.blend_type = 'DIVIDE'
divider_alpha.inputs[0].default_value = 1.0
set_mix_clamp(divider_alpha, True)
else:
remove_node(tree, layer, 'divider_alpha')
def check_entity_image_flip_y(entity):
yp = entity.id_data.yp
m1 = re.match(r'yp\.layers\[(\d+)\]$', entity.path_from_id())
m2 = re.match(r'yp\.layers\[(\d+)\]\.channels\[(\d+)\]$', entity.path_from_id())
if m1:
layer = entity
tree = get_source_tree(layer)
flip_y_needed = layer.image_flip_y and layer.type == 'IMAGE'
elif m2:
layer = yp.layers[int(m2.group(1))]
ch = entity
tree = get_tree(layer)
flip_y_needed = ch.image_flip_y and ch.normal_map_type in {'NORMAL_MAP', 'BUMP_NORMAL_MAP'} and ch.override_1 and ch.override_1_type == 'IMAGE'
else:
return None
if flip_y_needed:
flip_y = check_new_node(tree, entity, 'flip_y', 'ShaderNodeGroup', 'Flip Y')
flip_y.node_tree = lib.get_node_tree_lib(lib.FLIP_Y)
else:
remove_node(tree, entity, 'flip_y')
return layer
def move_mod_group(layer, from_tree, to_tree):
mod_group = from_tree.nodes.get(layer.mod_group)
if mod_group:
mod_tree = mod_group.node_tree
remove_node(from_tree, layer, 'mod_group', remove_data=False)
remove_node(from_tree, layer, 'mod_group_1', remove_data=False)
mod_group = new_node(to_tree, layer, 'mod_group', 'ShaderNodeGroup', 'mod_group')
mod_group.node_tree = mod_tree
mod_group_1 = new_node(to_tree, layer, 'mod_group_1', 'ShaderNodeGroup', 'mod_group_1')
mod_group_1.node_tree = mod_tree
def refresh_source_tree_ios(source_tree, layer_type):
# Create input and outputs
inp = get_tree_input_by_name(source_tree, 'Vector')
if not inp: new_tree_input(source_tree, 'Vector', 'NodeSocketVector')
out = get_tree_output_by_name(source_tree, 'Color')
if not out: new_tree_output(source_tree, 'Color', 'NodeSocketColor')
out = get_tree_output_by_name(source_tree, 'Alpha')
if not out: new_tree_output(source_tree, 'Alpha', 'NodeSocketFloat')
col1 = get_tree_output_by_name(source_tree, 'Color 1')
alp1 = get_tree_output_by_name(source_tree, 'Alpha 1')
if layer_type not in {'IMAGE', 'MUSGRAVE'}:
if not col1: col1 = new_tree_output(source_tree, 'Color 1', 'NodeSocketColor')
if not alp1: alp1 = new_tree_output(source_tree, 'Alpha 1', 'NodeSocketFloat')
else:
if col1: remove_tree_output(source_tree, col1)
if alp1: remove_tree_output(source_tree, alp1)
def enable_channel_source_tree(layer, root_ch, ch, rearrange = False):
#if not ch.override: return
if ch.source_group != '': return
layer_tree = get_tree(layer)
if ch.override_type not in {'VCOL', 'HEMI', 'DEFAULT'}:
# Get current source for reference
source_ref = layer_tree.nodes.get(ch.source)
linear_ref = layer_tree.nodes.get(ch.linear)
if not source_ref: return
# Create source tree
source_tree = bpy.data.node_groups.new(LAYERGROUP_PREFIX + root_ch.name + ' Source', 'ShaderNodeTree')
create_essential_nodes(source_tree, True)
refresh_source_tree_ios(source_tree, ch.override_type)
# Copy source from reference
source = new_node(source_tree, ch, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
# Copy linear node from reference
if linear_ref:
linear = new_node(source_tree, ch, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
# Create source node group
source_group = new_node(layer_tree, ch, 'source_group', 'ShaderNodeGroup', 'source_group')
source_n = new_node(layer_tree, ch, 'source_n', 'ShaderNodeGroup', 'source_n')
source_s = new_node(layer_tree, ch, 'source_s', 'ShaderNodeGroup', 'source_s')
source_e = new_node(layer_tree, ch, 'source_e', 'ShaderNodeGroup', 'source_e')
source_w = new_node(layer_tree, ch, 'source_w', 'ShaderNodeGroup', 'source_w')
source_group.node_tree = source_tree
source_n.node_tree = source_tree
source_s.node_tree = source_tree
source_e.node_tree = source_tree
source_w.node_tree = source_tree
layer_tree.nodes.remove(source_ref)
if linear_ref: layer_tree.nodes.remove(linear_ref)
# Create uv neighbor
if ch.override_type in {'VCOL', 'HEMI'}:
uv_neighbor = replace_new_node(
layer_tree, ch, 'uv_neighbor', 'ShaderNodeGroup', 'Neighbor UV',
lib.NEIGHBOR_FAKE, hard_replace=True
)
#else:
elif ch.override_type not in {'DEFAULT'}:
uv_neighbor = replace_new_node(
layer_tree, ch, 'uv_neighbor', 'ShaderNodeGroup', 'Neighbor UV',
lib.get_neighbor_uv_tree_name(layer.texcoord_type, entity=layer), hard_replace=True
)
set_uv_neighbor_resolution(ch, uv_neighbor)
if rearrange:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def enable_layer_source_tree(layer, rearrange=False):
# Check if source tree is already available
if layer.type in {'BACKGROUND', 'COLOR'}: return
if layer.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'BACKFACE'} and layer.source_group != '': return
layer_tree = get_tree(layer)
if layer.type not in {'VCOL', 'GROUP', 'HEMI', 'OBJECT_INDEX', 'BACKFACE'}:
# Get current source for reference
source_ref = layer_tree.nodes.get(layer.source)
linear_ref = layer_tree.nodes.get(layer.linear)
flip_y_ref = layer_tree.nodes.get(layer.flip_y)
divider_alpha_ref = layer_tree.nodes.get(layer.divider_alpha)
# Create source tree
source_tree = bpy.data.node_groups.new(LAYERGROUP_PREFIX + layer.name + ' Source', 'ShaderNodeTree')
create_essential_nodes(source_tree, True)
refresh_source_tree_ios(source_tree, layer.type)
# Copy source from reference
source = new_node(source_tree, layer, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
if linear_ref:
linear = new_node(source_tree, layer, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
if flip_y_ref:
flip_y = new_node(source_tree, layer, 'flip_y', flip_y_ref.bl_idname)
copy_node_props(flip_y_ref, flip_y)
if divider_alpha_ref:
divider_alpha = new_node(source_tree, layer, 'divider_alpha', divider_alpha_ref.bl_idname)
copy_node_props(divider_alpha_ref, divider_alpha)
# Create source node group
source_group = new_node(layer_tree, layer, 'source_group', 'ShaderNodeGroup', 'source_group')
source_n = new_node(layer_tree, layer, 'source_n', 'ShaderNodeGroup', 'source_n')
source_s = new_node(layer_tree, layer, 'source_s', 'ShaderNodeGroup', 'source_s')
source_e = new_node(layer_tree, layer, 'source_e', 'ShaderNodeGroup', 'source_e')
source_w = new_node(layer_tree, layer, 'source_w', 'ShaderNodeGroup', 'source_w')
source_group.node_tree = source_tree
source_n.node_tree = source_tree
source_s.node_tree = source_tree
source_e.node_tree = source_tree
source_w.node_tree = source_tree
# Remove previous source
layer_tree.nodes.remove(source_ref)
if linear_ref: layer_tree.nodes.remove(linear_ref)
if flip_y_ref: layer_tree.nodes.remove(flip_y_ref)
if divider_alpha_ref: layer_tree.nodes.remove(divider_alpha_ref)
# Bring modifiers to source tree
if layer.type in {'IMAGE', 'MUSGRAVE'}:
for mod in layer.modifiers:
Modifier.check_modifier_nodes(mod, source_tree, layer_tree)
else:
move_mod_group(layer, layer_tree, source_tree)
# Create uv neighbor
if layer.type in {'VCOL', 'HEMI'}:
uv_neighbor = replace_new_node(
layer_tree, layer, 'uv_neighbor', 'ShaderNodeGroup', 'Neighbor UV',
lib.NEIGHBOR_FAKE, hard_replace=True
)
if layer.type == 'VCOL':
uv_neighbor_1 = replace_new_node(
layer_tree, layer, 'uv_neighbor_1', 'ShaderNodeGroup', 'Neighbor UV 1',
lib.NEIGHBOR_FAKE, hard_replace=True
)
elif layer.type not in {'GROUP', 'OBJECT_INDEX', 'BACKFACE'}:
uv_neighbor = replace_new_node(
layer_tree, layer, 'uv_neighbor', 'ShaderNodeGroup', 'Neighbor UV',
lib.get_neighbor_uv_tree_name(layer.texcoord_type, entity=layer), hard_replace=True
)
set_uv_neighbor_resolution(layer, uv_neighbor)
if rearrange:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def disable_channel_source_tree(layer, root_ch, ch, rearrange=True, force=False):
yp = layer.id_data.yp
# Check if fine bump map is used on some of layer channels
if not force:
smooth_bump_ch = None
for i, root_ch in enumerate(yp.channels):
if root_ch.type == 'NORMAL' and root_ch.enable_smooth_bump and get_channel_enabled(layer.channels[i], layer, root_ch):
smooth_bump_ch = root_ch
if (ch.override_type not in {'DEFAULT'} and ch.source_group == '') or (not ch.override and smooth_bump_ch):
return
layer_tree = get_tree(layer)
#if ch.override_type not in {'DEFAULT'}:
source_group = layer_tree.nodes.get(ch.source_group)
if source_group:
source_ref = source_group.node_tree.nodes.get(ch.source)
linear_ref = source_group.node_tree.nodes.get(ch.linear)
# Create new source
source = new_node(layer_tree, ch, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
# Create new linear
if linear_ref:
linear = new_node(layer_tree, ch, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
# Remove previous source
remove_node(layer_tree, ch, 'source_group')
remove_node(layer_tree, ch, 'source_n')
remove_node(layer_tree, ch, 'source_s')
remove_node(layer_tree, ch, 'source_e')
remove_node(layer_tree, ch, 'source_w')
remove_node(layer_tree, ch, 'uv_neighbor')
#remove_node(layer_tree, ch, 'uv_neighbor_1')
if rearrange:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def disable_layer_source_tree(layer, rearrange=True, force=False):
yp = layer.id_data.yp
# Check if fine bump map is used on some of layer channels
if not force:
smooth_bump_ch = None
for i, root_ch in enumerate(yp.channels):
if root_ch.type == 'NORMAL' and root_ch.enable_smooth_bump and get_channel_enabled(layer.channels[i], layer, root_ch) and is_height_process_needed(layer):
smooth_bump_ch = root_ch
if (layer.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'BACKFACE'} and layer.source_group == '') or smooth_bump_ch:
return
layer_tree = get_tree(layer)
if force or layer.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'BACKFACE'}:
source_group = layer_tree.nodes.get(layer.source_group)
if source_group:
source_ref = source_group.node_tree.nodes.get(layer.source)
linear_ref = source_group.node_tree.nodes.get(layer.linear)
flip_y_ref = source_group.node_tree.nodes.get(layer.flip_y)
divider_alpha_ref = source_group.node_tree.nodes.get(layer.divider_alpha)
# Create new source
source = new_node(layer_tree, layer, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
if linear_ref:
linear = new_node(layer_tree, layer, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
if flip_y_ref:
flip_y = new_node(layer_tree, layer, 'flip_y', flip_y_ref.bl_idname)
copy_node_props(flip_y_ref, flip_y)
if divider_alpha_ref:
divider_alpha = new_node(layer_tree, layer, 'divider_alpha', divider_alpha_ref.bl_idname)
copy_node_props(divider_alpha_ref, divider_alpha)
# Bring back layer modifier to original tree
if layer.type in {'IMAGE', 'MUSGRAVE'}:
for mod in layer.modifiers:
Modifier.check_modifier_nodes(mod, layer_tree, source_group.node_tree)
else:
move_mod_group(layer, source_group.node_tree, layer_tree)
# Remove previous source
remove_node(layer_tree, layer, 'source_group')
remove_node(layer_tree, layer, 'source_n')
remove_node(layer_tree, layer, 'source_s')
remove_node(layer_tree, layer, 'source_e')
remove_node(layer_tree, layer, 'source_w')
remove_node(layer_tree, layer, 'uv_neighbor')
remove_node(layer_tree, layer, 'uv_neighbor_1')
if rearrange:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def check_layer_bump_process(layer, tree=None):
ypup = get_user_preferences()
yp = layer.id_data.yp
if not tree: tree = get_tree(layer)
height_root_ch = get_root_height_channel(yp)
# Check if previous normal is needed
need_prev_normal = check_need_prev_normal(layer)
dirty = False
if need_prev_normal and get_layer_enabled(layer):
if height_root_ch.enable_subdiv_setup: # and ypup.eevee_next_displacement:
lib_name = lib.SUBDIV_ON_NORMAL
elif height_root_ch.enable_smooth_bump:
lib_name = lib.FINE_BUMP_PROCESS
else: lib_name = lib.BUMP_PROCESS
bump_process, dirty = replace_new_node(
tree, layer, 'bump_process', 'ShaderNodeGroup', 'Bump Process',
lib_name, return_status=True, hard_replace=True
)
#update_layer_bump_process_max_height(height_root_ch, layer, tree)
else:
dirty = remove_node(tree, layer, 'bump_process')
return dirty
def check_mask_uv_neighbor(tree, layer, mask, mask_idx=-1):
yp = layer.id_data.yp
# Check if smooth bump channel is available
smooth_bump_ch = get_smooth_bump_channel(layer)
# Get channel that write height
write_height_ch = get_write_height_normal_channel(layer)
# Get mask index
if mask_idx == -1:
match = re.match(r'yp\.layers\[(\d+)\]\.masks\[(\d+)\]', mask.path_from_id())
mask_idx = int(match.group(2))
# Get chain
chain = get_bump_chain(layer)
if smooth_bump_ch and get_channel_enabled(smooth_bump_ch) and get_mask_enabled(mask) and (
(write_height_ch or mask_idx < chain) and
(mask.use_baked or (mask.type not in {'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'MODIFIER', 'EDGE_DETECT', 'HEMI', 'VCOL'} and mask.texcoord_type != 'Layer'))
):
#if not mask.use_baked and mask.type in {'VCOL', 'HEMI', 'EDGE_DETECT'}:
# lib_name = lib.NEIGHBOR_FAKE
#else:
lib_name = lib.get_neighbor_uv_tree_name(mask.texcoord_type, entity=mask)
uv_neighbor, dirty = replace_new_node(
tree, mask, 'uv_neighbor',
'ShaderNodeGroup', 'UV Neighbor', lib_name,
return_status=True, hard_replace=True
)
set_uv_neighbor_resolution(mask, uv_neighbor)
return dirty
else:
return remove_node(tree, mask, 'uv_neighbor')
return False
def enable_mask_source_tree(layer, mask, reconnect = False):
# Check if source tree is already available
#if (mask.use_baked or mask.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT'}) and mask.group_node != '': return
layer_tree = get_tree(layer)
# Create uv neighbor
#check_mask_uv_neighbor(layer_tree, layer, mask)
if mask.group_node == '' and (mask.use_baked or mask.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT'}):
# Get current source for reference
source_ref = layer_tree.nodes.get(mask.source)
baked_source_ref = layer_tree.nodes.get(mask.baked_source)
linear_ref = layer_tree.nodes.get(mask.linear)
# Create mask tree
mask_tree = bpy.data.node_groups.new(MASKGROUP_PREFIX + mask.name, 'ShaderNodeTree')
# Create input and outputs
if mask.type == 'MODIFIER':
new_tree_input(mask_tree, 'Value', 'NodeSocketFloat')
else: new_tree_input(mask_tree, 'Vector', 'NodeSocketVector')
new_tree_output(mask_tree, 'Value', 'NodeSocketFloat')
create_essential_nodes(mask_tree)
# Copy nodes from reference
source = new_node(mask_tree, mask, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
if baked_source_ref:
baked_source = new_node(mask_tree, mask, 'baked_source', baked_source_ref.bl_idname)
copy_node_props(baked_source_ref, baked_source)
if linear_ref:
linear = new_node(mask_tree, mask, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
# Create source node group
group_node = new_node(layer_tree, mask, 'group_node', 'ShaderNodeGroup', 'source_group')
source_n = new_node(layer_tree, mask, 'source_n', 'ShaderNodeGroup', 'source_n')
source_s = new_node(layer_tree, mask, 'source_s', 'ShaderNodeGroup', 'source_s')
source_e = new_node(layer_tree, mask, 'source_e', 'ShaderNodeGroup', 'source_e')
source_w = new_node(layer_tree, mask, 'source_w', 'ShaderNodeGroup', 'source_w')
group_node.node_tree = mask_tree
source_n.node_tree = mask_tree
source_s.node_tree = mask_tree
source_e.node_tree = mask_tree
source_w.node_tree = mask_tree
for mod in mask.modifiers:
MaskModifier.add_modifier_nodes(mod, mask_tree, layer_tree)
# Remove previous nodes
layer_tree.nodes.remove(source_ref)
if baked_source_ref: layer_tree.nodes.remove(baked_source_ref)
if linear_ref: layer_tree.nodes.remove(linear_ref)
if reconnect:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def disable_mask_source_tree(layer, mask, reconnect=False):
# Check if source tree is already gone
#if mask.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE'} and mask.group_node == '': return
layer_tree = get_tree(layer)
if mask.group_node != '': #and (mask.use_baked or mask.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE'}):
mask_tree = get_mask_tree(mask)
source_ref = mask_tree.nodes.get(mask.source)
baked_source_ref = mask_tree.nodes.get(mask.baked_source)
linear_ref = mask_tree.nodes.get(mask.linear)
group_node = layer_tree.nodes.get(mask.group_node)
# Create new nodes
source = new_node(layer_tree, mask, 'source', source_ref.bl_idname)
copy_node_props(source_ref, source)
if baked_source_ref:
baked_source = new_node(layer_tree, mask, 'baked_source', baked_source_ref.bl_idname)
copy_node_props(baked_source_ref, baked_source)
if linear_ref:
linear = new_node(layer_tree, mask, 'linear', linear_ref.bl_idname)
copy_node_props(linear_ref, linear)
for mod in mask.modifiers:
MaskModifier.add_modifier_nodes(mod, layer_tree, mask_tree)
# Remove previous source
remove_node(layer_tree, mask, 'group_node')
remove_node(layer_tree, mask, 'source_n')
remove_node(layer_tree, mask, 'source_s')
remove_node(layer_tree, mask, 'source_e')
remove_node(layer_tree, mask, 'source_w')
remove_node(layer_tree, mask, 'tangent')
remove_node(layer_tree, mask, 'bitangent')
remove_node(layer_tree, mask, 'tangent_flip')
remove_node(layer_tree, mask, 'bitangent_flip')
#remove_node(layer_tree, mask, 'uv_neighbor')
if reconnect:
# Reconnect outside nodes
reconnect_layer_nodes(layer)
# Rearrange nodes
rearrange_layer_nodes(layer)
def check_create_height_pack(layer, tree, height_root_ch, height_ch):
channel_enabled = get_channel_enabled(height_ch, layer, height_root_ch)
need_reconnect = False
# Height unpack for group layer
#if channel_enabled and height_root_ch.enable_smooth_bump and layer.type == 'GROUP':
# height_group_unpack, dirty = replace_new_node(tree, height_ch, 'height_group_unpack',
# 'ShaderNodeGroup', 'Unpack Height Group', lib.UNPACK_ONSEW, return_status=True)
# if dirty: need_reconnect = True
# height_alpha_group_unpack, dirty = replace_new_node(tree, height_ch, 'height_alpha_group_unpack',
# 'ShaderNodeGroup', 'Pack Height Alpha Group', lib.UNPACK_ONSEW, return_status=True)
# if dirty: need_reconnect = True
#else:
if remove_node(tree, height_ch, 'height_group_unpack'): need_reconnect = True
if remove_node(tree, height_ch, 'height_alpha_group_unpack'): need_reconnect = True
return need_reconnect
def check_create_spread_alpha(layer, tree, root_ch, ch):
channel_enabled = get_channel_enabled(ch, layer, root_ch)
need_reconnect = False
# NOTE: Remove spread alpha node entirely (at least for now), since it provides almost nothing in most situations
if False and channel_enabled and layer.type == 'IMAGE' and ch.normal_map_type != 'NORMAL_MAP': # and ch.enable_transition_bump:
if root_ch.enable_smooth_bump:
spread_alpha, dirty = replace_new_node(tree, ch, 'spread_alpha',
'ShaderNodeGroup', 'Spread Alpha Hack', lib.SPREAD_ALPHA_SMOOTH, hard_replace=True, return_status=True)
else:
spread_alpha, dirty = replace_new_node(tree, ch, 'spread_alpha',
'ShaderNodeGroup', 'Spread Alpha Hack', lib.SPREAD_ALPHA, hard_replace=True, return_status=True)
if dirty: need_reconnect = True
else:
if remove_node(tree, ch, 'spread_alpha'): need_reconnect = True
return need_reconnect
def check_mask_mix_nodes(layer, tree=None, specific_mask=None, specific_ch=None):
yp = layer.id_data.yp
if not tree: tree = get_tree(layer)
need_reconnect = False
trans_bump = get_transition_bump_channel(layer)
trans_bump_flip = trans_bump.transition_bump_flip if trans_bump else False
height_process_needed = is_height_process_needed(layer)
chain = get_bump_chain(layer)
for i, mask in enumerate(layer.masks):
if specific_mask and mask != specific_mask: continue
for j, c in enumerate(mask.channels):
ch = layer.channels[j]
root_ch = yp.channels[j]
channel_enabled = get_channel_enabled(ch, layer, root_ch)
write_height = get_write_height(ch)
if specific_ch and ch != specific_ch: continue
if not channel_enabled or not layer.enable_masks or not mask.enable or not c.enable:
if remove_node(tree, c, 'mix'): need_reconnect = True
if remove_node(tree, c, 'mix_remains'): need_reconnect = True
if remove_node(tree, c, 'mix_limit'): need_reconnect = True
if remove_node(tree, c, 'mix_limit_normal'): need_reconnect = True
if root_ch.type == 'NORMAL':
if remove_node(tree, c, 'mix_pure'): need_reconnect = True
if remove_node(tree, c, 'mix_normal'): need_reconnect = True
continue
if (root_ch.type == 'NORMAL' and root_ch.enable_smooth_bump and height_process_needed and
(write_height or (not write_height and i < chain))
):
mix = tree.nodes.get(c.mix)
if mix and (mix.type != 'GROUP' or not mix.name.endswith(mask.blend_type)):
if remove_node(tree, c, 'mix'): need_reconnect = True
mix = None
if not mix:
need_reconnect = True
mix = new_node(tree, c, 'mix', 'ShaderNodeGroup', 'Mask Blend')
mix.node_tree = lib.get_smooth_mix_node(mask.blend_type, layer.type)
set_default_value(mix, 0, mask.intensity_value)
else:
mix = tree.nodes.get(c.mix)
if mix and mix.type not in {'MIX_RGB', 'MIX'}:
if remove_node(tree, c, 'mix'): need_reconnect = True
mix = None
if not mix:
need_reconnect = True
mix = new_mix_node(tree, c, 'mix', 'Mask Blend')
mix.inputs[0].default_value = mask.intensity_value
if mix.blend_type != mask.blend_type:
mix.blend_type = mask.blend_type
# Use clamp to keep value between 0.0 to 1.0
if mask.blend_type not in {'MIX', 'MULTIPLY'}:
set_mix_clamp(mix, True)
if root_ch.type == 'NORMAL':
if i >= chain and trans_bump and ch == trans_bump:
mix_pure = tree.nodes.get(c.mix_pure)
if not mix_pure:
need_reconnect = True
mix_pure = new_mix_node(tree, c, 'mix_pure', 'Mask Blend Pure')
if mix_pure.blend_type != mask.blend_type:
mix_pure.blend_type = mask.blend_type
# Use clamp to keep value between 0.0 to 1.0
set_mix_clamp(mix_pure, True)
mix_pure.inputs[0].default_value = mask.intensity_value
else:
if remove_node(tree, c, 'mix_pure'): need_reconnect = True
if i >= chain and (
(trans_bump and ch == trans_bump and ch.transition_bump_crease) or
(not trans_bump)
):
mix_remains = tree.nodes.get(c.mix_remains)
if not mix_remains:
need_reconnect = True
mix_remains = new_mix_node(tree, c, 'mix_remains', 'Mask Blend Remaining')
mix_remains.inputs[0].default_value = mask.intensity_value
if mix_remains.blend_type != mask.blend_type:
mix_remains.blend_type = mask.blend_type
# Use clamp to keep value between 0.0 to 1.0
if mask.blend_type not in {'MIX', 'MULTIPLY'}:
set_mix_clamp(mix_remains, True)
else:
if remove_node(tree, c, 'mix_remains'): need_reconnect = True
if layer.type == 'GROUP' and is_layer_using_normal_map(layer):
mix_normal = tree.nodes.get(c.mix_normal)
if not mix_normal:
need_reconnect = True
mix_normal = new_mix_node(tree, c, 'mix_normal', 'Mask Normal')
mix_normal.inputs[0].default_value = mask.intensity_value
if mix_normal.blend_type != mask.blend_type:
mix_normal.blend_type = mask.blend_type
# Use clamp to keep value between 0.0 to 1.0
if mask.blend_type not in {'MIX', 'MULTIPLY'}:
set_mix_clamp(mix_normal, True)
else:
if remove_node(tree, c, 'mix_normal'): need_reconnect = True
else:
if (trans_bump and i >= chain and (
(trans_bump_flip and ch.enable_transition_ramp) or
(not trans_bump_flip and ch.enable_transition_ao)
)):
mix_remains = tree.nodes.get(c.mix_remains)
if not mix_remains:
need_reconnect = True
mix_remains = new_mix_node(tree, c, 'mix_remains', 'Mask Blend n')
mix_remains.inputs[0].default_value = mask.intensity_value
if mix_remains.blend_type != mask.blend_type:
mix_remains.blend_type = mask.blend_type
# Use clamp to keep value between 0.0 to 1.0
if mask.blend_type not in {'MIX', 'MULTIPLY'}:
set_mix_clamp(mix_remains, True)
else:
if remove_node(tree, c, 'mix_remains'): need_reconnect = True
if layer.type == 'GROUP' and mask.blend_type in limited_mask_blend_types:
if root_ch.type != 'NORMAL' or not root_ch.enable_smooth_bump and height_process_needed:
mix_limit = tree.nodes.get(c.mix_limit)
if not mix_limit:
need_reconnect = True
mix_limit = new_node(tree, c, 'mix_limit', 'ShaderNodeMath', root_ch.name + ' Mask Limit')
mix_limit.operation = 'MINIMUM'
mix_limit.use_clamp = True
else:
if remove_node(tree, c, 'mix_limit'): need_reconnect = True
if root_ch.type == 'NORMAL':
mix_limit_normal = tree.nodes.get(c.mix_limit_normal)
if not mix_limit_normal:
need_reconnect = True
mix_limit_normal = new_node(tree, c, 'mix_limit_normal', 'ShaderNodeMath', root_ch.name + ' Mask Limit Normal')
mix_limit_normal.operation = 'MINIMUM'
mix_limit_normal.use_clamp = True
else:
if remove_node(tree, c, 'mix_limit'): need_reconnect = True
if remove_node(tree, c, 'mix_limit_normal'): need_reconnect = True
return need_reconnect
def check_mask_source_tree(layer, specific_mask=None): #, ch=None):
#print("Checking mask source tree. Layer: " + layer.name + ' Specific Mask: ' + str(specific_mask))
yp = layer.id_data.yp
smooth_bump_ch = get_smooth_bump_channel(layer)
write_height_ch = get_write_height_normal_channel(layer)
chain = get_bump_chain(layer)
ch_idx = get_layer_channel_index(layer, smooth_bump_ch)
tree = get_tree(layer)
height_process_needed = is_height_process_needed(layer)
for i, mask in enumerate(layer.masks):
if specific_mask and specific_mask != mask: continue
if smooth_bump_ch and get_channel_enabled(smooth_bump_ch, layer, yp.channels[ch_idx]) and get_mask_enabled(mask) and (
mask.channels[ch_idx].enable and height_process_needed and (write_height_ch or i < chain) and
(mask.use_baked or mask.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT'})
):
enable_mask_source_tree(layer, mask)
else:
disable_mask_source_tree(layer, mask)
check_mask_uv_neighbor(tree, layer, mask)
def remove_tangent_sign_vcol(obj, uv_name):
mat = obj.active_material
objs = []
if obj.type == 'MESH':
objs.append(obj)
if mat.users > 1:
for ob in get_scene_objects():
if ob.type != 'MESH': continue
if mat.name in ob.data.materials and ob not in objs:
objs.append(ob)
for ob in objs:
vcols = get_vertex_colors(ob)
vcol = vcols.get(TANGENT_SIGN_PREFIX + uv_name)
if vcol: vcol = vcols.remove(vcol)
def recover_tangent_sign_process(ori_obj, ori_mode, ori_selects):
# Recover selected and active objects
bpy.ops.object.select_all(action='DESELECT')
for o in ori_selects:
if is_bl_newer_than(2, 80): o.select_set(True)
else: o.select = True
if is_bl_newer_than(2, 80): bpy.context.view_layer.objects.active = ori_obj
else: bpy.context.scene.objects.active = ori_obj
# Back to original mode
if ori_mode != ori_obj.mode:
bpy.ops.object.mode_set(mode=ori_mode)
def actual_refresh_tangent_sign_vcol(obj, uv_name):
if obj.type != 'MESH': return None
# Cannot do this in edit mode
ori_obj = bpy.context.object
ori_mode = ori_obj.mode
if ori_mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# Select only relevant object
ori_selects = [o for o in bpy.context.selected_objects]
bpy.ops.object.select_all(action='DESELECT')
if is_bl_newer_than(2, 80):
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
else:
obj.select = True
bpy.context.scene.objects.active = obj
# Set vertex color of bitangent sign
uv_layers = get_uv_layers(obj)
uv_layer = uv_layers.get(uv_name)
if uv_layer:
# Set uv as active
ori_layer_name = uv_layers.active.name
uv_layers.active = uv_layer
# Get vertex color
vcols = get_vertex_colors(obj)
vcol = vcols.get(TANGENT_SIGN_PREFIX + uv_name)
if not vcol:
try:
vcol = new_vertex_color(obj, TANGENT_SIGN_PREFIX + uv_name)
except:
recover_tangent_sign_process(ori_obj, ori_mode, ori_selects)
return None
# Set default color to be white
if is_bl_newer_than(2, 80):
for d in vcol.data:
d.color = (1.0, 1.0, 1.0, 1.0)
else:
for d in vcol.data:
d.color = (1.0, 1.0, 1.0)
# Use try except because ngon can cause error
try:
# Calc tangents
obj.data.calc_tangents()
# Get vcol again after calculate tangent to prevent error
vcol = vcols.get(TANGENT_SIGN_PREFIX + uv_name)
# Set tangent sign to vertex color
i = 0
for poly in obj.data.polygons:
for idx in poly.loop_indices:
vert = obj.data.loops[idx]
bs = max(vert.bitangent_sign, 0.0)
# Invert bitangent sign so the default value is 0.0 rather than 1.0
bs = 1.0 - bs
if is_bl_newer_than(2, 80):
vcol.data[i].color = (bs, bs, bs, 1.0)
else: vcol.data[i].color = (bs, bs, bs)
i += 1
# If using ngon, need a temporary mesh
except:
# Remember selection
if is_bl_newer_than(2, 80):
ori_select = [o for o in bpy.context.view_layer.objects if o.select_get()]
else: ori_select = [o for o in bpy.context.scene.objects if o.select]
# If object has multi users, get all related objects
related_objs = []
if obj.data.users > 1:
for o in get_scene_objects():
if o.data == obj.data and o != obj:
related_objs.append(o)
# Make object data single user
obj.data = obj.data.copy()
temp_ob = obj.copy()
temp_ob.data = obj.data.copy()
temp_ob.name = '___TEMP__'
if is_bl_newer_than(2, 80):
bpy.context.scene.collection.objects.link(temp_ob)
bpy.context.view_layer.objects.active = temp_ob
else:
bpy.context.scene.objects.link(temp_ob)
bpy.context.scene.objects.active = temp_ob
# Triangulate ngon faces on temp object
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.reveal()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(type="FACE")
bpy.ops.mesh.select_face_by_sides(number=4, type='GREATER')
bpy.ops.mesh.quads_convert_to_tris()
bpy.ops.mesh.tris_convert_to_quads()
bpy.ops.object.mode_set(mode='OBJECT')
# Remove all modifiers on temp object
for mod in temp_ob.modifiers:
bpy.ops.object.modifier_remove(modifier=mod.name)
# Calc tangents
temp_ob.data.calc_tangents()
# Set tangent sign to vertex color
tvcols = get_vertex_colors(temp_ob)
temp_vcol = tvcols.get(TANGENT_SIGN_PREFIX + uv_name)
i = 0
for poly in temp_ob.data.polygons:
for idx in poly.loop_indices:
vert = temp_ob.data.loops[idx]
bs = max(vert.bitangent_sign, 0.0)
# Invert bitangent sign so the default value is 0.0 rather than 1.0
bs = 1.0 - bs
if is_bl_newer_than(2, 80):
temp_vcol.data[i].color = (bs, bs, bs, 1.0)
else: temp_vcol.data[i].color = (bs, bs, bs)
i += 1
# Set active object back to the original mesh
if is_bl_newer_than(2, 80):
bpy.context.view_layer.objects.active = obj
else: bpy.context.scene.objects.active = obj
# Number of original modifiers
num_mods = len(obj.modifiers)
# Remember original enabled modifiers
ori_show_render_mods = []
ori_show_viewport_mods = []
for m in obj.modifiers:
ori_show_viewport_mods.append(m.show_viewport)
ori_show_render_mods.append(m.show_render)
m.show_viewport = False
m.show_render = False
# Add data transfer to original object
mod_name = 'Transferz'
mod = obj.modifiers.new(mod_name, 'DATA_TRANSFER')
# Move data transfer modifier to the top
#for i in range(len(obj.modifiers)-1):
for i in range(num_mods):
bpy.ops.object.modifier_move_up(modifier=mod_name)
# Set transfer object
mod.object = temp_ob
mod.use_loop_data = True
mod.data_types_loops = {'VCOL'}
# Apply modifier
bpy.ops.object.modifier_apply(modifier=mod_name)
# Recover original enabled modifiers
for i, m in enumerate(obj.modifiers):
if ori_show_viewport_mods[i]:
m.show_viewport = ori_show_viewport_mods[i]
if ori_show_render_mods[i]:
m.show_render = ori_show_render_mods[i]
# Delete temp object
remove_mesh_obj(temp_ob)
# Set back original select
for o in ori_select:
if is_bl_newer_than(2, 80):
o.select_set(True)
else: o.select = True
# Bring object data to related objects
if related_objs:
ori_mesh = related_objs[0].data
ori_name = ori_mesh.name
for o in related_objs:
o.data = obj.data
remove_datablock(bpy.data.meshes, ori_mesh)
o.data.name = ori_name
# Recover active uv
set_active_uv_layer(obj, ori_layer_name)
# Recovers
recover_tangent_sign_process(ori_obj, ori_mode, ori_selects)
# Get vcol again to make sure the data is consistent
vcols = get_vertex_colors(obj)
vcol = vcols.get(TANGENT_SIGN_PREFIX + uv_name)
return vcol
# Recovers
recover_tangent_sign_process(ori_obj, ori_mode, ori_selects)