-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Bake.py
3835 lines (3036 loc) · 141 KB
/
Bake.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, math, numpy
from bpy.props import *
from mathutils import *
from .common import *
from .bake_common import *
from .subtree import *
from .node_connections import *
from .node_arrangements import *
from .input_outputs import *
from . import lib, Layer, Mask, Modifier, MaskModifier, image_ops
def transfer_uv(objs, mat, entity, uv_map, is_entity_baked=False):
yp = entity.id_data.yp
scene = bpy.context.scene
# Check entity
m1 = re.match(r'^yp\.layers\[(\d+)\]$', entity.path_from_id())
m2 = re.match(r'^yp\.layers\[(\d+)\]\.masks\[(\d+)\]$', entity.path_from_id())
if m1:
if is_entity_baked:
tree = get_tree(entity)
source = tree.nodes.get(entity.baked_source)
else: source = get_layer_source(entity)
mapping = get_layer_mapping(entity)
index = int(m1.group(1))
elif m2:
if is_entity_baked:
tree = get_mask_tree(entity)
source = tree.nodes.get(entity.baked_source)
else: source = get_mask_source(entity)
mapping = get_mask_mapping(entity)
index = int(m2.group(2))
else: return
image = source.image
if not image: return
# Merge objects if necessary
temp_objs = []
if len(objs) > 1 and not is_join_objects_problematic(yp):
objs = temp_objs = [get_merged_mesh_objects(scene, objs)]
# Set active uv
for obj in objs:
uv_layers = get_uv_layers(obj)
uv_layers.active = uv_layers.get(uv_map)
# Get tile numbers
tilenums = UDIM.get_tile_numbers(objs, uv_map)
# Get image settings
segment = None
use_alpha = False
if image.yia.is_image_atlas and entity.segment_name != '':
segment = image.yia.segments.get(entity.segment_name)
width = segment.width
height = segment.height
if image.yia.color == 'WHITE':
col = (1.0, 1.0, 1.0, 1.0)
elif image.yia.color == 'BLACK':
col = (0.0, 0.0, 0.0, 1.0)
else:
col = (0.0, 0.0, 0.0, 0.0)
use_alpha = True
elif image.yua.is_udim_atlas and entity.segment_name != '':
segment = image.yua.segments.get(entity.segment_name)
segment_tilenums = UDIM.get_udim_segment_tilenums(segment)
# Get the highest resolution
for i, st in enumerate(segment_tilenums):
if i == 0 : width = height = 1
tile = image.tiles.get(st)
if tile.size[0] > width: width = tile.size[0]
if tile.size[1] > height: height = tile.size[1]
col = segment.base_color
use_alpha = True if col[3] < 0.5 else False
else:
width = image.size[0]
height = image.size[1]
# Change color if baked image is found
if 'Pointiness' in image.name:
col = (0.73, 0.73, 0.73, 1.0)
elif 'AO' in image.name:
col = (1.0, 1.0, 1.0, 1.0)
elif m2: # Possible mask base color
if index == 0:
col = (0.0, 0.0, 0.0, 1.0)
else:
col = (1.0, 1.0, 1.0, 1.0)
else:
col = (0.0, 0.0, 0.0, 0.0)
use_alpha = True
# Create temp image as bake target
if len(tilenums) > 1 or (segment and image.source == 'TILED'):
temp_image = bpy.data.images.new(
name='__TEMP', width=width, height=height,
alpha=True, float_buffer=image.is_float, tiled=True
)
# Fill tiles
for tilenum in tilenums:
UDIM.fill_tile(temp_image, tilenum, col, width, height)
# Initial pack
if image.yua.is_udim_atlas:
UDIM.initial_pack_udim(temp_image, col)
else: UDIM.initial_pack_udim(temp_image, col, image.name)
else:
temp_image = bpy.data.images.new(
name='__TEMP', width=width, height=height,
alpha=True, float_buffer=image.is_float
)
temp_image.colorspace_settings.name = image.colorspace_settings.name
temp_image.generated_color = col
# Create bake nodes
tex = mat.node_tree.nodes.new('ShaderNodeTexImage')
emit = mat.node_tree.nodes.new('ShaderNodeEmission')
# Set image to temp nodes
src = mat.node_tree.nodes.new('ShaderNodeTexImage')
src.image = image
src_uv = mat.node_tree.nodes.new('ShaderNodeUVMap')
src_uv.uv_map = entity.uv_name
# Copy mapping
mapp = mat.node_tree.nodes.new('ShaderNodeMapping')
if is_bl_newer_than(2, 81):
mapp.inputs[1].default_value[0] = mapping.inputs[1].default_value[0]
mapp.inputs[1].default_value[1] = mapping.inputs[1].default_value[1]
mapp.inputs[1].default_value[2] = mapping.inputs[1].default_value[2]
mapp.inputs[2].default_value[0] = mapping.inputs[2].default_value[0]
mapp.inputs[2].default_value[1] = mapping.inputs[2].default_value[1]
mapp.inputs[2].default_value[2] = mapping.inputs[2].default_value[2]
mapp.inputs[3].default_value[0] = mapping.inputs[3].default_value[0]
mapp.inputs[3].default_value[1] = mapping.inputs[3].default_value[1]
mapp.inputs[3].default_value[2] = mapping.inputs[3].default_value[2]
else:
mapp.translation[0] = mapping.translation[0]
mapp.translation[1] = mapping.translation[1]
mapp.translation[2] = mapping.translation[2]
mapp.rotation[0] = mapping.rotation[0]
mapp.rotation[1] = mapping.rotation[1]
mapp.rotation[2] = mapping.rotation[2]
mapp.scale[0] = mapping.scale[0]
mapp.scale[1] = mapping.scale[1]
mapp.scale[2] = mapping.scale[2]
# Get material output
output = get_active_mat_output_node(mat.node_tree)
ori_bsdf = output.inputs[0].links[0].from_socket
straight_over = None
if use_alpha:
straight_over = mat.node_tree.nodes.new('ShaderNodeGroup')
straight_over.node_tree = get_node_tree_lib(lib.STRAIGHT_OVER)
straight_over.inputs[1].default_value = 0.0
# Set temp image node
tex.image = temp_image
mat.node_tree.nodes.active = tex
# Links
if not is_entity_baked:
mat.node_tree.links.new(src_uv.outputs[0], mapp.inputs[0])
mat.node_tree.links.new(mapp.outputs[0], src.inputs[0])
else: mat.node_tree.links.new(src_uv.outputs[0], src.inputs[0])
rgb = src.outputs[0]
alpha = src.outputs[1]
if straight_over:
mat.node_tree.links.new(rgb, straight_over.inputs[2])
mat.node_tree.links.new(alpha, straight_over.inputs[3])
rgb = straight_over.outputs[0]
mat.node_tree.links.new(rgb, emit.inputs[0])
mat.node_tree.links.new(emit.outputs[0], output.inputs[0])
# Bake!
bake_object_op()
# Bake alpha if using alpha
if use_alpha:
# Create another temp image
temp_image1 = temp_image.copy()
tex.image = temp_image1
if temp_image1.source == 'TILED':
temp_image1.name = '__TEMP1'
UDIM.initial_pack_udim(temp_image1)
mat.node_tree.links.new(src.outputs[1], emit.inputs[0])
# Temp image should use linear to properly bake alpha
temp_image1.colorspace_settings.name = get_noncolor_name()
# Bake again!
bake_object_op()
# Set tile pixels
for tilenum in tilenums:
# Swap tile
if tilenum != 1001:
UDIM.swap_tile(temp_image, 1001, tilenum)
UDIM.swap_tile(temp_image1, 1001, tilenum)
# Copy the result to original temp image
copy_image_channel_pixels(temp_image1, temp_image, 0, 3)
# Swap tile again to recover
if tilenum != 1001:
UDIM.swap_tile(temp_image, 1001, tilenum)
UDIM.swap_tile(temp_image1, 1001, tilenum)
# Remove temp image 1
remove_datablock(bpy.data.images, temp_image1, user=tex, user_prop='image')
if segment and image.source == 'TILED':
# Remove original segment
UDIM.remove_udim_atlas_segment_by_name(image, segment.name, yp)
# Create new segment
new_segment = UDIM.get_set_udim_atlas_segment(
tilenums,
width=width, height=height, color=col,
colorspace=image.colorspace_settings.name, hdr=image.is_float, yp=yp,
source_image=temp_image, source_tilenums=tilenums
)
# Set image
if image != new_segment.id_data:
source.image = new_segment.id_data
# Remove temp image
remove_datablock(bpy.data.images, temp_image, user=tex, user_prop='image')
elif temp_image.source == 'TILED' or image.source == 'TILED':
# Replace image if any of the images is using UDIM
replace_image(image, temp_image)
else:
# Copy back temp/baked image to original image
copy_image_pixels(temp_image, image, segment)
# Remove temp image
remove_datablock(bpy.data.images, temp_image, user=tex, user_prop='image')
# HACK: Pack and refresh to update image in Blender 2.77 and lower
if not is_bl_newer_than(2, 78) and (image.packed_file or image.filepath == ''):
if image.is_float:
image_ops.pack_float_image(image)
else: image.pack(as_png=True)
image.reload()
# Remove temp nodes
simple_remove_node(mat.node_tree, tex)
simple_remove_node(mat.node_tree, emit)
simple_remove_node(mat.node_tree, src)
simple_remove_node(mat.node_tree, src_uv)
simple_remove_node(mat.node_tree, mapp)
if straight_over:
simple_remove_node(mat.node_tree, straight_over)
mat.node_tree.links.new(ori_bsdf, output.inputs[0])
if not is_entity_baked:
# Update entity transform
entity.translation = (0.0, 0.0, 0.0)
entity.rotation = (0.0, 0.0, 0.0)
entity.scale = (1.0, 1.0, 1.0)
# Update mapping
update_mapping(entity)
# Change uv of entity
entity.uv_name = uv_map
# Remove temporary objects
if temp_objs:
for o in temp_objs:
remove_mesh_obj(o)
def set_entities_which_using_the_same_image_or_segment(entity, target_uv_name):
yp = entity.id_data.yp
if entity.type == 'IMAGE':
m = re.match(r'yp\.layers\[(\d+)\]\.masks\[(\d+)\]', entity.path_from_id())
if m: source = get_mask_source(entity)
else: source = get_layer_source(entity)
image = source.image
segment_mix_name = image.name + entity.segment_name if image and (image.yia.is_image_atlas or image.yua.is_udim_atlas) else ''
for layer in yp.layers:
for mask in layer.masks:
if mask == entity or mask.type != 'IMAGE': continue
src = get_mask_source(mask)
img = src.image
if img.yia.is_image_atlas or img.yua.is_udim_atlas:
if img.name + mask.segment_name == segment_mix_name:
mask.uv_name = target_uv_name
else:
if img == image:
mask.uv_name = target_uv_name
if layer == entity or layer.type != 'IMAGE': continue
src = get_layer_source(layer)
img = src.image
if img.yia.is_image_atlas or img.yua.is_udim_atlas:
if img.name + layer.segment_name == segment_mix_name:
layer.uv_name = target_uv_name
else:
if img == image:
layer.uv_name = target_uv_name
def get_entities_to_transfer(yp, from_uv_map, to_uv_map):
# Check the same images used by multiple layers or masks
used_images = []
used_segments = []
entities = []
for layer in yp.layers:
if layer.baked_source != '' and layer.baked_uv_name == from_uv_map:
if layer not in entities: entities.append(layer)
if layer.type == 'IMAGE' and layer.uv_name == from_uv_map:
source = get_layer_source(layer)
if source and source.image:
image = source.image
if image.yia.is_image_atlas or image.yua.is_udim_atlas:
if image.name + layer.segment_name not in used_segments:
used_segments.append(image.name + layer.segment_name)
if layer not in entities: entities.append(layer)
else:
if image not in used_images:
used_images.append(image)
if layer not in entities: entities.append(layer)
if layer not in entities:
layer.uv_name = to_uv_map
for mask in layer.masks:
if mask.baked_source != '' and mask.baked_uv_name == from_uv_map:
if mask not in entities: entities.append(mask)
if mask.type == 'IMAGE' and mask.uv_name == from_uv_map:
source = get_mask_source(mask)
if source and source.image:
image = source.image
if image.yia.is_image_atlas or image.yua.is_udim_atlas:
if image.name + mask.segment_name not in used_segments:
used_segments.append(image.name + mask.segment_name)
if mask not in entities: entities.append(mask)
else:
if image not in used_images:
used_images.append(image)
if mask not in entities: entities.append(mask)
if mask not in entities:
mask.uv_name = to_uv_map
return entities
class YTransferSomeLayerUV(bpy.types.Operator, BaseBakeOperator):
bl_idname = "node.y_transfer_some_layer_uv"
bl_label = "Transfer Some Layer UV"
bl_description = "Transfer some layers/masks UV by baking it to other uv (this will take quite some time to finish)"
bl_options = {'REGISTER', 'UNDO'}
from_uv_map : StringProperty(default='')
uv_map : StringProperty(default='')
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
remove_from_uv : BoolProperty(
name = 'Delete From UV',
description = "Remove 'From UV' from objects",
default = False
)
reorder_uv_list : BoolProperty(
name = 'Reorder UV',
description = "Reorder 'To UV' so it will have the same index as 'From UV'",
default = True
)
@classmethod
def poll(cls, context):
return get_active_ypaint_node() and context.object.type == 'MESH' # and hasattr(context, 'layer')
def invoke(self, context, event):
self.invoke_operator(context)
obj = self.obj = context.object
scene = self.scene = context.scene
if hasattr(context, 'mask'):
self.entity = context.mask
elif hasattr(context, 'layer'):
self.entity = context.layer
# Use active uv layer name by default
uv_layers = get_uv_layers(obj)
# UV Map collections update
self.uv_map_coll.clear()
for uv in uv_layers:
if not uv.name.startswith(TEMP_UV):
self.uv_map_coll.add().name = uv.name
self.from_uv_map = self.entity.uv_name
return context.window_manager.invoke_props_dialog(self, width=320)
def check(self, context):
return True
def draw(self, context):
row = split_layout(self.layout, 0.4)
col = row.column(align=False)
col.label(text='From UV:')
col.label(text='To UV:')
col.label(text='Samples:')
col.label(text='Margin:')
col.label(text='')
if self.remove_from_uv:
col.label(text='')
col = row.column(align=False)
col.prop_search(self, "from_uv_map", self, "uv_map_coll", text='', icon='GROUP_UVS')
col.prop_search(self, "uv_map", self, "uv_map_coll", text='', icon='GROUP_UVS')
col.prop(self, 'samples', text='')
if is_bl_newer_than(3, 1):
split = split_layout(col, 0.4, align=True)
split.prop(self, 'margin', text='')
split.prop(self, 'margin_type', text='')
else:
col.prop(self, 'margin', text='')
col.prop(self, 'remove_from_uv')
if self.remove_from_uv:
col.prop(self, 'reorder_uv_list')
def execute(self, context):
T = time.time()
if self.from_uv_map == '' or self.uv_map == '':
self.report({'ERROR'}, "From or To UV Map cannot be empty!")
return {'CANCELLED'}
if self.from_uv_map == self.uv_map:
self.report({'ERROR'}, "From and To UV cannot have same value!")
return {'CANCELLED'}
mat = get_active_material()
node = get_active_ypaint_node()
yp = node.node_tree.yp
objs = get_all_objects_with_same_materials(mat)
# Check if all uv are available on all objects
for obj in objs:
uv_layers = get_uv_layers(obj)
from_uv = uv_layers.get(self.from_uv_map)
to_uv = uv_layers.get(self.uv_map)
if not from_uv or not to_uv:
self.report({'ERROR'}, "Some uvs are not found in some objects!")
return {'CANCELLED'}
# Prepare bake settings
book = remember_before_bake(yp)
prepare_bake_settings(
book, objs, yp, samples=self.samples, margin=self.margin,
uv_map=self.uv_map, bake_type='EMIT', bake_device=self.bake_device, margin_type=self.margin_type
)
# Get entites to transfer
entities = get_entities_to_transfer(yp, self.from_uv_map, self.uv_map)
for entity in entities:
if entity.type == 'IMAGE':
print('TRANSFER UV: Transferring entity ' + entity.name + '...')
transfer_uv(objs, mat, entity, self.uv_map)
if entity.baked_source != '':
print('TRANSFER UV: Transferring baked entity ' + entity.name + '...')
transfer_uv(objs, mat, entity, self.uv_map, is_entity_baked=True)
if entity.uv_name != self.uv_map:
entity.uv_name = self.uv_map
#return {'FINISHED'}
if self.remove_from_uv:
for obj in objs:
uv_layers = get_uv_layers(obj)
ori_index = get_uv_layer_index(obj, self.from_uv_map)
from_uv = uv_layers.get(self.from_uv_map)
uv_layers.remove(from_uv)
# Reorder UV
if self.reorder_uv_list and ori_index != -1:
uv_index = get_uv_layer_index(obj, self.uv_map)
if ori_index > uv_index:
ori_index -= 1
move_uv(obj, uv_index, ori_index)
# Recover bake settings
recover_bake_settings(book, yp)
# Check height channel uv
height_ch = get_root_height_channel(yp)
if height_ch and height_ch.main_uv == self.from_uv_map:
height_ch.main_uv = self.uv_map
#height_ch.enable_smooth_bump = height_ch.enable_smooth_bump
# Refresh mapping and stuff
yp.active_layer_index = yp.active_layer_index
print(
'INFO: All layers and masks using', self.from_uv_map,
'are transferred to', self.uv_map,
'in', '{:0.2f}'.format(time.time() - T), 'seconds!'
)
return {'FINISHED'}
class YTransferLayerUV(bpy.types.Operator, BaseBakeOperator):
bl_idname = "node.y_transfer_layer_uv"
bl_label = "Transfer Layer UV"
bl_description = "Transfer Layer UV by baking it to other uv (this will take quite some time to finish)"
bl_options = {'REGISTER', 'UNDO'}
uv_map : StringProperty(default='')
uv_map_coll : CollectionProperty(type=bpy.types.PropertyGroup)
@classmethod
def poll(cls, context):
return get_active_ypaint_node() and context.object.type == 'MESH' # and hasattr(context, 'layer')
def invoke(self, context, event):
self.invoke_operator(context)
obj = self.obj = context.object
scene = self.scene = context.scene
if hasattr(context, 'mask'):
self.entity = context.mask
elif hasattr(context, 'layer'):
self.entity = context.layer
if not self.entity:
return self.execute(context)
# Use active uv layer name by default
uv_layers = get_uv_layers(obj)
# UV Map collections update
self.uv_map_coll.clear()
for uv in uv_layers:
if not uv.name.startswith(TEMP_UV) and uv.name != self.entity.uv_name:
self.uv_map_coll.add().name = uv.name
return context.window_manager.invoke_props_dialog(self, width=320)
def check(self, context):
return True
def draw(self, context):
row = split_layout(self.layout, 0.4)
col = row.column(align=False)
col.label(text='Target UV:')
col.label(text='Samples:')
col.label(text='Margin:')
col = row.column(align=False)
col.prop_search(self, "uv_map", self, "uv_map_coll", text='', icon='GROUP_UVS')
col.prop(self, 'samples', text='')
if is_bl_newer_than(3, 1):
split = split_layout(col, 0.4, align=True)
split.prop(self, 'margin', text='')
split.prop(self, 'margin_type', text='')
else:
col.prop(self, 'margin', text='')
def execute(self, context):
T = time.time()
if not hasattr(self, 'entity'):
return {'CANCELLED'}
if self.entity.type != 'IMAGE' or self.entity.texcoord_type != 'UV':
self.report({'ERROR'}, "Only works with image layer/mask with UV Mapping")
return {'CANCELLED'}
if self.uv_map == '':
self.report({'ERROR'}, "Target UV Map cannot be empty!")
return {'CANCELLED'}
if self.uv_map == self.entity.uv_name:
self.report({'ERROR'}, "This layer/mask already use " + self.uv_map + "!")
return {'CANCELLED'}
mat = get_active_material()
yp = self.entity.id_data.yp
objs = get_all_objects_with_same_materials(mat)
# Prepare bake settings
book = remember_before_bake(yp)
prepare_bake_settings(
book, objs, yp, samples=self.samples, margin=self.margin,
uv_map=self.uv_map, bake_type='EMIT', bake_device=self.bake_device, margin_type=self.margin_type
)
if self.entity.type == 'IMAGE':
# Set other entites uv that using the same image or segment
set_entities_which_using_the_same_image_or_segment(self.entity, self.uv_map)
# Transfer UV
#for ent in entities:
transfer_uv(objs, mat, self.entity, self.uv_map)
if self.entity.baked_source != '':
transfer_uv(objs, mat, self.entity, self.uv_map, is_entity_baked=True)
# Recover bake settings
recover_bake_settings(book, yp)
# Refresh mapping and stuff
yp.active_layer_index = yp.active_layer_index
print(
'INFO:', self.entity.name,
'UV is transferred from', self.entity.uv_name,
'to', self.uv_map,
'in', '{:0.2f}'.format(time.time() - T), 'seconds!'
)
return {'FINISHED'}
def get_resize_image_entity_and_image(self, context):
yp = get_active_ypaint_node().node_tree.yp
entity = yp.layers.get(self.layer_name)
image = bpy.data.images.get(self.image_name)
if entity:
for mask in entity.masks:
if mask.active_edit:
entity = mask
break
return entity, image
def update_resize_image_tile_number(self, context):
entity, image = get_resize_image_entity_and_image(self, context)
if image and image.source == 'TILED':
tile = image.tiles.get(int(self.tile_number))
if tile:
self.width = tile.size[0]
self.height = tile.size[1]
class YResizeImage(bpy.types.Operator, BaseBakeOperator):
bl_idname = "node.y_resize_image"
bl_label = "Resize Image Layer/Mask"
bl_description = "Resize image of layer or mask"
bl_options = {'REGISTER', 'UNDO'}
layer_name : StringProperty(default='')
image_name : StringProperty(default='')
width : IntProperty(name='Width', default=1024, min=1, max=16384)
height : IntProperty(name='Height', default=1024, min=1, max=16384)
all_tiles : BoolProperty(
name = 'Resize All Tiles',
description = 'Resize all tiles (when using UDIM atlas, only segment tiles will be resized)',
default = False
)
tile_number : EnumProperty(
name = 'Tile Number',
description = 'Tile number that will be resized',
items = UDIM.udim_tilenum_items,
update = update_resize_image_tile_number
)
@classmethod
def poll(cls, context):
return get_active_ypaint_node() and context.object.type == 'MESH'
def invoke(self, context, event):
self.invoke_operator(context)
entity, image = get_resize_image_entity_and_image(self, context)
if image:
self.width = image.size[0]
self.height = image.size[1]
if image.source == 'TILED':
tile = image.tiles.get(int(self.tile_number))
if tile:
self.width = tile.size[0]
self.height = tile.size[1]
elif entity and image.yia.is_image_atlas:
segment = image.yia.segments.get(entity.segment_name)
self.width = segment.width
self.height = segment.height
return context.window_manager.invoke_props_dialog(self, width=320)
def draw(self, context):
image = bpy.data.images.get(self.image_name)
row = split_layout(self.layout, 0.4)
col = row.column(align=False)
col.label(text='Width:')
col.label(text='Height:')
if image:
if image.yia.is_image_atlas or not is_bl_newer_than(2, 81):
col.label(text='Samples:')
if image.source == 'TILED':
col.label(text='')
if not self.all_tiles:
col.label(text='Tile Number:')
col = row.column(align=False)
col.prop(self, 'width', text='')
col.prop(self, 'height', text='')
if image:
if image.yia.is_image_atlas or not is_bl_newer_than(2, 81):
col.prop(self, 'samples', text='')
if image.source == 'TILED':
if image.yua.is_udim_atlas:
col.prop(self, 'all_tiles', text='Resize All Atlas Segment Tiles')
else: col.prop(self, 'all_tiles')
if not self.all_tiles:
col.prop(self, 'tile_number', text='')
def execute(self, context):
yp = get_active_ypaint_node().node_tree.yp
entity, image = get_resize_image_entity_and_image(self, context)
if not entity or not image:
self.report({'ERROR'}, "There is no active image!")
return {'CANCELLED'}
# Get original size
segment = None
if image.yia.is_image_atlas:
segment = image.yia.segments.get(entity.segment_name)
ori_width = segment.width
ori_height = segment.height
if image.source == 'TILED':
tile = image.tiles.get(int(self.tile_number))
ori_width = tile.size[0]
ori_height = tile.size[1]
else:
ori_width = image.size[0]
ori_height = image.size[1]
if ori_width == self.width and ori_height == self.height:
self.report({'ERROR'}, "This image already had the same size!")
return {'CANCELLED'}
override_context = None
space = None
ori_space_image = None
if not image.yia.is_image_atlas and is_bl_newer_than(2, 81):
tilenums = [int(self.tile_number)]
if image.source == 'TILED' and self.all_tiles:
if image.yua.is_udim_atlas:
segment = image.yua.segments.get(entity.segment_name)
tilenums = UDIM.get_udim_segment_tilenums(segment)
else:
tilenums = [t.number for t in image.tiles]
ori_ui_type = bpy.context.area.ui_type
bpy.context.area.ui_type = 'UV'
bpy.context.space_data.image = image
for tilenum in tilenums:
if image.source == 'TILED':
tile = image.tiles.get(tilenum)
if not tile: continue
image.tiles.active = tile
bpy.ops.image.resize(size=(self.width, self.height))
bpy.context.area.ui_type = ori_ui_type
else:
scaled_img, new_segment = resize_image(
image, self.width, self.height, image.colorspace_settings.name,
self.samples, 0, segment, bake_device=self.bake_device, yp=yp
)
if new_segment:
entity.segment_name = new_segment.name
source = get_entity_source(entity)
source.image = scaled_img
segment.unused = True
update_mapping(entity)
# Update UV neighbor resolution
set_uv_neighbor_resolution(entity)
# Refresh active layer
yp.active_layer_index = yp.active_layer_index
return {'FINISHED'}
class YBakeChannelToVcol(bpy.types.Operator, BaseBakeOperator):
"""Bake Channel to Vertex Color"""
bl_idname = "node.y_bake_channel_to_vcol"
bl_label = "Bake channel to vertex color"
bl_options = {'REGISTER', 'UNDO'}
all_materials : BoolProperty(
name = 'Bake All Materials',
description = 'Bake all materials with ucupaint nodes rather than just the active one',
default = False
)
vcol_name : StringProperty(
name = 'Target Vertex Color Name',
description = "Target vertex color name, it will create one if it doesn't exist",
default = ''
)
add_emission : BoolProperty(
name = 'Add Emission',
description = 'Add the result with Emission Channel',
default = False
)
emission_multiplier : FloatProperty(
name = 'Emission Multiplier',
description = 'Emission multiplier so the emission can be more visible on the result',
default=1.0, min=0.0
)
force_first_index : BoolProperty(
name = 'Force First Index',
description = "Force target vertex color to be first on the vertex colors list (useful for exporting)",
default = True
)
include_alpha : BoolProperty(
name = 'Include Alpha',
description = "Bake channel alpha to result (need channel enable alpha)",
default = False
)
bake_to_alpha_only : BoolProperty(
name = 'Bake To Alpha Only',
description = "Bake value into the alpha",
default = False
)
@classmethod
def poll(cls, context):
return get_active_ypaint_node() and context.object.type == 'MESH'
@classmethod
def description(self, context, properties):
return get_operator_description(self)
def invoke(self, context, event):
self.invoke_operator(context)
node = get_active_ypaint_node()
yp = node.node_tree.yp
channel = yp.channels[yp.active_channel_index]
self.vcol_name = 'Baked ' + channel.name
# Add emission will only be available if it's on Color channel
self.show_emission_option = False
if channel.name == 'Color':
for ch in yp.channels:
if ch.name == 'Emission':
self.show_emission_option = True
# Only the 'RGB' type has alpha data
self.show_include_alpha_option = False
if channel.type == 'RGB':
self.show_include_alpha_option = True
# The type 'VALUE' can optionally be directly into the alpha channel
self.show_bake_to_alpha_only_option = False
if channel.type == 'VALUE':
self.show_bake_to_alpha_only_option = True
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):
return True
def draw(self, context):
row = split_layout(self.layout, 0.4)
col = row.column(align=True)
col.label(text='Target Vertex Color:')
if self.show_emission_option:
col.label(text='Add Emission:')
col.label(text='Emission Multiplier:')
if self.show_include_alpha_option:
col.label(text='Include Alpha:')
if self.show_bake_to_alpha_only_option:
col.label(text='Bake to Alpha:')
if not is_bl_equal(3, 2):
col.label(text='Force First Index:')
col = row.column(align=True)
col.prop(self, 'vcol_name', text='')
if self.show_emission_option:
col.prop(self, 'add_emission', text='')
col.prop(self, 'emission_multiplier', text='')
if self.show_include_alpha_option:
col.prop(self, 'include_alpha', text='')
if self.show_bake_to_alpha_only_option:
col.prop(self, 'bake_to_alpha_only', text='')
if not is_bl_equal(3, 2):
col.prop(self, 'force_first_index', text='')
def execute(self, context):
if not is_bl_newer_than(2, 92):
self.report({'ERROR'}, "You need at least Blender 2.92 to use this feature!")
return {'CANCELLED'}
mat = get_active_material()
node = get_active_ypaint_node()
yp = node.node_tree.yp
channel = yp.channels[yp.active_channel_index]
channel_name = channel.name
book = remember_before_bake(yp)
if self.all_materials:
mats = get_all_materials_with_yp_nodes()
else: mats = [mat]
for mat in mats:
for node in mat.node_tree.nodes:
if node.type != 'GROUP' or not node.node_tree or not node.node_tree.yp.is_ypaint_node: continue
tree = node.node_tree
yp = tree.yp
channel = yp.channels.get(channel_name)
if not channel: continue
# Get all objects using material