-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
UDIM.py
1271 lines (960 loc) · 40.8 KB
/
UDIM.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, numpy, os, tempfile, shutil, time, pathlib
from bpy.props import *
from .common import *
from . import lib, BakeInfo
UDIM_DIR = 'UDIM__'
UV_TOLERANCE = 0.1
def is_udim_supported():
return is_bl_newer_than(3, 3)
def fill_tiles(image, color=None, width=0, height=0, empty_only=False):
if image.source != 'TILED': return
if color == None: color = image.yui.base_color
for tile in image.tiles:
fill_tile(image, tile.number, color, width, height, empty_only)
def fill_tile(image, tilenum, color=None, width=0, height=0, empty_only=False):
if image.source != 'TILED': return False
if color == None: color = image.yui.base_color
tile = image.tiles.get(tilenum)
if width == 0: width = image.size[0]
if height == 0: height = image.size[1]
if width == 0: width = 1024
if height == 0: height = 1024
# HACK: For some reason 1001 tile is always exists when using get
# Check if it actually exists by comparing the returned tile number
if not tile or tile.number != tilenum:
# Create new tile
override = bpy.context.copy()
override['edit_image'] = image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.tile_add(number=tilenum, count=1, label="", color=color, width=width, height=height, float=image.is_float, alpha=True)
else: bpy.ops.image.tile_add(override, number=tilenum, count=1, label="", color=color, width=width, height=height, float=image.is_float, alpha=True)
elif not empty_only:
image.tiles.active = tile
override = bpy.context.copy()
override['edit_image'] = image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.tile_fill(color=color, width=width, height=height, float=image.is_float, alpha=True)
else: bpy.ops.image.tile_fill(override, color=color, width=width, height=height, float=image.is_float, alpha=True)
else:
return False
color_str = '('
color_str += str(color[0]) + ', '
color_str += str(color[1]) + ', '
color_str += str(color[2]) + ', '
color_str += str(color[3]) + ')'
print('UDIM: Filling tile ' + str(tilenum) + ' with color ' + color_str)
return True
def copy_udim_pixels(src, dest):
for tile in src.tiles:
# Check if tile number exists on both images and has same sizes
dtile = dest.tiles.get(tile.number)
if not dtile: continue
if tile.size[0] != dtile.size[0] or tile.size[1] != dtile.size[1]: continue
# Swap first
if tile.number != 1001:
swap_tile(src, 1001, tile.number)
swap_tile(dest, 1001, tile.number)
# Set pixels
dest.pixels = list(src.pixels)
# Swap back
if tile.number != 1001:
swap_tile(src, 1001, tile.number)
swap_tile(dest, 1001, tile.number)
def get_tile_numbers(objs, uv_name):
tiles = [1001]
if not is_udim_supported(): return tiles
T = time.time()
# Get active object
obj = bpy.context.object
ori_mode = 'OBJECT'
if obj in objs and obj.mode != 'OBJECT':
ori_mode = obj.mode
bpy.ops.object.mode_set(mode='OBJECT')
arr = numpy.empty(0, dtype=numpy.float32)
# Get all uv coordinates
for o in objs:
if o.type != 'MESH': continue
uv = o.data.uv_layers.get(uv_name)
if not uv: continue
uv_arr = numpy.zeros(len(o.data.loops) * 2, dtype=numpy.float32)
uv.data.foreach_get('uv', uv_arr)
arr = numpy.append(arr, uv_arr)
# Reshape the array to 2D
arr.shape = (arr.shape[0] // 2, 2)
# Tolerance to skip value around x.0
trange = [UV_TOLERANCE / 2.0, 1.0 - (UV_TOLERANCE / 2.0)]
arr = arr[
((arr[:,0] - (numpy.floor(arr[:,0]))) >= trange[0]) &
((arr[:,0] - (numpy.floor(arr[:,0]))) <= trange[1]) &
((arr[:,1] - (numpy.floor(arr[:,1]))) >= trange[0]) &
((arr[:,1] - (numpy.floor(arr[:,1]))) <= trange[1])
]
# Floor array to integer
arr = numpy.floor(arr).astype(int)
# Get unique value only
arr = numpy.unique(arr, axis=0)
# Get the udim representation
for i in arr:
# UV value can only be within 0 .. 10 range
u = min(max(i[0], 0), 10)
v = min(max(i[1], 0), 100)
# Calculate the tile
tile = 1001 + u + v * 10
if tile not in tiles:
tiles.append(tile)
if ori_mode != 'OBJECT':
bpy.ops.object.mode_set(mode=ori_mode)
print('INFO: Getting tile numbers is done in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
return tiles
def is_uvmap_udim(objs, uv_name):
if not is_udim_supported(): return False
T = time.time()
# Get active object
obj = bpy.context.object
ori_mode = 'OBJECT'
if obj in objs and obj.mode != 'OBJECT':
ori_mode = obj.mode
bpy.ops.object.mode_set(mode='OBJECT')
arr = numpy.empty(0, dtype=numpy.float32)
# Get all uv coordinates
for o in objs:
if o.type != 'MESH': continue
uv = o.data.uv_layers.get(uv_name)
if not uv: continue
uv_arr = numpy.zeros(len(o.data.loops) * 2, dtype=numpy.float32)
uv.data.foreach_get('uv', uv_arr)
arr = numpy.append(arr, uv_arr)
if ori_mode != 'OBJECT':
bpy.ops.object.mode_set(mode=ori_mode)
is_udim = numpy.any(arr > 1.0 + UV_TOLERANCE / 2)
print('INFO: UDIM checking is done in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
return is_udim
def get_temp_udim_dir():
if bpy.data.filepath != '':
directory = os.path.dirname(bpy.data.filepath)
return os.path.join(directory, UDIM_DIR)
return tempfile.gettempdir()
def is_using_temp_dir(image):
directory = os.path.dirname(bpy.path.abspath(image.filepath))
if directory == get_temp_udim_dir() or (bpy.data.filepath == '' and directory == tempfile.gettempdir()) or os.sep + UDIM_DIR + os.sep in image.filepath:
return True
return False
def remove_udim_files_from_disk(image, directory, remove_dir=False, tilenum=-1):
# Get filenames
img_names = []
filename = bpy.path.basename(image.filepath)
prefix = filename.split('.<UDIM>.')[0]
if os.path.isdir(directory):
for f in os.listdir(directory):
m = re.match(r'' + re.escape(prefix) + '\.(\d{4})\.*', f)
if m:
if tilenum != -1 and tilenum != int(m.group(1)): continue
img_names.append(f)
# Remove images
for f in img_names:
try: os.remove(os.path.join(directory, f))
except Exception as e: print(e)
# Remove directory with all the empty parents
if remove_dir and directory != tempfile.gettempdir():
cur_dir = pathlib.Path(directory)
while True:
# Only remove when the directory is empty
if os.path.isdir(cur_dir) and len(os.listdir(cur_dir)) == 0:
try: os.rmdir(cur_dir)
except Exception as e: print(e)
# Get the parent
parent_dir = cur_dir.parent
# Break if parent is not empty
if parent_dir == cur_dir or (os.path.isdir(parent_dir) and len(os.listdir(parent_dir)) > 0):
break
# Set current path to parent path
cur_dir = parent_dir
def get_udim_filepath(filename, directory):
filepath = os.path.join(directory, filename + '.<UDIM>.png')
if directory != tempfile.gettempdir():
try: filepath = bpy.path.relpath(filepath)
except: pass
#if not os.path.exists(directory):
# os.makedirs(directory)
return filepath
def save_udim(image):
override = bpy.context.copy()
override['edit_image'] = image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.save_as(filepath=bpy.path.abspath(image.filepath), relative_path=True)
#bpy.ops.image.save()
else:
bpy.ops.image.save_as(override, filepath=bpy.path.abspath(image.filepath), relative_path=True)
#bpy.ops.image.save(override)
def save_as_udim(image, filepath=''):
if filepath == '': filepath = image.filepath
override = bpy.context.copy()
override['edit_image'] = image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.save_as(filepath=bpy.path.abspath(filepath), relative_path=True)
else: bpy.ops.image.save_as(override, filepath=bpy.path.abspath(filepath), relative_path=True)
def pack_udim(image):
# NOTE: Empty tiles can cause error with packing, so there's a need to remove them
# Check if there's empty tiles
empty_numbers = []
for tile in image.tiles:
if tile.channels == 0:
empty_numbers.append(tile.number)
# Remove if there's empty tiles
if len(empty_numbers) > 0:
for number in empty_numbers:
tile = image.tiles.get(number)
if tile and tile.number == number:
image.tiles.active = tile
override = bpy.context.copy()
override['edit_image'] = image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.tile_remove()
else: bpy.ops.image.tile_remove(override)
# Save udim first before packing the image
save_udim(image)
image.pack()
# UDIM need filepath to work,
# So there's need to initialize filepath for every udim image created
def initial_pack_udim(image, base_color=None, filename='', force_temp_dir=False):
# Get temporary directory
temp_dir = get_temp_udim_dir()
# Check if image is already packed
use_packed = False
if image.packed_file: use_packed = True
# Check if image already use temporary filepath
use_temp_dir = is_using_temp_dir(image)
# Set temporary filepath
filepath = image.filepath
directory = os.path.dirname(bpy.path.abspath(filepath))
if (filepath == '' or # Set image filepath if it's still empty
not is_image_filepath_unique(filepath) or # Force set new filepath when image filepath is not unique
(force_temp_dir and bpy.data.filepath != '') or # Force temporary directory
(not use_temp_dir and not os.path.isdir(directory)) # When blend file is copied to another PC, there's a chance directory is missing
):
filename = filename if filename != '' else image.name
# Get temp filepath
filepath = get_udim_filepath(filename, temp_dir)
use_temp_dir = True
# Save then pack
save_as_udim(image, filepath)
if use_packed or use_temp_dir:
pack_udim(image)
# Remove temporary files
if use_temp_dir:
remove_udim_files_from_disk(image, temp_dir, True)
# Remember base color
if base_color:
image.yui.base_color = base_color
def swap_tiles(image, swap_dict, reverse=False):
# Directory of image
directory = os.path.dirname(bpy.path.abspath(image.filepath))
# Remember stuff
ori_packed = False
if image.packed_file: ori_packed = True
# Image saved flag
image_saved = False
iterator = reversed(swap_dict) if reverse else swap_dict
for tilenum0 in iterator:
tilenum1 = swap_dict[tilenum0]
tile0 = image.tiles.get(tilenum0)
tile1 = image.tiles.get(tilenum1)
if not tile0 or not tile1: continue
if tilenum0 == tilenum1: continue
# Save the image first
if not image_saved:
save_udim(image)
image_saved = True
print('UDIM: Swapping tile', tilenum0, 'to', tilenum1)
str0 = '.' + str(tilenum0) + '.'
str1 = '.' + str(tilenum1) + '.'
filename = bpy.path.basename(image.filepath)
prefix = filename.split('.<UDIM>.')[0]
# Get image paths
path0 = ''
path1 = ''
for f in os.listdir(directory):
m = re.match(r'' + re.escape(prefix) + '\.\d{4}\.*', f)
if m:
if str0 in f: path0 = os.path.join(directory, f)
elif str1 in f: path1 = os.path.join(directory, f)
# Swap paths
temp_path = path0.replace(str0, '.xxxx.')
os.rename(path0, temp_path)
os.rename(path1, path0)
os.rename(temp_path, path1)
if image_saved:
# Reload to update image
image.reload()
save_udim(image)
# Repack image
if ori_packed:
pack_udim(image)
# Remove file if they are using temporary directory
if is_using_temp_dir(image):
remove_udim_files_from_disk(image, directory, True)
def swap_tile(image, tilenum0, tilenum1):
swap_dict = {}
swap_dict[tilenum0] = tilenum1
swap_tiles(image, swap_dict)
def copy_tiles(image0, image1, copy_dict):
# Directory of images
directory0 = os.path.dirname(bpy.path.abspath(image0.filepath))
directory1 = os.path.dirname(bpy.path.abspath(image1.filepath))
# Remember stuff
ori0_packed = False
ori1_packed = False
if image0.packed_file: ori0_packed = True
if image1.packed_file: ori1_packed = True
# Image saved flag
image_saved = False
for tilenum0, tilenum1 in copy_dict.items():
tile0 = image0.tiles.get(tilenum0)
tile1 = image1.tiles.get(tilenum1)
if not tile0 or not tile1: continue
# Get image paths
str0 = '.' + str(tilenum0) + '.'
str1 = '.' + str(tilenum1) + '.'
filename0 = bpy.path.basename(image0.filepath)
filename1 = bpy.path.basename(image1.filepath)
splits0 = filename0.split('.<UDIM>.')
splits1 = filename1.split('.<UDIM>.')
prefix0 = splits0[0]
prefix1 = splits1[0]
suffix0 = splits0[1]
suffix1 = splits1[1]
path0 = os.path.join(directory0, prefix0 + str0 + suffix0)
path1 = os.path.join(directory1, prefix1 + str1 + suffix1)
if suffix0 != suffix1: continue
if path0 == path1: continue
# Save the image first
if not image_saved:
save_udim(image0)
save_udim(image1)
image_saved = True
print('UDIM: Copying tile', tilenum0, '(' + image0.name + ') to', tilenum1, '(' + image1.name + ')')
# Copy and replace image
if os.path.exists(path1):
os.remove(path1)
shutil.copyfile(path0, path1)
if image_saved:
# Reload to update image
#image0.reload()
image1.reload()
#save_udim(image0)
save_udim(image1)
# Repack image 0
if ori0_packed:
pack_udim(image0)
# Repack image 1
if ori1_packed:
pack_udim(image1)
if ori0_packed:
# Remove file if they are using temporary directory
if is_using_temp_dir(image0):
remove_udim_files_from_disk(image0, directory0, True)
if ori1_packed:
# Remove file if they are using temporary directory
if is_using_temp_dir(image1):
remove_udim_files_from_disk(image1, directory1, True)
def remove_tiles(image, tilenums):
#print('UDIM: Removing tiles is starting...')
# Directory of image
directory = os.path.dirname(bpy.path.abspath(image.filepath))
# Remember stuff
ori_packed = False
if image.packed_file: ori_packed = True
# Image saved flag
image_saved = False
for tilenum in tilenums:
tile = image.tiles.get(tilenum)
if not tile: continue
# Save the image first
if not image_saved:
save_udim(image)
image_saved = True
print('UDIM: Removing tile', tilenum)
# Remove tile
image.tiles.remove(tile)
# Repack image
if image_saved:
if ori_packed:
pack_udim(image)
# Remove file if they are using temporary directory
if is_using_temp_dir(image):
remove_udim_files_from_disk(image, directory, True)
else:
# Remove file
remove_udim_files_from_disk(image, directory, False, tilenum)
def remove_tile(image, tilenum):
remove_tiles(image, [tilenum])
class YRefillUDIMTiles(bpy.types.Operator):
bl_idname = "node.y_refill_udim_tiles"
bl_label = "Refill UDIM Tiles"
bl_description = "Refill all UDIM tiles used by all layers and masks based on their UV"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return get_active_ypaint_node()
def execute(self, context):
T = time.time()
yp = context.layer.id_data.yp
entities, images, segment_names, segment_name_props = get_yp_entities_images_and_segments(yp)
mat = get_active_material()
refreshed_images = []
for i, image in enumerate(images):
if image.source != 'TILED': continue
ents = entities[i]
entity = ents[0]
if image.yua.is_udim_atlas:
if image not in refreshed_images:
refresh_udim_atlas(image, yp)
refreshed_images.append(image)
else:
# Get tile numbers based from uv
uv_name = entity.uv_name if not entity.use_baked else entity.baked_uv_name
objs = get_all_objects_with_same_materials(mat, True, uv_name)
tilenums = get_tile_numbers(objs, uv_name)
# Get width and height
width = 1024
height = 1024
if image.size[0] != 0: width = image.size[0]
if image.size[1] != 0: height = image.size[1]
color = image.yui.base_color
for tilenum in tilenums:
fill_tile(image, tilenum, color, width, height, empty_only=True)
initial_pack_udim(image)
print('INFO: Refilling UDIM is done in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
return {'FINISHED'}
def udim_tilenum_items(self, context):
image = bpy.data.images.get(self.image_name)
if image.yua.is_udim_atlas:
yp = get_active_ypaint_node().node_tree.yp
layer = yp.layers.get(self.layer_name)
# Get entity
entity = layer
mask_entity = False
for mask in layer.masks:
if mask.active_edit:
entity = mask
mask_entity = True
break
segment_name = entity.segment_name if not entity.use_baked else entity.baked_segment_name
segment = image.yua.segments.get(segment_name)
tilenums = get_udim_segment_tilenums(segment)
else:
tilenums = [t.number for t in image.tiles]
items = []
for i, tilenum in enumerate(tilenums):
items.append((str(tilenum), str(tilenum), '', 'IMAGE_DATA', i))
return items
def get_udim_segment_index(image, segment):
index = -1
ids = [i for i, s in enumerate(image.yua.segments) if s == segment]
if len(ids) > 0: index = ids[0]
return index
def get_udim_segment_tilenums(segment):
image = segment.id_data
offset_y = get_udim_segment_mapping_offset(segment)
tilenums = []
for btile in segment.base_tiles:
tilenum = btile.number + offset_y * 10
tilenums.append(tilenum)
return tilenums
def get_udim_segment_base_tilenums(segment):
return [btile.number for btile in segment.base_tiles]
def set_udim_segment_mapping(entity, segment, image, use_baked=False):
offset_y = get_udim_segment_mapping_offset(segment)
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: mapping = get_layer_mapping(entity, get_baked=use_baked)
else: mapping = get_mask_mapping(entity, get_baked=use_baked)
if mapping: mapping.inputs[1].default_value[1] = offset_y
def create_udim_atlas(tilenums, name='', width=1024, height=1024, color=(0, 0, 0, 0), colorspace='', hdr=False):
if name != '':
name = '~' + name + ' UDIM Atlas'
else: name = '~UDIM Atlas'
# Get offset based on max y value
max_y = int((max(tilenums) - 1000) / 10)
offset_y = max_y + 2
name = get_unique_name(name, bpy.data.images)
image = bpy.data.images.new(
name=name, width=width, height=height,
alpha=True, float_buffer=hdr, tiled=True
)
image.yua.is_udim_atlas = True
image.yui.base_color = color
# Pack image
initial_pack_udim(image)
# Set colorspace
if colorspace != '' and image.colorspace_settings.name != colorspace: image.colorspace_settings.name = colorspace
return image
def refresh_udim_segment_base_tilenums(segment, tilenums):
# Add tiles
for tilenum in tilenums:
if str(tilenum) not in segment.base_tiles:
btile = segment.base_tiles.add()
btile.name = str(tilenum)
btile.number = tilenum
# Remove unused tiles
for i, btile in reversed(list(enumerate(segment.base_tiles))):
if btile.number not in tilenums:
segment.base_tiles.remove(i)
def create_udim_atlas_segment(image, tilenums, width=1024, height=1024, color=(0, 0, 0, 0), source_image=None, source_tilenums=[], yp=None):
#if yp: refresh_udim_atlas(image, yp)
# Make sure filepath is not empty
if image.filepath == '': initial_pack_udim(image)
atlas = image.yua
name = get_unique_name('Segment', atlas.segments)
segment = None
segment = atlas.segments.add()
segment.name = name
segment.base_color = color
refresh_udim_segment_base_tilenums(segment, tilenums)
offset = get_udim_segment_mapping_offset(segment) * 10
copy_dict = {}
for i, tilenum in enumerate(tilenums):
if source_image:
if source_tilenums != []:
source_tile = source_image.tiles.get(source_tilenums[i])
else: source_tile = source_image.tiles.get(tilenum)
width = source_tile.size[0]
height = source_tile.size[1]
if source_tilenums != []:
copy_dict[source_tilenums[i]] = tilenum + offset
else: copy_dict[tilenum] = tilenum + offset
tilenum += offset
fill_tile(image, tilenum, color, width, height, empty_only=False)
# Copy from source image
if source_image:
copy_tiles(source_image, image, copy_dict)
# Pack image
initial_pack_udim(image, force_temp_dir=True)
return segment
def is_tilenums_fit_in_udim_atlas(image, tilenums):
max_y = int((max(tilenums) - 1000) / 10)
atlas_tilenums = [t.number for t in image.tiles]
if len(atlas_tilenums) > 0:
atlas_max_y = int((max(atlas_tilenums) - 1000) / 10) + 1
else: atlas_max_y = 0
remains_y = 99 - atlas_max_y
return remains_y > max_y
def get_set_udim_atlas_segment(
tilenums,
width=1024, height=1024, color=(0, 0, 0, 0), colorspace='', hdr=False, yp=None,
source_image=None, source_tilenums=[],
image_exception=None, image_inclusions=[]
):
ypup = get_user_preferences()
segment = None
# Get bunch of images
if yp: #and ypup.unique_image_atlas_per_yp:
images = get_yp_images(yp, udim_only=True)
name = yp.id_data.name
else:
images = [img for img in bpy.data.images if img.source == 'TILED']
name = ''
# Extra images to be included
for image in image_inclusions:
if image not in images:
images.append(image)
for image in images:
if image_exception and image == image_exception: continue
if image.yua.is_udim_atlas and image.is_float == hdr and is_tilenums_fit_in_udim_atlas(image, tilenums):
if colorspace != '' and image.colorspace_settings.name != colorspace: continue
segment = create_udim_atlas_segment(
image, tilenums, width, height, color,
source_image=source_image, source_tilenums=source_tilenums, yp=yp
)
if segment:
break
if not segment:
# If proper UDIM atlas can't be found, create new one
image = create_udim_atlas(tilenums, name, width, height, color, colorspace, hdr)
segment = create_udim_atlas_segment(
image, tilenums, width, height, color,
source_image=source_image, source_tilenums=source_tilenums, yp=yp
)
return segment
def get_all_udim_atlas_tilenums(image, tilenums=[]):
all_tilenums = []
for segment in image.yua.segments:
tilenums = get_udim_segment_tilenums(segment)
all_tilenums.extend(tilenums)
return all_tilenums
def rearrange_tiles(image, convert_dict):
# Directory of images
directory = os.path.dirname(bpy.path.abspath(image.filepath))
# Remember stuff
ori_packed = False
if image.packed_file: ori_packed = True
# Image saved flag
image_saved = False
already_renamed = []
# First pass of renaming
for tilenum0, tilenum1 in convert_dict.items():
tile = image.tiles.get(tilenum0)
if not tile: continue
# Get image paths
str0 = '.' + str(tilenum0) + '.'
str1 = '.' + str(tilenum1) + '.'
filename = bpy.path.basename(image.filepath)
splits = filename.split('.<UDIM>.')
prefix = splits[0]
suffix = splits[1]
path0 = os.path.join(directory, prefix + str0 + suffix)
path1 = os.path.join(directory, prefix + str1 + suffix)
# Save the image first
if not image_saved:
save_udim(image)
image_saved = True
print('UDIM: Rename tile', tilenum0, 'to', tilenum1, '(' + image.name + ')')
# Copy and replace image
if os.path.exists(path1):
if tilenum1 in convert_dict:
path1 += '.TEMP_NAME'
else: os.remove(path1)
#shutil.copyfile(path0, path1)
os.rename(path0, path1)
# Second pass is removing temporary name suffix
if os.path.isdir(directory):
temp_names = []
ori_names = []
for f in os.listdir(directory):
if f.endswith('.TEMP_NAME'):
temp_names.append(f)
ori_names.append(f.split('.TEMP_NAME')[0])
for i, temp_name in enumerate(temp_names):
temp_path = os.path.join(directory, temp_name)
ori_path = os.path.join(directory, ori_names[i])
if os.path.exists(ori_path):
os.remove(ori_path)
os.rename(temp_path, ori_path)
if image_saved:
# Reload to update image
image.reload()
save_udim(image)
# Repack image
if ori_packed:
pack_udim(image)
# Remove file if they are using temporary directory
if is_using_temp_dir(image):
remove_udim_files_from_disk(image, directory, True)
def refresh_udim_atlas(image, yp=None, check_uv=True, remove_index=-1):
T = time.time()
# Actual tilenums from the image
cur_tilenums = [t.number for t in image.tiles]
if not yp: yp = get_active_ypaint_node().node_tree.yp
entities = get_yp_entites_using_same_image(yp, image)
# Create conversion dict
convert_dict = {}
uv_tilenums_dict = {}
new_tilenums_dict = {}
out_of_bound_segment_names = []
ori_offset_y = 0
new_offset_y = 0
for i, segment in enumerate(image.yua.segments):
# Get original base tilenums
ori_tilenums = new_tilenums = get_udim_segment_base_tilenums(segment)
# Get UV name
if check_uv:
uv_name = ''
ents = [ent for ent in entities if ent.segment_name == segment.name]
if ents: uv_name = ents[0].uv_name
if uv_name == '':
ents = [ent for ent in entities if ent.baked_segment_name == segment.name]
if ents: uv_name = ents[0].uv_name if ents[0].baked_uv_name == '' else ents[0].baked_uv_name
# Get new tilenums based on uv
if uv_name != '':
if uv_name not in uv_tilenums_dict:
mat = get_active_material()
objs = get_all_objects_with_same_materials(mat, True, uv_name)
new_tilenums = uv_tilenums_dict[uv_name] = get_tile_numbers(objs, uv_name)
else: new_tilenums = uv_tilenums_dict[uv_name]
# Remember new tilenums
new_tilenums_dict[segment.name] = new_tilenums
# Skip for to be removed index
if i != remove_index:
# Fill conversion dict
tile_convert_dict = {}
out_of_bound = False
for nt in new_tilenums:
new_index = nt + new_offset_y * 10
if new_index > 2000:
out_of_bound = True
else:
if nt in ori_tilenums:
ori_index = nt + ori_offset_y * 10
if ori_index != new_index:
tile_convert_dict[ori_index] = new_index
if out_of_bound:
out_of_bound_segment_names.append(segment.name)
else:
convert_dict.update(tile_convert_dict)
# Add tilenums height to original offset
ori_offset_y += get_tilenums_height(ori_tilenums) + 1
# Skip for to be removed index
if i != remove_index:
# Add tilenums height to new offset
new_offset_y += get_tilenums_height(new_tilenums) + 1
# If there are out of bound segments, create new segments
oob_dict = {}
new_atlas_images = []
for name in out_of_bound_segment_names:
segment = image.yua.segments.get(name)
segment_base_tilenums = get_udim_segment_base_tilenums(segment)
segment_tilenums = get_udim_segment_tilenums(segment)
new_segment = get_set_udim_atlas_segment(
segment_base_tilenums, color=segment.base_color,
colorspace=image.colorspace_settings.name, hdr=image.is_float, yp=yp,
source_image=image, source_tilenums=segment_tilenums,
image_exception=image, image_inclusions=new_atlas_images
)
oob_dict[name] = new_segment
if new_segment.id_data not in new_atlas_images:
new_atlas_images.append(new_segment.id_data)
# Remove out of bound segments
for name in out_of_bound_segment_names:
segment = image.yua.segments.get(name)
index = get_udim_segment_index(image, segment)
image.yua.segments.remove(index)
# If remove index exists
if remove_index != -1:
image.yua.segments.remove(remove_index)
# Set new tilenums
for segment in image.yua.segments:
new_tilenums = new_tilenums_dict[segment.name]
refresh_udim_segment_base_tilenums(segment, new_tilenums)
# Check for out of bounds segments
#segment_tilenums = get_udim_segment_tilenums(segment)
# Extend tilenums
tilenums = get_all_udim_atlas_tilenums(image)
# Fill tiles
dirty = False
for tilenum in tilenums:
if fill_tile(image, tilenum, empty_only=True):
dirty = True
# Pack after fill
if dirty or image.filepath == '' or not is_using_temp_dir(image): initial_pack_udim(image, force_temp_dir=True)
# Rearrange tiles
rearrange_tiles(image, convert_dict)
# Fill tiles again in case there's empty tiles
dirty = False
for tilenum in tilenums:
if fill_tile(image, tilenum, empty_only=True):
dirty = True
# Pack after fill again once more
if dirty or image.filepath == '' or not is_using_temp_dir(image): initial_pack_udim(image, force_temp_dir=True)