-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Layer.py
6797 lines (5405 loc) · 237 KB
/
Layer.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, time, re, os, random, numpy
from bpy.props import *
from bpy_extras.io_utils import ImportHelper
from . import Modifier, lib, Mask, transition, ImageAtlas, UDIM, NormalMapModifier
from .common import *
#from .bake_common import *
from .node_arrangements import *
from .node_connections import *
from .subtree import *
from .input_outputs import *
DEFAULT_NEW_IMG_SUFFIX = ' Layer'
DEFAULT_NEW_VCOL_SUFFIX = ' VCol'
DEFAULT_NEW_VDM_SUFFIX = ' VDM'
def channel_items(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
items = []
for i, ch in enumerate(yp.channels):
# Add two spaces to prevent text from being translated
text_ch_name = ch.name + ' '
icon_name = lib.channel_custom_icon_dict[ch.type]
items.append((str(i), text_ch_name, '', lib.get_icon(icon_name), i))
items.append(('-1', 'All Channels', '', lib.get_icon('channels'), len(items)))
return items
def get_normal_map_type_items(self, context):
items = []
if is_bl_newer_than(2, 80):
items.append(('BUMP_MAP', 'Bump Map', ''))
items.append(('NORMAL_MAP', 'Normal Map', ''))
items.append(('BUMP_NORMAL_MAP', 'Bump + Normal Map', ''))
items.append(('VECTOR_DISPLACEMENT_MAP', 'Vector Displacement Map', ''))
else:
items.append(('BUMP_MAP', 'Bump Map', '', 'MATCAP_09', 0))
items.append(('NORMAL_MAP', 'Normal Map', '', 'MATCAP_23', 1))
items.append(('BUMP_NORMAL_MAP', 'Bump + Normal Map', '', 'MATCAP_23', 2))
items.append(('VECTOR_DISPLACEMENT_MAP', 'Vector Displacement Map', '', 'MATCAP_23', 3))
return items
def load_hemi_props(layer, source):
norm = source.node_tree.nodes.get('Normal')
if norm: norm.outputs[0].default_value = layer.hemi_vector
trans = source.node_tree.nodes.get('Vector Transform')
if trans: trans.convert_from = layer.hemi_space
def add_new_layer(
group_tree, layer_name, layer_type, channel_idx,
blend_type, normal_blend_type, normal_map_type,
texcoord_type, uv_name='', image=None, vcol=None, segment=None,
solid_color=(1, 1, 1),
add_mask=False, mask_type='IMAGE', mask_image_filepath='', mask_relative=True,
mask_texcoord_type='UV', mask_color='BLACK', mask_use_hdr=False,
mask_uv_name='', mask_width=1024, mask_height=1024, use_image_atlas_for_mask=False,
hemi_space='WORLD', hemi_use_prev_normal=True,
mask_color_id=(1, 0, 1), mask_vcol_data_type='BYTE_COLOR', mask_vcol_domain='CORNER',
use_divider_alpha=False, use_udim_for_mask=False,
interpolation='Linear', mask_interpolation='Linear', mask_edge_detect_radius=0.05
):
yp = group_tree.yp
ypup = get_user_preferences()
obj = bpy.context.object
mat = obj.active_material
# Halt rearrangements and reconnections until all nodes already created
yp.halt_reconnect = True
#yp.halt_update = True
# Get parent and index dict
parent_dict = get_parent_dict(yp)
index_dict = get_index_dict(yp)
# Get active layer
try: active_layer = yp.layers[yp.active_layer_index]
except: active_layer = None
# Get a possible parent layer group
parent_layer = None
if active_layer:
if active_layer.type == 'GROUP':
parent_layer = active_layer
elif active_layer.parent_idx != -1:
parent_layer = yp.layers[active_layer.parent_idx]
# Get parent index
if parent_layer:
parent_idx = get_layer_index(parent_layer)
has_parent = True
else:
parent_idx = -1
has_parent = False
# Add layer to group
layer = yp.layers.add()
layer.type = layer_type
layer.name = get_unique_name(layer_name, yp.layers)
layer.uv_name = uv_name
check_uvmap_on_other_objects_with_same_mat(mat, uv_name)
if segment:
layer.segment_name = segment.name
if image:
layer.image_name = image.name
# Move new layer to current index
last_index = len(yp.layers)-1
if active_layer and active_layer.type == 'GROUP':
index = yp.active_layer_index + 1
else: index = yp.active_layer_index
# Set parent index
parent_dict = set_parent_dict_val(yp, parent_dict, layer.name, parent_idx)
yp.layers.move(last_index, index)
layer = yp.layers[index] # Repoint to new index
# Remap parents
for lay in yp.layers:
lay.parent_idx = get_layer_index_by_name(yp, parent_dict[lay.name])
# Remap fcurves
remap_layer_fcurves(yp, index_dict)
# New layer tree
tree = bpy.data.node_groups.new(LAYERGROUP_PREFIX + layer_name, 'ShaderNodeTree')
tree.yp.is_ypaint_layer_node = True
tree.yp.version = get_current_version_str()
# New layer node group
group_node = new_node(group_tree, layer, 'group_node', 'ShaderNodeGroup', layer_name)
group_node.node_tree = tree
# Create info nodes
create_info_nodes(tree)
# Tree start and end
create_essential_nodes(tree, True, False, True)
# Uniform Scale
if is_bl_newer_than(2, 81) and is_layer_using_vector(layer):
layer.enable_uniform_scale = ypup.enable_uniform_uv_scale_by_default
# Add source
if layer_type == 'VCOL':
source = new_node(tree, layer, 'source', get_vcol_bl_idname(), 'Source')
else: source = new_node(tree, layer, 'source', layer_node_bl_idnames[layer_type], 'Source')
if layer_type == 'IMAGE':
# Always set non color to image node because of linear pipeline
if hasattr(source, 'color_space'):
source.color_space = 'NONE'
# Add new image if it's image layer
source.image = image
# Set interpolation
source.interpolation = interpolation
elif layer_type == 'VCOL':
if vcol: set_source_vcol_name(source, vcol.name)
else: set_source_vcol_name(source, layer_name)
elif layer_type == 'COLOR':
col = (solid_color[0], solid_color[1], solid_color[2], 1.0)
source.outputs[0].default_value = col
elif layer_type == 'HEMI':
source.node_tree = get_node_tree_lib(lib.HEMI)
duplicate_lib_node_tree(source)
load_hemi_props(layer, source)
layer.hemi_space = hemi_space
layer.hemi_use_prev_normal = hemi_use_prev_normal
# Add texcoord node
#texcoord = new_node(tree, layer, 'texcoord', 'NodeGroupInput', 'TexCoord Inputs')
# Add mapping node
if is_mapping_possible(layer.type):
mapping = new_node(tree, layer, 'mapping', 'ShaderNodeMapping', 'Mapping')
mapping.vector_type = 'POINT' #if segment else 'TEXTURE'
# Set layer coordinate type
layer.texcoord_type = texcoord_type
# Set layer spread fix
#if image and image.is_float:
# layer.divide_rgb_by_alpha = True
#else:
layer.divide_rgb_by_alpha = use_divider_alpha
# Add channels to current layer
for root_ch in yp.channels:
ch = layer.channels.add()
if add_mask:
#mask_name = 'Mask ' + layer.name
mask_name = Mask.get_new_mask_name(obj, layer, mask_type)
mask_image = None
mask_vcol = None
mask_segment = None
if mask_type == 'IMAGE':
if not mask_image_filepath:
color = (0, 0, 0, 0)
if mask_color == 'WHITE':
color = (1, 1, 1, 1)
elif mask_color == 'BLACK':
color = (0, 0, 0, 1)
if use_udim_for_mask:
objs = get_all_objects_with_same_materials(mat)
tilenums = UDIM.get_tile_numbers(objs, mask_uv_name)
if use_image_atlas_for_mask:
if use_udim_for_mask:
mask_segment = UDIM.get_set_udim_atlas_segment(
tilenums, mask_width, mask_height, color,
colorspace=get_noncolor_name(), hdr=mask_use_hdr, yp=yp
)
else:
mask_segment = ImageAtlas.get_set_image_atlas_segment(
mask_width, mask_height, mask_color, mask_use_hdr, yp=yp
)
mask_image = mask_segment.id_data
else:
if use_udim_for_mask:
mask_image = bpy.data.images.new(
mask_name, width=mask_width, height=mask_height,
alpha=False, float_buffer=mask_use_hdr, tiled=True
)
# Fill tiles
for tilenum in tilenums:
UDIM.fill_tile(mask_image, tilenum, color, mask_width, mask_height)
UDIM.initial_pack_udim(mask_image, color)
else:
mask_image = bpy.data.images.new(
mask_name, width=mask_width, height=mask_height,
alpha=False, float_buffer=mask_use_hdr
)
mask_image.generated_color = color
if hasattr(mask_image, 'use_alpha'):
mask_image.use_alpha = False
else:
if not os.path.isfile(mask_image_filepath):
print("There's no image with address '" + mask_image_filepath + "'!")
return {'CANCELLED'}
path = os.path.basename(mask_image_filepath)
directory = os.path.dirname(mask_image_filepath)
mask_image = load_image(path, directory)
if mask_relative and bpy.data.filepath != '':
try: mask_image.filepath = bpy.path.relpath(mask_image.filepath)
except: pass
if mask_image.colorspace_settings.name != get_noncolor_name() and not mask_image.is_dirty:
mask_image.colorspace_settings.name = get_noncolor_name()
# New vertex color
elif mask_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 mask_type == 'VCOL':
for o in objs:
if mask_name not in get_vertex_colors(o):
if not is_bl_newer_than(3, 3) and len(get_vertex_colors(o)) >= 8: continue
mask_vcol = new_vertex_color(o, mask_name, mask_vcol_data_type, mask_vcol_domain)
if mask_color == 'WHITE':
set_obj_vertex_colors(o, mask_vcol.name, (1.0, 1.0, 1.0, 1.0))
elif mask_color == 'BLACK':
set_obj_vertex_colors(o, mask_vcol.name, (0.0, 0.0, 0.0, 1.0))
set_active_vertex_color(o, mask_vcol)
elif mask_type == 'COLOR_ID':
check_colorid_vcol(objs)
mask = Mask.add_new_mask(
layer, mask_name, mask_type, mask_texcoord_type,
mask_uv_name, mask_image, mask_vcol, mask_segment,
interpolation=mask_interpolation, color_id=mask_color_id,
edge_detect_radius=mask_edge_detect_radius
)
mask.active_edit = True
# Fill channel layer props
shortcut_created = False
for i, ch in enumerate(layer.channels):
root_ch = yp.channels[i]
# Set some props to selected channel
if layer.type in {'GROUP', 'BACKGROUND'} or channel_idx == i or channel_idx == -1:
ch.enable = True
if root_ch.type == 'NORMAL':
ch.normal_blend_type = normal_blend_type
else:
ch.blend_type = blend_type
else:
ch.enable = False
if root_ch.type == 'NORMAL':
ch.normal_map_type = normal_map_type
# Background layer has default bump distance of 0.0
if layer.type in {'BACKGROUND'}:
ch.bump_distance = 0.0
# Set linear node of layer channel
check_layer_channel_linear_node(ch, layer, root_ch)
# Check uv maps
check_uv_nodes(yp)
# Check image projections
check_layer_projections(layer)
# Check and create layer channel nodes
check_all_layer_channel_io_and_nodes(layer, tree) #, has_parent=has_parent)
# Refresh paint image by updating the index
yp.active_layer_index = index
# Unhalt rearrangements and reconnections since all nodes already created
yp.halt_reconnect = False
#yp.halt_update = False
# Check layer IO
check_all_layer_channel_io_and_nodes(layer)
check_start_end_root_ch_nodes(group_tree)
# Rearrange node inside layers
reconnect_layer_nodes(layer)
rearrange_layer_nodes(layer)
return layer
class YRefreshNeighborUV(bpy.types.Operator):
"""Refresh Neighbor UV"""
bl_idname = "node.y_refresh_neighbor_uv"
bl_label = "Refresh Neighbor UV"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'layer') and hasattr(context, 'channel') and hasattr(context, 'image') and context.image
def execute(self, context):
set_uv_neighbor_resolution(context.layer)
return {'FINISHED'}
class YUseLinearColorSpace(bpy.types.Operator):
"""This addon need to linear color space image to works properly"""
bl_idname = "node.y_use_linear_color_space"
bl_label = "Use Linear Color Space"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'layer') #and hasattr(context, 'channel') and hasattr(context, 'image') and context.image
def execute(self, context):
yp = context.layer.id_data.yp
check_yp_linear_nodes(yp)
return {'FINISHED'}
class YNewVcolToOverrideChannel(bpy.types.Operator):
bl_idname = "node.y_new_vcol_to_override_channel"
bl_label = "New Vertex Color To Override Channel Layer"
bl_description = "New Vertex Color To Override Channel Layer"
bl_options = {'UNDO'}
name : StringProperty(default='')
data_type : EnumProperty(
name = 'Vertex Color Data Type',
description = 'Vertex color data type',
items = vcol_data_type_items,
default = 'BYTE_COLOR'
)
domain : EnumProperty(
name = 'Vertex Color Domain',
description = 'Vertex color domain',
items = vcol_domain_items,
default = 'CORNER'
)
@classmethod
def poll(cls, context):
return get_active_ypaint_node()
@classmethod
def description(self, context, properties):
return get_operator_description(self)
def invoke(self, context, event):
self.ch = context.parent
yp = self.ch.id_data.yp
m = re.match(r'yp\.layers\[(\d+)\]\.channels\[(\d+)\]', self.ch.path_from_id())
if not m: return []
layer = yp.layers[int(m.group(1))]
root_ch = yp.channels[int(m.group(2))]
self.tree = get_tree(layer)
self.name = layer.name + ' ' + root_ch.name + ' Override'
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 draw(self, context):
row = split_layout(self.layout, 0.4)
col = row.column()
col.label(text='Name:')
if is_bl_newer_than(3, 2):
col.label(text='Domain:')
col.label(text='Data Type:')
col = row.column()
col.prop(self, 'name', text='')
if is_bl_newer_than(3, 2):
crow = col.row(align=True)
crow.prop(self, 'domain', expand=True)
crow = col.row(align=True)
crow.prop(self, 'data_type', expand=True)
def execute(self, context):
T = time.time()
ch = self.ch
yp = ch.id_data.yp
tree = self.tree
obj = context.object
mat = obj.active_material
wm = context.window_manager
if self.name == '' :
self.report({'ERROR'}, "Vertex color cannot be empty!")
return {'CANCELLED'}
# Make sure channel is on
if not ch.enable:
ch.enable = True
# Make sure override is on
if not ch.override:
ch.override = True
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)
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.data_type, self.domain)
set_obj_vertex_colors(o, vcol.name, (1.0, 1.0, 1.0, 1.0))
set_active_vertex_color(o, vcol)
# Update vcol cache
if ch.override_type == 'VCOL':
source_label = root_ch.name + ' Override : ' + ch.override_type
vcol_node, dirty = check_new_node(tree, ch, 'source', get_vcol_bl_idname(), source_label, True)
else: vcol_node, dirty = check_new_node(tree, ch, 'cache_vcol', get_vcol_bl_idname(), '', True)
set_source_vcol_name(vcol_node, self.name)
# Set vcol name attribute
yp.halt_update = True
ch.override_vcol_name = self.name
yp.halt_update = False
ch.override_type = 'VCOL'
ch.active_edit = True
# Update UI
wm.ypui.need_update = True
print('INFO: Vertex Color is created in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
wm.yptimer.time = str(time.time())
return {'FINISHED'}
def update_new_layer_uv_map(self, context):
if not UDIM.is_udim_supported(): return
if hasattr(self, 'type') and 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_map)
def update_new_layer_mask_uv_map(self, context):
if not UDIM.is_udim_supported(): return
if self.mask_type != 'IMAGE':
self.use_udim_for_mask = 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_for_mask = UDIM.is_uvmap_udim(objs, self.mask_uv_name)
def update_channel_idx_new_layer(self, context):
node = get_active_ypaint_node()
yp = node.node_tree.yp
# Bump map will use cubic interpolation
channel_idx = int(self.channel_idx)
if channel_idx != -1 and channel_idx < len(yp.channels):
channel = yp.channels[channel_idx]
else: channel = None
if channel and channel.type == 'NORMAL' and self.normal_map_type == 'BUMP_MAP':
self.interpolation = 'Cubic'
class YNewVDMLayer(bpy.types.Operator):
bl_idname = "node.y_new_vdm_layer"
bl_label = "New VDM Layer"
bl_description = "New Vector Displacement Layer"
bl_options = {'UNDO'}
name : StringProperty(default='')
width : IntProperty(name='Width', default=1024, min=1, max=16384)
height : IntProperty(name='Height', default=1024, min=1, max=16384)
image_resolution : EnumProperty(
name = 'Image Resolution',
items = image_resolution_items,
default = '1024'
)
use_custom_resolution : BoolProperty(
name = 'Custom Resolution',
description = 'Use custom Resolution to adjust the width and height individually',
default = False
)
blend_type : EnumProperty(
name = 'Blend Type',
items = normal_blend_items,
default = 'OVERLAY'
)
use_udim : BoolProperty(
name = 'Use UDIM Tiles',
description = 'Use UDIM Tiles',
default = False
)
enable_subdiv_setup : BoolProperty(
name = 'Enable Displacement Setup',
description = 'Enable Displacement Setup on Normal channel',
default = True
)
# NOTE: UDIM is not supported yet, so no UDIM checking
uv_map : StringProperty(default='') #, update=update_new_layer_uv_map)
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'MESH' and get_active_ypaint_node()
@classmethod
def description(self, context, properties):
return get_operator_description(self)
def invoke(self, context, event):
ypup = get_user_preferences()
obj = context.object
node = self.node = get_active_ypaint_node()
yp = self.yp = node.node_tree.yp
# Set default name
name = obj.active_material.name + DEFAULT_NEW_VDM_SUFFIX
self.name = get_unique_name(name, bpy.data.images)
# 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
# Set default UV name
#uv_name = get_default_uv_name(obj, yp)
uv_name = get_active_render_uv(obj)
self.uv_map = uv_name
# UV Map collections update
self.uv_map_coll.clear()
for uv in get_uv_layers(obj):
if not uv.name.startswith(TEMP_UV):
self.uv_map_coll.add().name = uv.name
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 draw(self, context):
yp = self.yp
first_vdm = get_first_vdm_layer(yp)
row = split_layout(self.layout, 0.4)
col = row.column(align=False)
col.label(text='Name:')
col.label(text='')
if not self.use_custom_resolution:
col.label(text='Resolution:')
else:
col.label(text='Width:')
col.label(text='Height:')
col.label(text='Blend Type:')
col.label(text='UV Map:')
col = row.column(align=False)
col.prop(self, 'name', text='')
col.prop(self, 'use_custom_resolution')
if not self.use_custom_resolution:
crow = col.row(align=True)
crow.prop(self, 'image_resolution', expand=True)
else:
col.prop(self, 'width', text='')
col.prop(self, 'height', text='')
col.prop(self, 'blend_type', text='')
if not first_vdm:
col.prop_search(self, "uv_map", self, "uv_map_coll", text='', icon='GROUP_UVS')
else:
col.label(text=self.uv_map + '*') # + ' (used by other VDM)')
self.layout.label(text='* Only one UV map is currently supported')
# NOTE: UDIM is not supported yet
if False:
col.prop(self, 'use_udim')
height_root_ch = get_root_height_channel(yp)
if height_root_ch and not height_root_ch.enable_subdiv_setup:
col = self.layout.column()
col.label(text='Displacement Setup is not enabled yet!', icon='ERROR')
col.prop(self, 'enable_subdiv_setup')
def check(self, context):
if not self.use_custom_resolution:
self.width = int(self.image_resolution)
self.height = int(self.image_resolution)
return True
def execute(self, context):
T = time.time()
wm = context.window_manager
node = self.node
yp = self.yp
mat = get_active_material()
objs = get_all_objects_with_same_materials(mat)
height_root_ch = get_root_height_channel(yp)
if not height_root_ch:
self.report({'ERROR'}, "There should be a normal channel!")
return {'CANCELLED'}
channel_idx = get_channel_index(height_root_ch)
alpha = True
color = (0, 0, 0, 0)
if self.use_udim:
tilenums = UDIM.get_tile_numbers(objs, self.uv_map)
img = bpy.data.images.new(
name=self.name, width=self.width, height=self.height,
alpha=alpha, float_buffer=True, 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=True
)
#img.generated_type = self.generated_type
img.generated_type = 'BLANK'
img.generated_color = color
if hasattr(img, 'use_alpha'):
img.use_alpha = True
update_image_editor_image(context, img)
yp.halt_update = True
layer = add_new_layer(
node.node_tree, self.name, 'IMAGE',
channel_idx, 'MIX', self.blend_type,
'VECTOR_DISPLACEMENT_MAP', 'UV', self.uv_map, img,
interpolation = 'Cubic'
)
yp.halt_update = False
if not height_root_ch.enable_subdiv_setup and self.enable_subdiv_setup:
height_root_ch.enable_subdiv_setup = True
# Reconnect and rearrange nodes
reconnect_yp_nodes(node.node_tree)
rearrange_yp_nodes(node.node_tree)
# Update UI
ypui = context.window_manager.ypui
ypui.layer_ui.expand_channels = False
ypui.layer_ui.expand_content = False
ypui.layer_ui.expand_source = False
ch = layer.channels[channel_idx]
ch.expand_content = True
ypui.need_update = True
# Set uv map active render
for obj in objs:
uv_layers = get_uv_layers(obj)
uv = uv_layers.get(self.uv_map)
if uv and not uv.active_render:
uv.active_render = True
print('INFO: VDM Layer', layer.name, 'is created in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
wm.yptimer.time = str(time.time())
return {'FINISHED'}
class YNewLayer(bpy.types.Operator):
bl_idname = "node.y_new_layer"
bl_label = "New Layer"
bl_description = "New Layer"
bl_options = {'REGISTER', 'UNDO'}
name : StringProperty(default='')
type : EnumProperty(
name = 'Layer Type',
items = layer_type_items,
default = 'IMAGE'
)
# For image layer
width : IntProperty(name='Width', default=1024, min=1, max=16384)
height : IntProperty(name='Height', default=1024, min=1, max=16384)
#color : FloatVectorProperty(name='Color', size=4, subtype='COLOR', default=(0.0,0.0,0.0,0.0), min=0.0, max=1.0)
#alpha : BoolProperty(name='Alpha', default=True)
hdr : BoolProperty(name='32 bit Float', default=False)
interpolation : EnumProperty(
name = 'Image Interpolation Type',
description = 'Image interpolation type',
items = interpolation_type_items,
default = 'Linear'
)
texcoord_type : EnumProperty(
name = 'Layer Coordinate Type',
description = 'Layer Coordinate Type',
items = texcoord_type_items,
default = 'UV'
)
channel_idx : EnumProperty(
name = 'Channel',
description = 'Channel of new layer, can be changed later',
items = channel_items,
update = update_channel_idx_new_layer
)
blend_type : EnumProperty(
name = 'Blend',
description = 'Blend type',
items = blend_type_items,
)
normal_blend_type : EnumProperty(
name = 'Normal Blend Type',
items = normal_blend_items,
default = 'MIX'
)
solid_color : FloatVectorProperty(
name = 'Solid Color',
size = 3,
subtype = 'COLOR',
default=(1.0, 1.0, 1.0), min=0.0, max=1.0
)
add_mask : BoolProperty(
name = 'Add Mask',
description = 'Add mask to new layer',
default = False
)
mask_type : EnumProperty(
name = 'Mask Type',
description = 'Mask type',
items = (
('IMAGE', 'Image', '', 'IMAGE_DATA', 0),
('VCOL', 'Vertex Color', '', 'GROUP_VCOL', 1),
('COLOR_ID', 'Color ID', '', 'COLOR', 2),
('EDGE_DETECT', 'Edge Detect', '', 'MESH_CUBE', 3)
),
default = 'IMAGE'
)
mask_color : EnumProperty(
name = 'Mask Color',
description = 'Mask Color',
items = (
('WHITE', 'White (Full Opacity)', ''),
('BLACK', 'Black (Full Transparency)', ''),
),
default = 'BLACK'
)
mask_width : IntProperty(name='Mask Width', default=1024, min=1, max=4096)
mask_height : IntProperty(name='Mask Height', default=1024, min=1, max=4096)
mask_image_resolution : EnumProperty(
name = 'Image Resolution',
items = image_resolution_items,
default = '1024'
)
mask_use_custom_resolution : BoolProperty(
name = 'Custom Resolution',
description= 'Use custom Resolution to adjust the width and height individually',
default = False
)
mask_interpolation : EnumProperty(
name = 'Mask Image Interpolation Type',
description = 'Mask image interpolation type',
items = interpolation_type_items,
default = 'Linear'
)
mask_texcoord_type : EnumProperty(
name = 'Mask Coordinate Type',
description = 'Mask Coordinate Type',
items = texcoord_type_items,
default = 'UV'
)
mask_uv_name : StringProperty(default='', update=update_new_layer_mask_uv_map)
mask_use_hdr : BoolProperty(name='32 bit Float', default=False)
mask_color_id : FloatVectorProperty(
name = 'Color ID',
size = 3,
subtype = 'COLOR',
default=(1.0, 0.0, 1.0), min=0.0, max=1.0
)
mask_image_filepath : StringProperty(
name = 'Mask Image Path',
description = 'Path to mask image',
default = '',
subtype = 'FILE_PATH',
options = {'SKIP_SAVE'}
)
mask_relative : BoolProperty(name="Relative Mask Path", default=True, description="Apply relative paths")
uv_map : StringProperty(default='', update=update_new_layer_uv_map)
normal_map_type : EnumProperty(
name = 'Normal Map Type',
description = 'Normal map type of this layer',
items = get_normal_map_type_items
)
use_udim : BoolProperty(
name = 'Use UDIM Tiles',
description = 'Use UDIM Tiles',
default = False
)
use_udim_for_mask : BoolProperty(
name = 'Use UDIM Tiles for Mask',
description='Use UDIM Tiles for Mask',
default = False
)
use_image_atlas : BoolProperty(
name = 'Use Image Atlas',
description='Use Image Atlas',
default = False
)
use_image_atlas_for_mask : BoolProperty(
name = 'Use Image Atlas for Mask',
description='Use Image Atlas for Mask',
default = False
)
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 account previous Normal',
default = True
)
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'
)
mask_vcol_data_type : EnumProperty(
name = 'Mask Vertex Color Data Type',
description = 'Mask Vertex color data type',
items = vcol_data_type_items,
default = 'BYTE_COLOR'
)
mask_vcol_domain : EnumProperty(
name = 'Mask Vertex Color Domain',
description = 'Mask Vertex color domain',
items = vcol_domain_items,
default = 'CORNER'
)
use_divider_alpha : BoolProperty(
name = 'Divide RGB by Alpha',
description='Divide RGB by its alpha value (very recommended for vertex color layer)',
default = False
)
# For edge detection
mask_edge_detect_radius : FloatProperty(
name = 'Edge Detect Mask Radius',
description = 'Edge detect mask radius',
default=0.05, min=0.0, max=10.0
)
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
image_resolution : EnumProperty(