-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathMask.py
More file actions
2780 lines (2180 loc) · 90.9 KB
/
Mask.py
File metadata and controls
2780 lines (2180 loc) · 90.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
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, time, random
from bpy.props import *
from bpy_extras.io_utils import ImportHelper
from . import lib, ImageAtlas, MaskModifier, UDIM, ListItem, BaseOperator, Decal
from .common import *
from .node_connections import *
from .node_arrangements import *
from .subtree import *
from .input_outputs import *
#def check_object_index_props(entity, source=None):
# source.inputs[0].default_value = entity.object_index
def setup_color_id_source(mask, source, color_id=None):
if is_bl_newer_than(2, 82):
source.node_tree = get_node_tree_lib(lib.COLOR_ID_EQUAL_282)
else: source.node_tree = get_node_tree_lib(lib.COLOR_ID_EQUAL)
if color_id != None:
mask.color_id = color_id
else: color_id = mask.color_id
col = (color_id[0], color_id[1], color_id[2], 1.0)
source.inputs[0].default_value = col
def setup_object_idx_source(mask, source, object_index=None):
source.node_tree = get_node_tree_lib(lib.OBJECT_INDEX_EQUAL)
if object_index != None:
mask.object_index = object_index
else: object_index = mask.object_index
source.inputs[0].default_value = object_index
def setup_edge_detect_source(entity, source, edge_detect_radius=None, edge_detect_method=None):
yp = entity.id_data.yp
if edge_detect_method == None:
edge_detect_method = entity.edge_detect_method
elif entity.edge_detect_method != edge_detect_method:
ori_halt_update = yp.halt_update
yp.halt_update = True
entity.edge_detect_method = edge_detect_method
yp.halt_update = ori_halt_update
if edge_detect_method == 'CROSS':
if entity.hemi_use_prev_normal:
lib_name = lib.EDGE_DETECT_CUSTOM_NORMAL
else: lib_name = lib.EDGE_DETECT
else:
if entity.hemi_use_prev_normal:
lib_name = lib.EDGE_DETECT_CUSTOM_NORMAL_DOT
else: lib_name = lib.EDGE_DETECT_DOT
ori_lib = source.node_tree
if not ori_lib or ori_lib.name != lib_name:
source.node_tree = get_node_tree_lib(lib_name)
if ori_lib and ori_lib.users == 0:
remove_datablock(bpy.data.node_groups, ori_lib)
if edge_detect_radius != None:
source.inputs[0].default_value = entity.edge_detect_radius = edge_detect_radius
else: source.inputs[0].default_value = entity.edge_detect_radius
enable_eevee_ao()
def setup_modifier_mask_source(tree, mask, modifier_type):
source = None
if modifier_type == 'INVERT':
source = new_node(tree, mask, 'source', 'ShaderNodeInvert', 'Mask Source')
elif modifier_type == 'RAMP':
source = new_node(tree, mask, 'source', 'ShaderNodeValToRGB', 'Mask Source')
#ramp_mix = new_mix_node(tree, mask, 'ramp_mix', 'Ramp Mix', 'FLOAT')
elif modifier_type == 'CURVE':
source = new_node(tree, mask, 'source', 'ShaderNodeRGBCurve', 'Mask Source')
return source
def add_new_mask(
layer, name, mask_type, texcoord_type, uv_name,
image=None, vcol_name='', segment=None,
object_index=0, blend_type='MULTIPLY', hemi_space='WORLD', hemi_use_prev_normal=False,
color_id=(1, 0, 1), edge_detect_radius=0.05, edge_detect_method='CROSS',
modifier_type='INVERT', interpolation='Linear', ao_distance=1.0, socket_input_name='Color'
):
yp = layer.id_data.yp
yp.halt_update = True
ypup = get_user_preferences()
tree = get_tree(layer)
nodes = tree.nodes
mask = layer.masks.add()
mask.name = get_unique_name(name, layer.masks)
mask.type = mask_type
mask.texcoord_type = texcoord_type
mask.socket_input_name = socket_input_name
# Uniform Scale
if is_bl_newer_than(2, 81) and is_mask_using_vector(mask):
mask.enable_uniform_scale = ypup.enable_uniform_uv_scale_by_default
if segment:
mask.segment_name = segment.name
source = None
if mask_type == 'VCOL':
source = new_node(tree, mask, 'source', get_vcol_bl_idname(), 'Mask Source')
elif mask_type == 'MODIFIER':
source = setup_modifier_mask_source(tree, mask, modifier_type)
mask.modifier_type = modifier_type
elif mask.type != 'BACKFACE': source = new_node(tree, mask, 'source', layer_node_bl_idnames[mask_type], 'Mask Source')
if image:
source.image = image
if hasattr(source, 'color_space'):
source.color_space = 'NONE'
source.interpolation = interpolation
elif mask_type == 'VCOL':
if vcol_name != '': set_source_vcol_name(source, vcol_name)
else: set_source_vcol_name(source, name)
if mask_type == 'HEMI':
source.node_tree = get_node_tree_lib(lib.HEMI)
duplicate_lib_node_tree(source)
mask.hemi_space = hemi_space
mask.hemi_use_prev_normal = hemi_use_prev_normal
elif mask_type == 'OBJECT_INDEX':
setup_object_idx_source(mask, source, object_index)
elif mask_type == 'COLOR_ID':
setup_color_id_source(mask, source, color_id)
elif mask_type == 'EDGE_DETECT':
mask.hemi_use_prev_normal = hemi_use_prev_normal
setup_edge_detect_source(mask, source, edge_detect_radius, edge_detect_method)
elif mask_type == 'AO':
mask.hemi_use_prev_normal = hemi_use_prev_normal
mask.ao_distance = ao_distance
enable_eevee_ao()
# Set default uv name if it's an empty string
if uv_name == '':
uv_name = get_default_uv_name()
mask.uv_name = uv_name
if is_mapping_possible(mask_type):
mapping = new_node(tree, mask, 'mapping', 'ShaderNodeMapping', 'Mask Mapping')
mapping.vector_type = 'POINT' if segment else 'TEXTURE'
if segment:
ImageAtlas.set_segment_mapping(mask, segment, image)
refresh_temp_uv(bpy.context.object, mask)
for i, root_ch in enumerate(yp.channels):
c = mask.channels.add()
mask.blend_type = blend_type
# Check mask multiplies
check_mask_mix_nodes(layer, tree)
# Check mask source tree
check_mask_source_tree(layer)
# Check the need of bump process
check_layer_bump_process(layer, tree)
# Check uv maps
check_uv_nodes(yp)
# Check layer io
check_all_layer_channel_io_and_nodes(layer, tree)
# Check mask linear
check_mask_image_linear_node(mask)
yp.halt_update = False
# Update coords
update_mask_texcoord_type(mask, None, False)
# Update list items
ListItem.refresh_list_items(yp)
return mask
def remove_mask_channel_nodes(tree, c):
remove_node(tree, c, 'mix')
remove_node(tree, c, 'mix_n')
remove_node(tree, c, 'mix_s')
remove_node(tree, c, 'mix_e')
remove_node(tree, c, 'mix_w')
remove_node(tree, c, 'mix_pure')
remove_node(tree, c, 'mix_remains')
remove_node(tree, c, 'mix_normal')
remove_node(tree, c, 'mix_vdisp')
remove_node(tree, c, 'mix_limit')
remove_node(tree, c, 'mix_limit_normal')
def remove_mask_channel(tree, layer, ch_index):
# Remove mask nodes
for mask in layer.masks:
# Get channels
c = mask.channels[ch_index]
ch = layer.channels[ch_index]
# Remove mask channel nodes first
remove_mask_channel_nodes(tree, c)
# Remove the mask itself
for mask in layer.masks:
mask.channels.remove(ch_index)
def remove_mask(layer, mask, obj, refresh_list=True):
tree = get_tree(layer)
yp = layer.id_data.yp
mat = obj.active_material
# Get mask index
mask_index = [i for i, m in enumerate(layer.masks) if m == mask][0]
# Dealing with decal object
Decal.remove_decal_object(tree, mask)
# Remove mask fcurves first
remove_entity_fcurves(mask)
shift_mask_fcurves_up(layer, mask_index)
# Dealing with image atlas segments
if mask.type == 'IMAGE':
src = get_mask_source(mask)
if src and src.image:
image = src.image
if mask.segment_name != '':
if image.yia.is_image_atlas:
segment = image.yia.segments.get(mask.segment_name)
segment.unused = True
elif image.yua.is_udim_atlas:
print('ZEGMENT:', mask.segment_name)
UDIM.remove_udim_atlas_segment_by_name(image, mask.segment_name, yp=yp)
group_node = tree.nodes.get(mask.group_node)
if group_node:
stree = group_node.node_tree
else: stree = tree
remove_node(stree, mask, 'source')
remove_node(stree, mask, 'baked_source')
remove_node(stree, mask, 'linear')
remove_node(stree, mask, 'separate_color_channels')
remove_node(tree, mask, 'group_node')
remove_node(tree, mask, 'blur_vector')
remove_node(tree, mask, 'mapping')
remove_node(tree, mask, 'texcoord')
remove_node(tree, mask, 'baked_mapping')
remove_node(tree, mask, 'uv_map')
remove_node(tree, mask, 'uv_neighbor')
# Remove mask modifiers
for m in mask.modifiers:
MaskModifier.delete_modifier_nodes(tree, m)
# Remove mask channel nodes
for c in mask.channels:
remove_mask_channel_nodes(tree, c)
# Remove mask
layer.masks.remove(mask_index)
# Update list items
if refresh_list:
ListItem.refresh_list_items(yp)
def get_new_mask_name(obj, layer, mask_type, modifier_type=''):
surname = '(' + layer.name + ')'
items = layer.masks
if mask_type == 'IMAGE':
name = 'Mask'
name = get_unique_name(name, layer.masks, surname)
name = get_unique_name(name, bpy.data.images)
return name
elif mask_type == 'VCOL' and obj.type == 'MESH':
name = 'Mask Attribute' if is_bl_newer_than(3, 2) else 'Mask VCol'
items = get_vertex_color_names(obj)
return get_unique_name(name, items, surname)
elif mask_type == 'MODIFIER':
name = 'Mask ' + modifier_type.title()
return get_unique_name(name, items, surname)
else:
name = 'Mask ' + mask_type_labels[mask_type]
return get_unique_name(name, items, surname)
def update_new_mask_uv_map(self, context):
if not UDIM.is_udim_supported(): return
if self.type != 'IMAGE':
self.use_udim = False
return
if get_user_preferences().enable_auto_udim_detection:
mat = get_active_material()
objs = get_all_objects_with_same_materials(mat)
self.use_udim = UDIM.is_uvmap_udim(objs, self.uv_name)
def get_mask_cache_name(mask_type, modifier_type=''):
name = 'cache_' + mask_type.lower()
if mask_type == 'MODIFIER':
name += '_' + modifier_type.lower()
return name
def is_mask_type_cacheable(mask_type, modifier_type=''):
if mask_type == 'MODIFIER':
return modifier_type in {'RAMP', 'CURVE'}
return mask_type not in {'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'EDGE_DETECT', 'BACKFACE', 'AO'}
def replace_mask_type(mask, new_type, item_name='', remove_data=False, modifier_type='INVERT'):
yp = mask.id_data.yp
match = re.match(r'yp\.layers\[(\d+)\]\.masks\[(\d+)\]$', mask.path_from_id())
layer = yp.layers[int(match.group(1))]
# Check if mask is using image atlas
if mask.type == 'IMAGE' and mask.segment_name != '':
# Replace to non atlas image will remove the segment
if new_type == 'IMAGE':
src = get_mask_source(mask)
if src.image.yia.is_image_atlas:
segment = src.image.yia.segments.get(mask.segment_name)
segment.unused = True
elif src.image.yua.is_udim_atlas:
UDIM.remove_udim_atlas_segment_by_name(src.image, mask.segment_name, yp=yp)
# Set segment name to empty
mask.segment_name = ''
# Reset mapping
clear_mapping(mask)
# Save hemi vector
if mask.type == 'HEMI':
src = get_mask_source(mask)
save_hemi_props(mask, src)
yp.halt_reconnect = True
# Standard bump map is easier to convert
fine_bump_channels = [ch for ch in yp.channels if ch.enable_smooth_bump]
for ch in fine_bump_channels:
ch.enable_smooth_bump = False
# Disable transition will also helps
transition_channels = [ch for ch in layer.channels if ch.enable_transition_bump]
for ch in transition_channels:
ch.enable_transition_bump = False
# Current source
tree = get_mask_tree(mask)
source = get_mask_source(mask)
# Save source to cache if it's not image, vertex color, or background
if is_mask_type_cacheable(mask.type, mask.modifier_type):
setattr(mask, get_mask_cache_name(mask.type, mask.modifier_type), source.name)
# Remove uv input link
if any(source.inputs) and any(source.inputs[0].links):
tree.links.remove(source.inputs[0].links[0])
source.label = ''
else:
# Remember values by disabling then enabling the mask again
if mask.enable:
mask.enable = False
mask.enable = True
remove_node(tree, mask, 'source', remove_data=remove_data)
# Disable modifier tree
#if is_mask_type_cacheable(mask.type, mask.modifier_type) and is_mask_type_cacheable(new_type, modifier_type):
# Modifier.disable_modifiers_tree(mask)
# Try to get available cache
cache = None
if is_mask_type_cacheable(new_type, modifier_type) and mask.type != new_type:
cache = tree.nodes.get(getattr(mask, get_mask_cache_name(new_type, modifier_type)))
if cache:
mask.source = cache.name
setattr(mask, get_mask_cache_name(new_type, modifier_type), '')
cache.label = 'Source'
else:
if new_type == 'MODIFIER':
source = setup_modifier_mask_source(tree, mask, modifier_type)
elif new_type != 'BACKFACE': source = new_node(tree, mask, 'source', layer_node_bl_idnames[new_type], 'Source')
if new_type == 'IMAGE':
image = bpy.data.images.get(item_name)
source.image = image
check_mask_image_projections(mask, source)
if mask.texcoord_type == 'Decal':
source.extension = 'CLIP'
if hasattr(source, 'color_space'):
source.color_space = 'NONE'
if image.colorspace_settings.name != get_noncolor_name() and not image.is_dirty:
image.colorspace_settings.name = get_noncolor_name()
elif new_type == 'VCOL':
set_source_vcol_name(source, item_name)
elif new_type == 'HEMI':
source.node_tree = get_node_tree_lib(lib.HEMI)
duplicate_lib_node_tree(source)
load_hemi_props(mask, source)
elif new_type == 'COLOR_ID':
mat = get_active_material()
objs = get_all_objects_with_same_materials(mat)
check_colorid_vcol(objs, set_as_active=True)
setup_color_id_source(mask, source)
elif new_type == 'OBJECT_INDEX':
setup_object_idx_source(mask, source)
elif new_type == 'EDGE_DETECT':
setup_edge_detect_source(mask, source)
elif new_type == 'AO':
enable_eevee_ao()
# Change mask type
ori_type = mask.type
mask.type = new_type
# Change mask modifier type
if mask.type == 'MODIFIER':
mask.modifier_type = modifier_type
# Set up mapping
mapping = tree.nodes.get(mask.mapping)
if is_mapping_possible(new_type):
if not mapping:
mapping = new_node(tree, mask, 'mapping', 'ShaderNodeMapping', 'Mask Mapping')
else:
remove_node(tree, mask, 'mapping')
# Update mask name
image = None
if mask.type == 'IMAGE':
# Rename mask with image name
source = get_mask_source(mask)
if source and source.image:
image = source.image
yp.halt_update = True
if image.yia.is_image_atlas or image.yua.is_udim_atlas:
new_name = 'Mask (' + layer.name + ')'
# Set back the mapping
if image.yia.is_image_atlas:
segment = image.yia.segments.get(mask.segment_name)
ImageAtlas.set_segment_mapping(mask, segment, image)
else:
segment = image.yua.segments.get(mask.segment_name)
UDIM.set_udim_segment_mapping(mask, segment, image)
else: new_name = image.name
mask.name = get_unique_name(new_name, layer.masks)
yp.halt_update = False
# Set interpolation to Cubic if normal/height channel is found
height_ch = get_height_channel(mask)
if height_ch and height_ch.enable:
source.interpolation = 'Cubic'
elif mask.type == 'VCOL':
# Rename mask with vcol name
source = get_mask_source(mask)
if source: mask.name = get_unique_name(source.attribute_name, layer.masks)
# Set active vertex color
set_active_vertex_color_by_name(bpy.context.object, source.attribute_name)
elif mask.type == 'MODIFIER':
# Rename mask with modifier types
mask.name = get_unique_name(MaskModifier.mask_modifier_type_labels[mask.modifier_type], layer.masks)
elif ori_type in {'IMAGE', 'VCOL'}:
# Rename mask with texture types
mask.name = get_unique_name(mask_type_labels[mask.type], layer.masks)
elif mask_type_labels[ori_type] in mask.name:
# Rename texture types with another texture types
mask.name = get_unique_name(mask.name.replace(mask_type_labels[ori_type], mask_type_labels[mask.type]), layer.masks)
# Enable modifiers tree if generated texture is used
#if mask.type not in {'IMAGE', 'VCOL', 'BACKGROUND'}:
# Modifier.enable_modifiers_tree(mask)
#Modifier.check_modifiers_trees(mask)
# Set default UV name when necessary
if is_mapping_possible(mask.type) and mask.uv_name == '':
obj = bpy.context.object
if obj and obj.type == 'MESH' and len(obj.data.uv_layers) > 0:
yp.halt_update = True
mask.uv_name = get_default_uv_name(obj, yp)
yp.halt_update = False
# Always remove baked mask when changing type
if mask.use_baked:
mask.use_baked = False
remove_node(tree, mask, 'baked_source')
# Update group ios
check_all_layer_channel_io_and_nodes(layer, tree)
# Update linear stuff
#for i, ch in enumerate(mask.channels):
# root_ch = yp.channels[i]
# set_layer_channel_linear_node(tree, mask, root_ch, ch)
# Back to use fine bump if conversion happen
for ch in fine_bump_channels:
ch.enable_smooth_bump = True
# Bring back transition
for ch in transition_channels:
ch.enable_transition_bump = True
# Update uv neighbor
#set_uv_neighbor_resolution(mask)
yp.halt_reconnect = False
# Check uv maps
check_uv_nodes(yp)
# Check children which need rearrange
#for i in child_ids:
#lay = yp.layers[i]
#for lay in yp.layers:
# check_all_layer_channel_io_and_nodes(lay)
# reconnect_layer_nodes(lay)
# rearrange_layer_nodes(lay)
for lay in yp.layers:
check_all_layer_channel_io_and_nodes(lay)
reconnect_layer_nodes(lay)
rearrange_layer_nodes(lay)
#reconnect_layer_nodes(layer)
#rearrange_layer_nodes(layer)
#if mask.type in {'BACKGROUND', 'GROUP'} or ori_type == 'GROUP':
reconnect_yp_nodes(mask.id_data)
rearrange_yp_nodes(mask.id_data)
# Update UI
bpy.context.window_manager.ypui.need_update = True
mask.expand_source = mask.type not in {'IMAGE'} or (image != None and image.y_bake_info.is_baked and not image.y_bake_info.is_baked_channel)
class YNewLayerMask(bpy.types.Operator):
bl_idname = "wm.y_new_layer_mask"
bl_label = "New Layer Mask"
bl_description = "New Layer Mask"
bl_options = {'REGISTER', 'UNDO'}
name : StringProperty(name='Mask Name', default='')
type : EnumProperty(
name = 'Mask Type',
items = mask_type_items,
default = 'IMAGE'
)
modifier_type : EnumProperty(
name = 'Mask Modifier Type',
items = MaskModifier.mask_modifier_type_items,
default = 'INVERT'
)
width : IntProperty(name='Width', default=1024, min=1, max=16384)
height : IntProperty(name='Height', default=1024, min=1, max=16384)
interpolation : EnumProperty(
name = 'Image Interpolation Type',
description = 'image interpolation type',
items = interpolation_type_items,
default = 'Linear'
)
blend_type : EnumProperty(
name = 'Blend',
description = 'Blend type',
items = mask_blend_type_items,
default = 3 if is_bl_newer_than(2, 90) else None,
)
color_option : EnumProperty(
name = 'Color Option',
description = 'Color Option',
items = (
('WHITE', 'White (Full Opacity)', ''),
('BLACK', 'Black (Full Transparency)', ''),
),
default='WHITE'
)
color_id : FloatVectorProperty(
name = 'Color ID',
size = 3,
subtype = 'COLOR',
default=(1.0, 0.0, 1.0), min=0.0, max=1.0,
)
vcol_fill : BoolProperty(
name = 'Fill Selected Geometry with '+get_vertex_color_label()+' / Color ID',
description = 'Fill selected geometry with '+get_vertex_color_label(00)+' / color ID',
default = True
)
hdr : BoolProperty(
name = '32-bit Float',
description = 'Use 32-bit float image',
default = False
)
texcoord_type : EnumProperty(
name = 'Mask Coordinate Type',
description = 'Mask Coordinate Type',
items = mask_texcoord_type_items,
default = 'UV'
)
uv_name : StringProperty(
name = 'UV Map',
description = 'UV Map to use for mask coordinate',
default = '',
update = update_new_mask_uv_map
)
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
use_udim : BoolProperty(
name = 'Use UDIM Tiles',
description = 'Use UDIM Tiles',
default = False
)
use_image_atlas : BoolProperty(
name = 'Use Image Atlas',
description = 'Use Image Atlas',
default = False
)
# For fake lighting
hemi_space : EnumProperty(
name = 'Fake Lighting Space',
description = 'Fake lighting space',
items = hemi_space_items,
default = 'WORLD'
)
hemi_use_prev_normal : BoolProperty(
name = 'Use previous Normal',
description = 'Take previous Normal into the account',
default = True
)
# For object index
object_index : IntProperty(
name = 'Object Index',
description = 'Object Pass Index',
default=0, min=0
)
edge_detect_radius : FloatProperty(
name = 'Edge Detect Radius',
description = 'Edge detect radius',
default=0.05, min=0.0, max=10.0
)
ao_distance : FloatProperty(
name = 'Ambient Occlusion Distance',
description = 'Ambient occlusion distance',
default=1.0, min=0.0, max=10.0
)
vcol_data_type : EnumProperty(
name = get_vertex_color_label()+' Data Type',
description = get_vertex_color_label(10)+' data type',
items = vcol_data_type_items,
default = 'BYTE_COLOR'
)
vcol_domain : EnumProperty(
name = get_vertex_color_label()+' Domain',
description = get_vertex_color_label(10)+' domain',
items = vcol_domain_items,
default = 'CORNER'
)
image_resolution : EnumProperty(
name = 'Image Resolution',
items = image_resolution_items,
default = '1024'
)
use_custom_resolution : BoolProperty(
name = 'Custom Resolution',
default = False,
description = 'Use custom Resolution to adjust the width and height individually'
)
@classmethod
def poll(cls, context):
return True
@classmethod
def description(self, context, properties):
return get_operator_description(self)
def get_to_be_cleared_image_atlas(self, context, yp):
if self.type == 'IMAGE' and self.use_image_atlas:
return ImageAtlas.check_need_of_erasing_segments(yp, self.color_option, self.width, self.height, self.hdr)
return None
def invoke(self, context, event):
node = get_active_ypaint_node()
yp = node.node_tree.yp
obj = context.object
layer = get_active_layer(yp)
self.auto_cancel = False
if not layer:
self.auto_cancel = True
return self.execute(context)
yp = layer.id_data.yp
ypup = get_user_preferences()
self.name = get_new_mask_name(obj, layer, self.type, self.modifier_type)
# Use user preference default image size
if ypup.default_image_resolution == 'CUSTOM':
self.use_custom_resolution = True
self.width = self.height = ypup.default_new_image_size
elif ypup.default_image_resolution != 'DEFAULT':
self.image_resolution = ypup.default_image_resolution
if self.type == 'COLOR_ID':
# Check if color id already being used
while True:
# Use color id tolerance value as lowest value to avoid pure black color
self.color_id = (random.uniform(COLORID_TOLERANCE, 1.0), random.uniform(COLORID_TOLERANCE, 1.0), random.uniform(COLORID_TOLERANCE, 1.0))
if not is_colorid_already_being_used(yp, self.color_id): break
# Disable use previous normal for edge detect since it has very little effect
if self.type == 'EDGE_DETECT':
self.hemi_use_prev_normal = False
# Make sure decal is off when adding non mappable mask
if not is_mapping_possible(self.type) and self.texcoord_type == 'Decal':
self.texcoord_type = 'UV'
if not is_object_work_with_uv(obj):
self.texcoord_type = 'Generated'
if obj.type == 'MESH' and len(obj.data.uv_layers) > 0:
self.uv_name = get_default_uv_name(obj, yp)
# UV Map collections update
self.uv_map_coll.clear()
for uv in obj.data.uv_layers:
if not uv.name.startswith(TEMP_UV):
self.uv_map_coll.add().name = uv.name
# The default blend type for mask is multiply
if self.type in {'MODIFIER'}:
self.blend_type = 'MIX'
else:
self.blend_type = 'MULTIPLY'
# Check if there's height channel and use cubic interpolation if there is one
height_ch = get_height_channel(layer)
if height_ch and height_ch.enable and self.type == 'IMAGE':
self.interpolation = 'Cubic'
elif layer.type == 'IMAGE':
source = get_layer_source(layer)
if source and source.image: self.interpolation = source.interpolation
if get_user_preferences().skip_property_popups and not event.shift:
return self.execute(context)
width = 320
if self.type == 'EDGE_DETECT':
width = 370
return context.window_manager.invoke_props_dialog(self, width=width)
def check(self, context):
ypup = get_user_preferences()
if not self.use_custom_resolution:
self.height = self.width = int(self.image_resolution)
# New image cannot use more pixels than the image atlas
if self.use_image_atlas:
if self.hdr: max_size = ypup.hdr_image_atlas_size
else: max_size = ypup.image_atlas_size
if self.width > max_size: self.width = max_size
if self.height > max_size: self.height = max_size
return True
def draw(self, context):
obj = context.object
node = get_active_ypaint_node()
yp = node.node_tree.yp
layer = get_active_layer(yp)
row = split_layout(self.layout, 0.4)
col = row.column(align=False)
col.label(text='Name:')
if self.type == 'IMAGE' and self.use_custom_resolution == False:
col.label(text='')
col.label(text='Resolution:')
elif self.type == 'IMAGE' and self.use_custom_resolution == True:
col.label(text='')
col.label(text='Width:')
col.label(text='Height:')
if self.type == 'IMAGE':
col.label(text='Interpolation:')
if self.type in {'VCOL', 'IMAGE'}:
col.label(text='Color:')
if self.type == 'COLOR_ID':
col.label(text='Color ID:')
if obj.mode == 'EDIT':
col.label(text='')
if self.type == 'VCOL':
if is_bl_newer_than(3, 2):
col.label(text='Domain:')
col.label(text='Data Type:')
if obj.mode == 'EDIT' and self.color_option == 'BLACK':
col.label(text='')
if self.type == 'HEMI':
col.label(text='Space:')
if self.type == 'EDGE_DETECT':
col.label(text='Radius:')
if self.type == 'AO':
col.label(text='AO Distance:')
if self.type in {'HEMI', 'EDGE_DETECT', 'AO'}:
col.label(text='')
if self.type == 'IMAGE':
col.label(text='')
if self.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT', 'MODIFIER', 'AO'}:
col.label(text='Vector:')
if self.type == 'IMAGE':
if UDIM.is_udim_supported():
col.label(text='')
col.label(text='')
if self.type == 'OBJECT_INDEX':
col.label(text='Object Index')
col.label(text='Blend:')
col = row.column(align=False)
col.prop(self, 'name', text='')
if self.type == 'IMAGE' and self.use_custom_resolution == False:
crow = col.row(align=True)
crow.prop(self, 'use_custom_resolution')
crow = col.row(align=True)
crow.prop(self, 'image_resolution', expand= True,)
elif self.type == 'IMAGE' and self.use_custom_resolution == True:
crow = col.row(align=True)
crow.prop(self, 'use_custom_resolution')
col.prop(self, 'width', text='')
col.prop(self, 'height', text='')
if self.type == 'IMAGE':
col.prop(self, 'interpolation', text='')
if self.type in {'VCOL', 'IMAGE'}:
col.prop(self, 'color_option', text='')
if self.type == 'COLOR_ID':
col.prop(self, 'color_id', text='')
if obj.mode == 'EDIT':
col.prop(self, 'vcol_fill', text='Fill Selected Faces')
if self.type == 'HEMI':
col.prop(self, 'hemi_space', text='')
if self.type == 'EDGE_DETECT':
col.prop(self, 'edge_detect_radius', text='')
if self.type == 'AO':
col.prop(self, 'ao_distance', text='')
if self.type in {'HEMI', 'EDGE_DETECT', 'AO'}:
col.prop(self, 'hemi_use_prev_normal')
if self.type == 'VCOL':
if is_bl_newer_than(3, 2):
crow = col.row(align=True)
crow.prop(self, 'vcol_domain', expand=True)
crow = col.row(align=True)
crow.prop(self, 'vcol_data_type', expand=True)
if obj.mode == 'EDIT' and self.color_option == 'BLACK':
col.prop(self, 'vcol_fill', text='Fill Selected Faces')
if self.type == 'IMAGE':
col.prop(self, 'hdr')
if self.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT', 'MODIFIER', 'AO'}:
crow = col.row(align=True)
crow.prop(self, 'texcoord_type', text='')
if obj.type == 'MESH' and self.texcoord_type == 'UV':
crow.prop_search(self, "uv_name", self, "uv_map_coll", text='', icon='GROUP_UVS')
if self.type == 'IMAGE':
if UDIM.is_udim_supported():
col.prop(self, 'use_udim')
ccol = col.column()
ccol.prop(self, 'use_image_atlas')
if self.get_to_be_cleared_image_atlas(context, yp):
col = self.layout.column(align=True)
col.label(text='INFO: An unused atlas segment can be used.', icon='ERROR')
col.label(text='It will take a couple seconds to clear.')
if self.type == 'OBJECT_INDEX':
col.prop(self, 'object_index', text='')
col.prop(self, 'blend_type', text='')
if self.type == 'AO':
col = self.layout.column(align=True)
col.label(text='Realtime AO can look different in baked/rendered view!', icon='ERROR')
elif self.type == 'EDGE_DETECT':
col = self.layout.column(align=True)
col.label(text='Realtime Edge Detect can look different in baked/rendered view!', icon='ERROR')
elif self.type == 'BACKFACE':
col = self.layout.column(align=True)
col.label(text='Backface mask can\'t be baked!', icon='ERROR')
def execute(self, context):
if hasattr(self, 'auto_cancel') and self.auto_cancel: return {'CANCELLED'}
obj = context.object
mat = obj.active_material
ypui = context.window_manager.ypui
node = get_active_ypaint_node()
yp = node.node_tree.yp
layer = get_active_layer(yp)
# Check if object is not a mesh
if self.type == 'VCOL' and obj.type != 'MESH':
self.report({'ERROR'}, get_vertex_color_label(10)+" mask only works with mesh object!")
return {'CANCELLED'}
if not is_bl_newer_than(3, 3) and self.type == 'VCOL' and len(get_vertex_color_names(obj)) >= 8:
self.report({'ERROR'}, "Mesh can only use 8 "+get_vertex_color_label(00)+"s!")
return {'CANCELLED'}
# Clearing unused image atlas segments
img_atlas = self.get_to_be_cleared_image_atlas(context, yp)
if img_atlas: ImageAtlas.clear_unused_segments(img_atlas.yia)