-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Mask.py
2186 lines (1724 loc) · 71 KB
/
Mask.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, time, random
from bpy.props import *
from bpy_extras.io_utils import ImportHelper
from . import lib, ImageAtlas, MaskModifier, UDIM
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 add_new_mask(
layer, name, mask_type, texcoord_type, uv_name, image=None, vcol=None, segment=None,
object_index=0, blend_type='MULTIPLY', hemi_space='WORLD', hemi_use_prev_normal=False,
color_id=(1, 0, 1), source_input='RGB', edge_detect_radius=0.05,
modifier_type='INVERT', interpolation='Linear'
):
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.source_input = source_input
# 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':
mask.modifier_type = modifier_type
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')
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: 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
if mask_type == 'OBJECT_INDEX':
source.node_tree = get_node_tree_lib(lib.OBJECT_INDEX_EQUAL)
mask.object_index = object_index
source.inputs[0].default_value = object_index
if mask_type == 'COLOR_ID':
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)
mask.color_id = color_id
col = (color_id[0], color_id[1], color_id[2], 1.0)
source.inputs[0].default_value = col
if mask_type == 'EDGE_DETECT':
source.node_tree = get_node_tree_lib(lib.EDGE_DETECT)
source.inputs[0].default_value = mask.edge_detect_radius = edge_detect_radius
# Enable AO to see edge detect mask
scene = bpy.context.scene
if not scene.eevee.use_gtao: scene.eevee.use_gtao = True
if is_mapping_possible(mask_type):
mask.uv_name = uv_name
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):
ch = layer.channels[i]
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)
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_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):
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
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)
disable_mask_source_tree(layer, mask)
remove_node(tree, mask, 'source')
remove_node(tree, mask, 'baked_source')
remove_node(tree, mask, 'blur_vector')
remove_node(tree, mask, 'separate_color_channels')
remove_node(tree, mask, 'mapping')
remove_node(tree, mask, 'texcoord')
remove_node(tree, mask, 'baked_mapping')
remove_node(tree, mask, 'linear')
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)
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 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 ' + [i[1] for i in mask_type_items if i[0] == mask_type][0]
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)
class YNewLayerMask(bpy.types.Operator):
bl_idname = "node.y_new_layer_mask"
bl_label = "New Layer Mask"
bl_description = "New Layer Mask"
bl_options = {'REGISTER', 'UNDO'}
name : StringProperty(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,
)
hdr : BoolProperty(name='32 bit Float', default=False)
texcoord_type : EnumProperty(
name = 'Mask Coordinate Type',
description = 'Mask Coordinate Type',
items = mask_texcoord_type_items,
default = 'UV'
)
uv_name : StringProperty(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(default=0.05, min=0.0, max=10.0)
vcol_data_type : EnumProperty(
name = 'Vertex Color Data Type',
description = 'Vertex color data type',
items = vcol_data_type_items,
default = 'BYTE_COLOR'
)
vcol_domain : EnumProperty(
name = 'Vertex Color Domain',
description = 'Vertex color 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
# 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 obj.type != 'MESH':
self.texcoord_type = 'Generated'
elif 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 len(layer.masks) == 0:
self.blend_type = 'MULTIPLY'
elif 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)
return context.window_manager.invoke_props_dialog(self, width=320)
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 is_bl_newer_than(3, 2) and self.type == 'VCOL':
col.label(text='Domain:')
col.label(text='Data Type:')
if self.type == 'HEMI':
col.label(text='Space:')
col.label(text='')
if self.type == 'EDGE_DETECT':
col.label(text='Radius:')
if self.type == 'IMAGE':
col.label(text='')
if self.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT', 'MODIFIER'}:
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')
if len(layer.masks) > 0:
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 self.type == 'HEMI':
col.prop(self, 'hemi_space', text='')
col.prop(self, 'hemi_use_prev_normal')
if self.type == 'EDGE_DETECT':
col.prop(self, 'edge_detect_radius', text='')
if is_bl_newer_than(3, 2) and self.type == 'VCOL':
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 self.type == 'IMAGE':
col.prop(self, 'hdr')
if self.type not in {'VCOL', 'HEMI', 'OBJECT_INDEX', 'COLOR_ID', 'BACKFACE', 'EDGE_DETECT', 'MODIFIER'}:
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='')
if len(layer.masks) > 0:
col.prop(self, 'blend_type', text='')
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'}, "Vertex color 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 vertex colors!")
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)
# Check if layer with same name is already available
if self.type == 'IMAGE':
same_name = [i for i in bpy.data.images if i.name == self.name]
elif self.type == 'VCOL':
same_name = [i for i in get_vertex_color_names(obj) if i == self.name]
else: same_name = [m for m in layer.masks if m.name == self.name]
if same_name:
if self.type == 'IMAGE':
self.report({'ERROR'}, "Image named '" + self.name +"' is already available!")
elif self.type == 'VCOL':
self.report({'ERROR'}, "Vertex Color named '" + self.name +"' is already available!")
else: self.report({'ERROR'}, "Mask named '" + self.name +"' is already available!")
return {'CANCELLED'}
alpha = False
img = None
vcol = None
segment = None
# New image
if self.type == 'IMAGE':
if self.color_option == 'WHITE':
color = (1, 1, 1, 1)
elif self.color_option == 'BLACK':
color = (0, 0, 0, 1)
if self.use_udim:
objs = get_all_objects_with_same_materials(mat)
tilenums = UDIM.get_tile_numbers(objs, self.uv_name)
if self.use_image_atlas:
if self.use_udim:
segment = UDIM.get_set_udim_atlas_segment(tilenums, self.width, self.height, color, get_noncolor_name(), self.hdr, yp)
else:
segment = ImageAtlas.get_set_image_atlas_segment(
self.width, self.height, self.color_option, self.hdr, yp=yp
)
img = segment.id_data
else:
if self.use_udim:
img = bpy.data.images.new(
name=self.name, width=self.width, height=self.height,
alpha=alpha, float_buffer=self.hdr, tiled=True
)
# Fill tiles
for tilenum in tilenums:
UDIM.fill_tile(img, tilenum, color, self.width, self.height)
UDIM.initial_pack_udim(img, color)
else:
img = bpy.data.images.new(
name=self.name, width=self.width, height=self.height,
alpha=alpha, float_buffer=self.hdr
)
img.generated_color = color
if hasattr(img, 'use_alpha'):
img.use_alpha = False
if img.colorspace_settings.name != get_noncolor_name() and not img.is_dirty:
img.colorspace_settings.name = get_noncolor_name()
# New vertex color
elif self.type in {'VCOL', 'COLOR_ID'}:
objs = [obj] if obj.type == 'MESH' else []
if mat.users > 1:
for o in get_scene_objects():
if o.type != 'MESH': continue
if mat.name in o.data.materials and o not in objs:
objs.append(o)
if self.type == 'VCOL':
for o in objs:
if self.name not in get_vertex_colors(o):
if not is_bl_newer_than(3, 3) and len(get_vertex_colors(o)) >= 8: continue
vcol = new_vertex_color(o, self.name, self.vcol_data_type, self.vcol_domain)
if self.color_option == 'WHITE':
set_obj_vertex_colors(o, vcol.name, (1.0, 1.0, 1.0, 1.0))
elif self.color_option == 'BLACK':
set_obj_vertex_colors(o, vcol.name, (0.0, 0.0, 0.0, 1.0))
set_active_vertex_color(o, vcol)
elif self.type == 'COLOR_ID':
check_colorid_vcol(objs)
# Voronoi and noise mask will use grayscale value by default
source_input = 'RGB' if self.type not in {'VORONOI', 'NOISE'} else 'ALPHA'
# Add new mask
mask = add_new_mask(
layer, self.name, self.type, self.texcoord_type, self.uv_name, img, vcol, segment, self.object_index, self.blend_type,
self.hemi_space, self.hemi_use_prev_normal, self.color_id, source_input=source_input, edge_detect_radius=self.edge_detect_radius,
modifier_type=self.modifier_type, interpolation=self.interpolation
)
# Enable edit mask
if self.type in {'IMAGE', 'VCOL', 'COLOR_ID'}:
mask.active_edit = True
reconnect_layer_nodes(layer)
rearrange_layer_nodes(layer)
reconnect_yp_nodes(layer.id_data)
rearrange_yp_nodes(layer.id_data)
# Update UI
ypui.layer_ui.expand_masks = True
if self.type not in {'IMAGE', 'VCOL', 'BACKFACE'}:
mask.expand_content = True
mask.expand_source = True
ypui.need_update = True
return {'FINISHED'}
class YOpenImageAsMask(bpy.types.Operator, ImportHelper):
"""Open Image as Mask"""
bl_idname = "node.y_open_image_as_mask"
bl_label = "Open Image as Mask"
bl_options = {'REGISTER', 'UNDO'}
# File related
files : CollectionProperty(type=bpy.types.OperatorFileListElement, options={'HIDDEN', 'SKIP_SAVE'})
directory : StringProperty(maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'})
# File browser filter
filter_folder : BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
filter_image : BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
display_type : EnumProperty(
items = (
('FILE_DEFAULTDISPLAY', 'Default', ''),
('FILE_SHORTDISLPAY', 'Short List', ''),
('FILE_LONGDISPLAY', 'Long List', ''),
('FILE_IMGDISPLAY', 'Thumbnails', '')
),
default = 'FILE_IMGDISPLAY',
options = {'HIDDEN', 'SKIP_SAVE'}
)
relative : BoolProperty(name="Relative Path", default=True, description="Apply relative paths")
interpolation : EnumProperty(
name = 'Image Interpolation Type',
description = 'image interpolation type',
items = interpolation_type_items,
default = 'Linear'
)
texcoord_type : EnumProperty(
name = 'Mask Coordinate Type',
description = 'Mask Coordinate Type',
items = mask_texcoord_type_items,
default = 'UV'
)
uv_map : StringProperty(default='')
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
blend_type : EnumProperty(
name = 'Blend',
description = 'Blend type',
items = mask_blend_type_items,
default = 3 if is_bl_newer_than(2, 90) else None,
)
source_input : EnumProperty(
name = 'Source Input',
description = 'Source data for mask input',
items = (
('RGB', 'Color', ''),
('ALPHA', 'Alpha', '')
),
default = 'RGB'
)
use_udim_detecting : BoolProperty(
name = 'Detect UDIMs',
description = 'Detect selected UDIM files and load all matching tiles.',
default = True
)
file_browser_filepath : StringProperty(default='')
def generate_paths(self):
return (fn.name for fn in self.files), self.directory
@classmethod
def poll(cls, context):
node = get_active_ypaint_node()
return node and len(node.node_tree.yp.layers) > 0
@classmethod
def description(self, context, properties):
return get_operator_description(self)
def invoke(self, context, event):
obj = context.object
if hasattr(context, 'layer'):
self.layer = context.layer
yp = self.layer.id_data.yp
else:
node = get_active_ypaint_node()
yp = node.node_tree.yp
self.layer = yp.layers[yp.active_layer_index]
if obj.type != 'MESH':
self.texcoord_type = 'Object'
# Use active uv layer name by default
if obj.type == 'MESH' and len(obj.data.uv_layers) > 0:
self.uv_map = 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 len(self.layer.masks) == 0:
self.blend_type = 'MULTIPLY'
# Default source input is always color for now
self.source_input = 'RGB'
# Check if there's height channel and use cubic interpolation if there is one
height_ch = get_height_channel(self.layer)
if height_ch and height_ch.enable:
self.interpolation = 'Cubic'
elif self.layer.type == 'IMAGE':
source = get_layer_source(self.layer)
if source and source.image: self.interpolation = source.interpolation
if self.file_browser_filepath != '':
if get_user_preferences().skip_property_popups and not event.shift:
return self.execute(context)
return context.window_manager.invoke_props_dialog(self)
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def check(self, context):
return True
def draw(self, context):
obj = context.object
row = self.layout.row()
col = row.column()
if self.file_browser_filepath != '':
col.label(text='Image:')
col.label(text='Interpolation:')
col.label(text='Vector:')
if len(self.layer.masks) > 0:
col.label(text='Blend:')
col.label(text='Image Channel:')
col = row.column()
if self.file_browser_filepath != '':
col.label(text=os.path.basename(self.file_browser_filepath), icon='IMAGE_DATA')
col.prop(self, 'interpolation', text='')
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_map", obj.data, "uv_layers", text='', icon='GROUP_UVS')
crow.prop_search(self, "uv_map", self, "uv_map_coll", text='', icon='GROUP_UVS')
if len(self.layer.masks) > 0:
col.prop(self, 'blend_type', text='')
crow = col.row(align=True)
crow.prop(self, 'source_input', expand=True)
layout = col if self.file_browser_filepath != '' else self.layout
layout.prop(self, 'relative')
if UDIM.is_udim_supported():
layout.prop(self, 'use_udim_detecting')
def execute(self, context):
T = time.time()
if not hasattr(self, 'layer'): return {'CANCELLED'}
layer = self.layer
yp = layer.id_data.yp
wm = context.window_manager
ypui = wm.ypui
obj = context.object
if self.file_browser_filepath == '':
import_list, directory = self.generate_paths()
else:
if not os.path.isfile(self.file_browser_filepath):
self.report({'ERROR'}, "There's no image with address '" + self.file_browser_filepath + "'!")
return {'CANCELLED'}
import_list = [os.path.basename(self.file_browser_filepath)]
directory = os.path.dirname(self.file_browser_filepath)
if not UDIM.is_udim_supported():
images = tuple(load_image(path, directory) for path in import_list)
else:
ori_ui_type = bpy.context.area.type
bpy.context.area.type = 'IMAGE_EDITOR'
images = []
for path in import_list:
bpy.ops.image.open(
filepath=directory + os.sep + path, directory=directory,
relative_path=self.relative, use_udim_detecting=self.use_udim_detecting
)
image = bpy.context.space_data.image
if image not in images:
images.append(image)
bpy.context.area.type = ori_ui_type
for image in images:
if self.relative and bpy.data.filepath != '':
try: image.filepath = bpy.path.relpath(image.filepath)
except: pass
if image.colorspace_settings.name != get_noncolor_name() and not image.is_dirty:
image.colorspace_settings.name = get_noncolor_name()
# Add new mask
mask = add_new_mask(
layer, image.name, 'IMAGE', self.texcoord_type, self.uv_map, image, None,
blend_type=self.blend_type, source_input=self.source_input,
interpolation = self.interpolation
)
reconnect_layer_nodes(layer)
rearrange_layer_nodes(layer)
reconnect_yp_nodes(layer.id_data)
rearrange_yp_nodes(layer.id_data)
# Update UI
wm.ypui.need_update = True
wm.ypui.layer_ui.expand_masks = True
mask.expand_content = True
mask.expand_vector = True
print('INFO: Image(s) opened as mask(s) in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
wm.yptimer.time = str(time.time())
return {'FINISHED'}
''' Check if data is used as layer, if so, source input will change to ALPHA '''
def update_available_data_name_as_mask(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
if self.type == 'IMAGE':
for layer in yp.layers:
if layer.type == 'IMAGE':
source = get_layer_source(layer)
if source.image and source.image.name == self.image_name:
self.source_input = 'ALPHA'
return
elif self.type == 'VCOL' and is_bl_newer_than(2, 92):
for layer in yp.layers:
if layer.type == 'VCOL':
source = get_layer_source(layer)
if source.attribute_name == self.vcol_name:
self.source_input = 'ALPHA'
return
self.source_input = 'RGB'
class YOpenAvailableDataAsMask(bpy.types.Operator):
bl_idname = "node.y_open_available_data_as_mask"
bl_label = "Open available data as Layer Mask"
bl_description = "Open available data as Layer Mask"
bl_options = {'REGISTER', 'UNDO'}
type : EnumProperty(
name = 'Layer Type',
items = (
('IMAGE', 'Image', ''),
('VCOL', 'Vertex Color', '')
),
default = 'IMAGE'
)
interpolation : EnumProperty(
name = 'Image Interpolation Type',
description = 'image interpolation type',