forked from ucupumar/ucupaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_connections.py
3624 lines (2825 loc) · 173 KB
/
node_connections.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 .common import *
def create_link(tree, out, inp):
node = inp.node
if not any(l for l in out.links if l.to_socket == inp):
tree.links.new(out, inp)
#print(out, 'is connected to', inp)
if node: return node.outputs
return []
def break_link(tree, out, inp):
for link in out.links:
if link.to_socket == inp:
tree.links.remove(link)
return True
return False
def break_input_link(tree, inp):
for link in inp.links:
tree.links.remove(link)
def break_output_link(tree, outp):
for link in outp.links:
tree.links.remove(link)
def reconnect_mask_modifier_nodes(tree, mod, start_value):
value = start_value
if mod.type == 'INVERT':
invert = tree.nodes.get(mod.invert)
create_link(tree, value, invert.inputs[1])
value = invert.outputs[0]
elif mod.type == 'RAMP':
ramp = tree.nodes.get(mod.ramp)
ramp_mix = tree.nodes.get(mod.ramp_mix)
mixcol0, mixcol1, mixout = get_mix_color_indices(ramp_mix)
create_link(tree, value, ramp.inputs[0])
create_link(tree, value, ramp_mix.inputs[mixcol0])
create_link(tree, ramp.outputs[0], ramp_mix.inputs[mixcol1])
value = ramp_mix.outputs[mixout]
elif mod.type == 'CURVE':
curve = tree.nodes.get(mod.curve)
create_link(tree, value, curve.inputs[1])
value = curve.outputs[0]
return value
def reconnect_modifier_nodes(tree, mod, start_rgb, start_alpha):
if not mod.enable:
return start_rgb, start_alpha
rgb = start_rgb
alpha = start_alpha
if mod.type == 'INVERT':
invert = tree.nodes.get(mod.invert)
create_link(tree, start_rgb, invert.inputs[0])
create_link(tree, start_alpha, invert.inputs[1])
rgb = invert.outputs[0]
alpha = invert.outputs[1]
elif mod.type == 'RGB_TO_INTENSITY':
rgb2i = tree.nodes.get(mod.rgb2i)
create_link(tree, start_rgb, rgb2i.inputs[0])
create_link(tree, start_alpha, rgb2i.inputs[1])
rgb = rgb2i.outputs[0]
alpha = rgb2i.outputs[1]
elif mod.type == 'INTENSITY_TO_RGB':
i2rgb = tree.nodes.get(mod.i2rgb)
create_link(tree, start_rgb, i2rgb.inputs[0])
create_link(tree, start_alpha, i2rgb.inputs[1])
rgb = i2rgb.outputs[0]
alpha = i2rgb.outputs[1]
elif mod.type == 'OVERRIDE_COLOR':
oc = tree.nodes.get(mod.oc)
create_link(tree, start_rgb, oc.inputs[0])
create_link(tree, start_alpha, oc.inputs[1])
rgb = oc.outputs[0]
alpha = oc.outputs[1]
elif mod.type == 'COLOR_RAMP':
color_ramp_alpha_multiply = tree.nodes.get(mod.color_ramp_alpha_multiply)
color_ramp_linear_start = tree.nodes.get(mod.color_ramp_linear_start)
color_ramp = tree.nodes.get(mod.color_ramp)
color_ramp_linear = tree.nodes.get(mod.color_ramp_linear)
color_ramp_mix_alpha = tree.nodes.get(mod.color_ramp_mix_alpha)
color_ramp_mix_rgb = tree.nodes.get(mod.color_ramp_mix_rgb)
am_mixcol0, am_mixcol1, am_mixout = get_mix_color_indices(color_ramp_alpha_multiply)
ma_mixcol0, ma_mixcol1, ma_mixout = get_mix_color_indices(color_ramp_mix_alpha)
mr_mixcol0, mr_mixcol1, mr_mixout = get_mix_color_indices(color_ramp_mix_rgb)
create_link(tree, start_rgb, color_ramp_alpha_multiply.inputs[am_mixcol0])
create_link(tree, start_alpha, color_ramp_alpha_multiply.inputs[am_mixcol1])
if color_ramp_linear_start:
create_link(tree, color_ramp_alpha_multiply.outputs[am_mixout], color_ramp_linear_start.inputs[0])
create_link(tree, color_ramp_linear_start.outputs[0], color_ramp.inputs[0])
else:
create_link(tree, color_ramp_alpha_multiply.outputs[am_mixout], color_ramp.inputs[0])
create_link(tree, start_rgb, color_ramp_mix_rgb.inputs[mr_mixcol0])
if color_ramp_linear_start:
create_link(tree, color_ramp.outputs[0], color_ramp_linear.inputs[0])
create_link(tree, color_ramp_linear.outputs[0], color_ramp_mix_rgb.inputs[mr_mixcol1])
else:
create_link(tree, color_ramp.outputs[0], color_ramp_mix_rgb.inputs[mr_mixcol1])
create_link(tree, start_alpha, color_ramp_mix_alpha.inputs[ma_mixcol0])
create_link(tree, color_ramp.outputs[1], color_ramp_mix_alpha.inputs[ma_mixcol1])
rgb = color_ramp_mix_rgb.outputs[mr_mixout]
alpha = color_ramp_mix_alpha.outputs[ma_mixout]
elif mod.type == 'RGB_CURVE':
rgb_curve = tree.nodes.get(mod.rgb_curve)
create_link(tree, start_rgb, rgb_curve.inputs[1])
rgb = rgb_curve.outputs[0]
elif mod.type == 'HUE_SATURATION':
huesat = tree.nodes.get(mod.huesat)
create_link(tree, start_rgb, huesat.inputs[4])
rgb = huesat.outputs[0]
elif mod.type == 'BRIGHT_CONTRAST':
brightcon = tree.nodes.get(mod.brightcon)
create_link(tree, start_rgb, brightcon.inputs[0])
rgb = brightcon.outputs[0]
elif mod.type == 'MULTIPLIER':
multiplier = tree.nodes.get(mod.multiplier)
create_link(tree, start_rgb, multiplier.inputs[0])
create_link(tree, start_alpha, multiplier.inputs[1])
rgb = multiplier.outputs[0]
alpha = multiplier.outputs[1]
elif mod.type == 'MATH':
math = tree.nodes.get(mod.math)
create_link(tree, start_rgb, math.inputs[0])
create_link(tree, start_alpha, math.inputs[1])
rgb = math.outputs[0]
alpha = math.outputs[1]
return rgb, alpha
def reconnect_all_modifier_nodes(tree, parent, start_rgb, start_alpha, mod_group=None, use_modifier_1=False):
rgb = start_rgb
alpha = start_alpha
if mod_group:
# Connect modifier group node
create_link(tree, rgb, mod_group.inputs[0])
create_link(tree, alpha, mod_group.inputs[1])
# Get nodes inside modifier group tree and repoint it
mod_tree = mod_group.node_tree
start = mod_tree.nodes.get(MOD_TREE_START)
rgb = start.outputs[0]
alpha = start.outputs[1]
else:
mod_tree = tree
modifiers = parent.modifiers
if use_modifier_1:
modifiers = parent.modifiers_1
# Connect all the nodes
for mod in reversed(modifiers):
rgb, alpha = reconnect_modifier_nodes(mod_tree, mod, rgb, alpha)
if mod_group:
# Connect to end node
end = mod_tree.nodes.get(MOD_TREE_END)
create_link(mod_tree, rgb, end.inputs[0])
create_link(mod_tree, alpha, end.inputs[1])
# Repoint rgb and alpha to mod group
rgb = mod_group.outputs[0]
alpha = mod_group.outputs[1]
return rgb, alpha
def remove_all_prev_inputs(tree, layer, node): #, height_only=False):
yp = layer.id_data.yp
if layer.parent_idx == -1:
return
for i, ch in enumerate(layer.channels):
root_ch = yp.channels[i]
if has_previous_layer_channels(layer, root_ch): continue
if root_ch.type == 'NORMAL':
io_name = root_ch.name + io_suffix['HEIGHT']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['HEIGHT'] + io_suffix['ALPHA']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
#if height_only: continue
if root_ch.enable_smooth_bump:
for letter in nsew_letters:
io_name = root_ch.name + io_suffix['HEIGHT_' + letter.upper()]
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['HEIGHT_' + letter.upper()] + io_suffix['ALPHA']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['MAX_HEIGHT']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['VDISP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
#if height_only: continue
io_name = root_ch.name
if io_name in node.inputs:
# Should always fill normal input
#geometry = tree.nodes.get(GEOMETRY)
if root_ch.type == 'NORMAL': # and geometry:
create_link(tree, get_essential_node(tree, GEOMETRY)['Normal'], node.inputs[io_name])
else:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['ALPHA']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
def remove_unused_group_node_connections(tree, layer, node): #, height_only=False):
yp = layer.id_data.yp
#node = tree.nodes.get(layer.group_node)
if layer.type != 'GROUP':
return
for i, ch in enumerate(layer.channels):
root_ch = yp.channels[i]
if has_channel_children(layer, root_ch): continue
io_name = root_ch.name + io_suffix['HEIGHT'] + io_suffix['GROUP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['HEIGHT'] + io_suffix['ALPHA'] + io_suffix['GROUP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
#if height_only: continue
io_name = root_ch.name + io_suffix['GROUP']
if io_name in node.inputs:
# Should always fill normal input
#geometry = tree.nodes.get(GEOMETRY)
#if root_ch.type == 'NORMAL' and geometry:
# create_link(tree, geometry.outputs['Normal'], node.inputs[io_name])
#else:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['ALPHA'] + io_suffix['GROUP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
if root_ch.enable_smooth_bump:
for letter in nsew_letters:
io_name = root_ch.name + io_suffix['HEIGHT_' + letter.upper()] + io_suffix['GROUP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
io_name = root_ch.name + io_suffix['HEIGHT_' + letter.upper()] + io_suffix['ALPHA'] + io_suffix['GROUP']
if io_name in node.inputs:
break_input_link(tree, node.inputs[io_name])
def reconnect_relief_mapping_nodes(yp, node):
parallax_ch = get_root_parallax_channel(yp)
if not parallax_ch: return
linear_loop = node.node_tree.nodes.get('_linear_search')
binary_loop = node.node_tree.nodes.get('_binary_search')
loop = node.node_tree.nodes.get('_linear_search')
if loop:
loop_start = loop.node_tree.nodes.get(TREE_START)
loop_end = loop.node_tree.nodes.get(TREE_END)
prev_it = None
for i in range (parallax_ch.parallax_num_of_linear_samples):
it = loop.node_tree.nodes.get('_iterate_' + str(i))
if not prev_it:
create_link(loop.node_tree,
loop_start.outputs['t'], it.inputs['t'])
else:
create_link(loop.node_tree,
prev_it.outputs['t'], it.inputs['t'])
create_link(loop.node_tree,
loop_start.outputs['tx'], it.inputs['tx'])
create_link(loop.node_tree,
loop_start.outputs['v'], it.inputs['v'])
create_link(loop.node_tree,
loop_start.outputs['dataz'], it.inputs['dataz'])
create_link(loop.node_tree,
loop_start.outputs['size'], it.inputs['size'])
if i == parallax_ch.parallax_num_of_linear_samples-1:
create_link(loop.node_tree,
it.outputs['t'], loop_end.inputs['t'])
prev_it = it
loop = node.node_tree.nodes.get('_binary_search')
if loop:
loop_start = loop.node_tree.nodes.get(TREE_START)
loop_end = loop.node_tree.nodes.get(TREE_END)
prev_it = None
for i in range (parallax_ch.parallax_num_of_binary_samples):
it = loop.node_tree.nodes.get('_iterate_' + str(i))
if not prev_it:
create_link(loop.node_tree,
loop_start.outputs['t'], it.inputs['t'])
create_link(loop.node_tree,
loop_start.outputs['size'], it.inputs['size'])
else:
create_link(loop.node_tree,
prev_it.outputs['t'], it.inputs['t'])
create_link(loop.node_tree,
prev_it.outputs['size'], it.inputs['size'])
create_link(loop.node_tree,
loop_start.outputs['tx'], it.inputs['tx'])
create_link(loop.node_tree,
loop_start.outputs['v'], it.inputs['v'])
create_link(loop.node_tree,
loop_start.outputs['dataz'], it.inputs['dataz'])
if i == parallax_ch.parallax_num_of_binary_samples-1:
create_link(loop.node_tree,
it.outputs['t'], loop_end.inputs['t'])
prev_it = it
def connect_parallax_iteration(tree, prefix):
start = tree.nodes.get(TREE_START)
end = tree.nodes.get(TREE_END)
# Inside iterate group
prev_it = start
counter = 0
while True:
it = tree.nodes.get(prefix + str(counter))
if it:
for inp in it.inputs:
if inp.name in prev_it.outputs:
create_link(tree, prev_it.outputs[inp.name], inp)
elif inp.name in start.outputs:
create_link(tree, start.outputs[inp.name], inp)
else:
for inp in end.inputs:
if inp.name == '': continue
if inp.name in prev_it.outputs:
create_link(tree, prev_it.outputs[inp.name], inp)
elif inp.name in start.outputs:
create_link(tree, start.outputs[inp.name], inp)
break
prev_it = it
counter += 1
def reconnect_parallax_layer_nodes__(group_tree, parallax, uv_name=''):
yp = group_tree.yp
parallax_ch = get_root_parallax_channel(yp)
if not parallax_ch: return
# Connect iterate group
loop = parallax.node_tree.nodes.get('_parallax_loop')
if not loop: return
# Connect top level iteration
connect_parallax_iteration(loop.node_tree, '_iterate_')
# Connect depth lib iteration
counter = 0
while True:
it = loop.node_tree.nodes.get('_iterate_depth_' + str(counter))
if it:
connect_parallax_iteration(it.node_tree, '_iterate_')
else:
break
counter += 1
def reconnect_parallax_layer_nodes_(group_tree, parallax, uv_name=''):
yp = group_tree.yp
parallax_ch = get_root_parallax_channel(yp)
if not parallax_ch: return
# Connect iterate group
loop = parallax.node_tree.nodes.get('_parallax_loop')
if not loop: return
connect_parallax_iteration(loop.node_tree, '_iterate_group_')
# Connect inside iterate group
iterate_group_0 = loop.node_tree.nodes.get('_iterate_group_0')
if not iterate_group_0: return
connect_parallax_iteration(iterate_group_0.node_tree, '_iterate_')
#def reconnect_parallax_layer_nodes(group_tree, parallax, uv_name=''):
#
# yp = group_tree.yp
#
# parallax_ch = get_root_parallax_channel(yp)
# if not parallax_ch: return
#
# loop = parallax.node_tree.nodes.get('_parallax_loop')
# if not loop: return
#
# loop_start = loop.node_tree.nodes.get(TREE_START)
# loop_end = loop.node_tree.nodes.get(TREE_END)
#
# prev_it = None
#
# for i in range (parallax_ch.parallax_num_of_layers):
# it = loop.node_tree.nodes.get('_iterate_' + str(i))
#
# if not prev_it:
# create_link(loop.node_tree,
# loop_start.outputs['depth_from_tex'], it.inputs['depth_from_tex'])
#
# for uv in yp.uvs:
# if uv_name != '' and uv.name != uv_name: continue
# create_link(loop.node_tree,
# loop_start.outputs[uv.name + CURRENT_UV], it.inputs[uv.name + CURRENT_UV])
#
# for tc in texcoord_lists:
# io_name = TEXCOORD_IO_PREFIX + tc + CURRENT_UV
# if io_name in loop_start.outputs:
# create_link(loop.node_tree, loop_start.outputs[io_name], it.inputs[io_name])
# else:
# create_link(loop.node_tree,
# prev_it.outputs['cur_layer_depth'], it.inputs['cur_layer_depth'])
# create_link(loop.node_tree,
# prev_it.outputs['depth_from_tex'], it.inputs['depth_from_tex'])
#
# create_link(loop.node_tree,
# prev_it.outputs['index'], it.inputs['index'])
#
# for uv in yp.uvs:
# if uv_name != '' and uv.name != uv_name: continue
# create_link(loop.node_tree,
# prev_it.outputs[uv.name + CURRENT_UV], it.inputs[uv.name + CURRENT_UV])
#
# for tc in texcoord_lists:
# io_name = TEXCOORD_IO_PREFIX + tc + CURRENT_UV
# if io_name in prev_it.outputs:
# create_link(loop.node_tree, prev_it.outputs[io_name], it.inputs[io_name])
#
# create_link(loop.node_tree,
# loop_start.outputs['layer_depth'], it.inputs['layer_depth'])
# create_link(loop.node_tree,
# loop_start.outputs['base'], it.inputs['base'])
#
# for uv in yp.uvs:
# if uv_name != '' and uv.name != uv_name: continue
# create_link(loop.node_tree, loop_start.outputs[uv.name + START_UV], it.inputs[uv.name + START_UV])
# create_link(loop.node_tree, loop_start.outputs[uv.name + DELTA_UV], it.inputs[uv.name + DELTA_UV])
#
# for tc in texcoord_lists:
# io_name = TEXCOORD_IO_PREFIX + tc + START_UV
# if io_name in loop_start.outputs:
# create_link(loop.node_tree, loop_start.outputs[io_name], it.inputs[io_name])
# io_name = TEXCOORD_IO_PREFIX + tc + DELTA_UV
# if io_name in loop_start.outputs:
# create_link(loop.node_tree, loop_start.outputs[io_name], it.inputs[io_name])
#
# if i == parallax_ch.parallax_num_of_layers-1:
#
# create_link(loop.node_tree,
# it.outputs['cur_layer_depth'], loop_end.inputs['cur_layer_depth'])
# create_link(loop.node_tree,
# it.outputs['depth_from_tex'], loop_end.inputs['depth_from_tex'])
# create_link(loop.node_tree,
# it.outputs['index'], loop_end.inputs['index'])
#
# for uv in yp.uvs:
# if uv_name != '' and uv.name != uv_name: continue
# create_link(loop.node_tree,
# it.outputs[uv.name + CURRENT_UV], loop_end.inputs[uv.name + CURRENT_UV])
#
# for tc in texcoord_lists:
# io_name = TEXCOORD_IO_PREFIX + tc + CURRENT_UV
# if io_name in it.outputs:
# create_link(loop.node_tree, it.outputs[io_name], loop_end.inputs[io_name])
#
# prev_it = it
def reconnect_baked_parallax_layer_nodes(yp, node):
parallax_ch = get_root_parallax_channel(yp)
if not parallax_ch: return
num_of_layers = int(parallax_ch.baked_parallax_num_of_layers)
loop = node.node_tree.nodes.get('_parallax_loop')
if loop:
loop_start = loop.node_tree.nodes.get(TREE_START)
loop_end = loop.node_tree.nodes.get(TREE_END)
prev_it = None
for i in range (num_of_layers):
it = loop.node_tree.nodes.get('_iterate_' + str(i))
if not prev_it:
create_link(loop.node_tree,
loop_start.outputs['cur_uv'], it.inputs['cur_uv'])
create_link(loop.node_tree,
loop_start.outputs['cur_layer_depth'], it.inputs['cur_layer_depth'])
create_link(loop.node_tree,
loop_start.outputs['depth_from_tex'], it.inputs['depth_from_tex'])
else:
create_link(loop.node_tree,
prev_it.outputs['cur_uv'], it.inputs['cur_uv'])
create_link(loop.node_tree,
prev_it.outputs['cur_layer_depth'], it.inputs['cur_layer_depth'])
create_link(loop.node_tree,
prev_it.outputs['depth_from_tex'], it.inputs['depth_from_tex'])
create_link(loop.node_tree,
loop_start.outputs['delta_uv'], it.inputs['delta_uv'])
create_link(loop.node_tree,
loop_start.outputs['layer_depth'], it.inputs['layer_depth'])
if i == num_of_layers-1:
create_link(loop.node_tree,
it.outputs['cur_uv'], loop_end.inputs['cur_uv'])
create_link(loop.node_tree,
it.outputs['cur_layer_depth'], loop_end.inputs['cur_layer_depth'])
create_link(loop.node_tree,
it.outputs['depth_from_tex'], loop_end.inputs['depth_from_tex'])
prev_it = it
def reconnect_parallax_process_nodes(group_tree, parallax, baked=False, uv_name=''): #, uv_maps, tangents, bitangents):
yp = group_tree.yp
#parallax = group_tree.nodes.get(PARALLAX)
#if not parallax: return
tree = parallax.node_tree
start = tree.nodes.get(TREE_START)
end = tree.nodes.get(TREE_END)
# Depth source
depth_source_0 = tree.nodes.get('_depth_source_0')
depth_source_1 = tree.nodes.get('_depth_source_1')
depth_start = depth_source_0.node_tree.nodes.get(TREE_START)
depth_end = depth_source_0.node_tree.nodes.get(TREE_END)
# Iteration
loop = tree.nodes.get('_parallax_loop')
iterate = loop.node_tree.nodes.get('_iterate')
iterate_start = iterate.node_tree.nodes.get(TREE_START)
iterate_end = iterate.node_tree.nodes.get(TREE_END)
iterate_depth = iterate.node_tree.nodes.get('_depth_from_tex')
iterate_branch = iterate.node_tree.nodes.get('_branch')
#iterate_group_0 = loop.node_tree.nodes.get('_iterate')
#iterate_group_start = iterate_group_0.node_tree.nodes.get(TREE_START)
#iterate_group_end = iterate_group_0.node_tree.nodes.get(TREE_END)
weight = tree.nodes.get('_weight')
for uv in yp.uvs:
if uv_name != '' and uv.name != uv_name: continue
# Start and delta uv inputs
create_link(tree, start.outputs[uv.name + START_UV], depth_source_0.inputs[uv.name + START_UV])
create_link(tree, start.outputs[uv.name + START_UV], depth_source_1.inputs[uv.name + START_UV])
create_link(tree, start.outputs[uv.name + START_UV], loop.inputs[uv.name + START_UV])
create_link(tree, start.outputs[uv.name + DELTA_UV], depth_source_0.inputs[uv.name + DELTA_UV])
create_link(tree, start.outputs[uv.name + DELTA_UV], depth_source_1.inputs[uv.name + DELTA_UV])
create_link(tree, start.outputs[uv.name + DELTA_UV], loop.inputs[uv.name + DELTA_UV])
create_link(tree, depth_source_0.outputs[uv.name + CURRENT_UV], loop.inputs[uv.name + CURRENT_UV])
# Parallax final mix
if baked: parallax_mix = tree.nodes.get(uv.baked_parallax_mix)
else: parallax_mix = tree.nodes.get(uv.parallax_mix)
mixcol0, mixcol1, mixout = get_mix_color_indices(parallax_mix)
create_link(tree, weight.outputs[0], parallax_mix.inputs[0])
create_link(tree, loop.outputs[uv.name + CURRENT_UV], parallax_mix.inputs[mixcol0])
create_link(tree, depth_source_1.outputs[uv.name + CURRENT_UV], parallax_mix.inputs[mixcol1])
# End uv
#create_link(tree, loop.outputs[uv.name + CURRENT_UV], end.inputs[uv.name])
create_link(tree, parallax_mix.outputs[mixout], end.inputs[uv.name])
# Inside depth source
if baked: delta_uv = depth_source_0.node_tree.nodes.get(uv.baked_parallax_delta_uv)
else: delta_uv = depth_source_0.node_tree.nodes.get(uv.parallax_delta_uv)
mixcol0, mixcol1, mixout = get_mix_color_indices(delta_uv)
if baked: current_uv = depth_source_0.node_tree.nodes.get(uv.baked_parallax_current_uv)
else: current_uv = depth_source_0.node_tree.nodes.get(uv.parallax_current_uv)
height_map = depth_source_0.node_tree.nodes.get(HEIGHT_MAP)
create_link(depth_source_0.node_tree, depth_start.outputs['index'], delta_uv.inputs[mixcol0])
create_link(depth_source_0.node_tree, depth_start.outputs[uv.name + DELTA_UV], delta_uv.inputs[mixcol1])
create_link(depth_source_0.node_tree, depth_start.outputs[uv.name + START_UV], current_uv.inputs[0])
create_link(depth_source_0.node_tree, delta_uv.outputs[mixout], current_uv.inputs[1])
create_link(depth_source_0.node_tree, current_uv.outputs[0], depth_end.inputs[uv.name + CURRENT_UV])
if height_map:
create_link(depth_source_0.node_tree, current_uv.outputs[0], height_map.inputs[0])
create_link(depth_source_0.node_tree, height_map.outputs[0], depth_end.inputs[0])
# Inside iteration
create_link(iterate.node_tree,
iterate_start.outputs[uv.name + START_UV], iterate_depth.inputs[uv.name + START_UV])
create_link(iterate.node_tree,
iterate_start.outputs[uv.name + DELTA_UV], iterate_depth.inputs[uv.name + DELTA_UV])
if baked: parallax_current_uv_mix = iterate.node_tree.nodes.get(uv.baked_parallax_current_uv_mix)
else: parallax_current_uv_mix = iterate.node_tree.nodes.get(uv.parallax_current_uv_mix)
mixcol0, mixcol1, mixout = get_mix_color_indices(parallax_current_uv_mix)
create_link(iterate.node_tree, iterate_branch.outputs[0], parallax_current_uv_mix.inputs[0])
create_link(iterate.node_tree,
iterate_depth.outputs[uv.name + CURRENT_UV], parallax_current_uv_mix.inputs[mixcol0])
create_link(iterate.node_tree,
iterate_start.outputs[uv.name + CURRENT_UV], parallax_current_uv_mix.inputs[mixcol1])
create_link(iterate.node_tree,
parallax_current_uv_mix.outputs[mixout], iterate_end.inputs[uv.name + CURRENT_UV])
if not baked:
for tc in texcoord_lists:
base_name = TEXCOORD_IO_PREFIX + tc
if base_name + START_UV not in start.outputs: continue
# Start and delta uv inputs
create_link(tree, start.outputs[base_name + START_UV], depth_source_0.inputs[base_name + START_UV])
create_link(tree, start.outputs[base_name + START_UV], depth_source_1.inputs[base_name + START_UV])
create_link(tree, start.outputs[base_name + START_UV], loop.inputs[base_name + START_UV])
create_link(tree, start.outputs[base_name + DELTA_UV], depth_source_0.inputs[base_name + DELTA_UV])
create_link(tree, start.outputs[base_name + DELTA_UV], depth_source_1.inputs[base_name + DELTA_UV])
create_link(tree, start.outputs[base_name + DELTA_UV], loop.inputs[base_name + DELTA_UV])
create_link(tree, depth_source_0.outputs[base_name + CURRENT_UV], loop.inputs[base_name + CURRENT_UV])
# Parallax final mix
parallax_mix = tree.nodes.get(PARALLAX_MIX_PREFIX + base_name)
mixcol0, mixcol1, mixout = get_mix_color_indices(parallax_mix)
create_link(tree, weight.outputs[0], parallax_mix.inputs[0])
create_link(tree, loop.outputs[base_name + CURRENT_UV], parallax_mix.inputs[mixcol0])
create_link(tree, depth_source_1.outputs[base_name + CURRENT_UV], parallax_mix.inputs[mixcol1])
# End uv
create_link(tree, parallax_mix.outputs[mixout], end.inputs[base_name])
# Inside depth source
delta_uv = depth_source_0.node_tree.nodes.get(PARALLAX_DELTA_PREFIX + base_name)
mixcol0, mixcol1, mixout = get_mix_color_indices(delta_uv)
current_uv = depth_source_0.node_tree.nodes.get(PARALLAX_CURRENT_PREFIX + base_name)
create_link(depth_source_0.node_tree, depth_start.outputs['index'], delta_uv.inputs[mixcol0])
create_link(depth_source_0.node_tree, depth_start.outputs[base_name + DELTA_UV], delta_uv.inputs[mixcol1])
create_link(depth_source_0.node_tree, depth_start.outputs[base_name + START_UV], current_uv.inputs[0])
create_link(depth_source_0.node_tree, delta_uv.outputs[mixout], current_uv.inputs[1])
create_link(depth_source_0.node_tree, current_uv.outputs[0], depth_end.inputs[base_name + CURRENT_UV])
# Inside iteration
create_link(iterate.node_tree,
iterate_start.outputs[base_name + START_UV], iterate_depth.inputs[base_name + START_UV])
create_link(iterate.node_tree,
iterate_start.outputs[base_name + DELTA_UV], iterate_depth.inputs[base_name + DELTA_UV])
parallax_current_uv_mix = iterate.node_tree.nodes.get(PARALLAX_CURRENT_MIX_PREFIX + base_name)
mixcol0, mixcol1, mixout = get_mix_color_indices(parallax_current_uv_mix)
create_link(iterate.node_tree, iterate_branch.outputs[0], parallax_current_uv_mix.inputs[0])
create_link(iterate.node_tree,
iterate_depth.outputs[base_name + CURRENT_UV], parallax_current_uv_mix.inputs[mixcol0])
create_link(iterate.node_tree,
iterate_start.outputs[base_name + CURRENT_UV], parallax_current_uv_mix.inputs[mixcol1])
create_link(iterate.node_tree,
parallax_current_uv_mix.outputs[mixout], iterate_end.inputs[base_name + CURRENT_UV])
#reconnect_parallax_layer_nodes(group_tree, parallax, uv_name)
#reconnect_parallax_layer_nodes_(group_tree, parallax, uv_name)
reconnect_parallax_layer_nodes__(group_tree, parallax, uv_name)
def reconnect_depth_layer_nodes(group_tree, parallax_ch, parallax):
yp = group_tree.yp
depth_source_0 = parallax.node_tree.nodes.get('_depth_source_0')
tree = depth_source_0.node_tree
start = tree.nodes.get(TREE_START)
end = tree.nodes.get(TREE_END)
unpack = tree.nodes.get('_unpack')
normalize = tree.nodes.get('_normalize')
if parallax_ch.enable_smooth_bump:
io_height_name = parallax_ch.name + io_suffix['HEIGHT_ONS']
else: io_height_name = parallax_ch.name + io_suffix['HEIGHT']
io_alpha_name = parallax_ch.name + io_suffix['ALPHA']
if parallax_ch.enable_smooth_bump:
io_height_alpha_name = parallax_ch.name + io_suffix['HEIGHT_ONS'] + io_suffix['ALPHA']
else: io_height_alpha_name = parallax_ch.name + io_suffix['HEIGHT'] + io_suffix['ALPHA']
height = start.outputs['base']
parallax_ch_idx = get_channel_index(parallax_ch)
for i, layer in reversed(list(enumerate(yp.layers))):
#if yp.disable_quick_toggle and (not layer.enable or not layer.channels[parallax_ch_idx].enable): continue
if not layer.enable or not layer.channels[parallax_ch_idx].enable: continue
node = tree.nodes.get(layer.depth_group_node)
uv_names = []
if layer.texcoord_type == 'UV':
uv_names.append(layer.uv_name)
for mask in layer.masks:
if mask.texcoord_type == 'UV' and mask.uv_name not in uv_names:
uv_names.append(mask.uv_name)
for uv_name in uv_names:
inp = node.inputs.get(uv_name + io_suffix['UV'])
uv = yp.uvs.get(uv_name)
if not uv: continue
current_uv = tree.nodes.get(uv.parallax_current_uv)
if inp and current_uv:
create_link(tree, current_uv.outputs[0], inp)
for tc in texcoord_lists:
inp = node.inputs.get(TEXCOORD_IO_PREFIX + tc)
if not inp: continue
current_uv = tree.nodes.get(PARALLAX_CURRENT_PREFIX + TEXCOORD_IO_PREFIX + tc)
if not current_uv: continue
create_link(tree, current_uv.outputs[0], inp)
if layer.parent_idx != -1: continue
height = create_link(tree, height, node.inputs[io_height_name])[io_height_name]
if parallax_ch.enable_smooth_bump:
height = create_link(tree, height, unpack.inputs[0])[0]
create_link(tree, height, normalize.inputs[0])
create_link(tree, normalize.outputs[0], end.inputs['depth_from_tex'])
# List of last members
last_members = []
for layer in yp.layers:
if not layer.enable: continue
if is_bottom_member(layer, True):
last_members.append(layer)
# Remove unused input links
node = tree.nodes.get(layer.depth_group_node)
if layer.type == 'GROUP':
remove_unused_group_node_connections(tree, layer, node) #, height_only=True)
remove_all_prev_inputs(tree, layer, node) #, height_only=True)
# Group stuff
for layer in last_members:
node = tree.nodes.get(layer.depth_group_node)
cur_layer = layer
cur_node = node
io_alpha = cur_node.outputs.get(io_alpha_name)
io_height = cur_node.outputs.get(io_height_name)
io_height_alpha = cur_node.outputs.get(io_height_alpha_name)
while True:
# Get upper layer
upper_idx, upper_layer = get_upper_neighbor(cur_layer)
upper_node = tree.nodes.get(upper_layer.depth_group_node)
# Connect
if upper_layer.parent_idx == cur_layer.parent_idx:
#if not yp.disable_quick_toggle or upper_layer.enable:
if upper_layer.enable:
if io_alpha_name in upper_node.inputs:
if io_alpha:
io_alpha = create_link(tree, io_alpha, upper_node.inputs[io_alpha_name])[io_alpha_name]
else: io_alpha = upper_node.outputs[io_alpha_name]
if io_height_name in upper_node.inputs:
if io_height:
io_height = create_link(tree, io_height, upper_node.inputs[io_height_name])[io_height_name]
else: io_height = upper_node.outputs[io_height_name]
if io_height_alpha_name in upper_node.inputs:
if io_height_alpha:
io_height_alpha = create_link(tree, io_height_alpha, upper_node.inputs[io_height_alpha_name])[io_height_alpha_name]
else: io_height_alpha = upper_node.outputs[io_height_alpha_name]
cur_layer = upper_layer
cur_node = upper_node
else:
io_name = io_alpha_name + io_suffix['GROUP']
if io_alpha and io_name in upper_node.inputs:
#create_link(tree, cur_node.outputs[io_alpha_name], upper_node.inputs[io_name])
create_link(tree, io_alpha, upper_node.inputs[io_name])
io_name = io_height_name + io_suffix['GROUP']
if io_height and io_name in upper_node.inputs:
create_link(tree, io_height, upper_node.inputs[io_name])
io_name = io_height_alpha_name + io_suffix['GROUP']
if io_height_alpha and io_name in upper_node.inputs:
#create_link(tree, cur_node.outputs[io_height_alpha_name], upper_node.inputs[io_name])
create_link(tree, io_height_alpha, upper_node.inputs[io_name])
break
''' Get essential node and if not found, create one '''
def get_essential_node(tree, name):
node = tree.nodes.get(name)
if not node:
if name == TREE_START:
node = tree.nodes.new('NodeGroupInput')
node.name = TREE_START
node.label = 'Start'
elif name == TREE_END:
node = tree.nodes.new('NodeGroupOutput')
node.name = TREE_END
node.label = 'End'
elif name == ONE_VALUE:
node = tree.nodes.new('ShaderNodeValue')
node.name = ONE_VALUE
node.label = 'One Value'
node.outputs[0].default_value = 1.0
elif name == ZERO_VALUE:
node = tree.nodes.new('ShaderNodeValue')
node.name = ZERO_VALUE
node.label = 'Zero Value'
node.outputs[0].default_value = 0.0
elif name == GEOMETRY:
node = tree.nodes.new('ShaderNodeNewGeometry')
node.name = GEOMETRY
elif name == TEXCOORD:
node = tree.nodes.new('ShaderNodeTexCoord')
node.name = TEXCOORD
if name == TREE_END:
return node.inputs
return node.outputs
''' Check for all essential nodes and delete them if no links found '''
def clean_essential_nodes(tree, exclude_texcoord=False, exclude_geometry=False):
for name in [ONE_VALUE, ZERO_VALUE, GEOMETRY, TEXCOORD, TREE_START, TREE_END]:
if exclude_texcoord and name == TEXCOORD: continue
if exclude_geometry and name == GEOMETRY: continue
node = tree.nodes.get(name)
if node:
link_found = False
if len(node.outputs) > 0:
for outp in node.outputs:
if len(outp.links) > 0:
link_found = True
break
elif len(node.inputs) > 0:
for inp in node.inputs:
if len(inp.links) > 0:
link_found = True
break
if not link_found:
tree.nodes.remove(node)
def reconnect_yp_nodes(tree, merged_layer_ids = []):
yp = tree.yp
nodes = tree.nodes
ypup = get_user_preferences()
#print('Reconnect tree ' + tree.name)
start = nodes.get(TREE_START)
end = nodes.get(TREE_END)
texcoord = nodes.get(TEXCOORD)
parallax = tree.nodes.get(PARALLAX)
# Parallax
parallax_ch = get_root_parallax_channel(yp)
parallax = tree.nodes.get(PARALLAX)
baked_parallax = tree.nodes.get(BAKED_PARALLAX)
baked_parallax_filter = tree.nodes.get(BAKED_PARALLAX_FILTER)
# UVs
uv_maps = {}
tangents = {}
bitangents = {}
for uv in yp.uvs:
uv_map = nodes.get(uv.uv_map)
if uv_map:
uv_maps[uv.name] = uv_map.outputs[0]
tangent_process = nodes.get(uv.tangent_process)
if tangent_process:
tangents[uv.name] = tangent_process.outputs['Tangent']
bitangents[uv.name] = tangent_process.outputs['Bitangent']
# Get main tangent and bitangent
height_ch = get_root_height_channel(yp)
main_uv = None
if height_ch and height_ch.main_uv != '':
main_uv = yp.uvs.get(height_ch.main_uv)