-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
image_ops.py
1245 lines (1003 loc) · 44.9 KB
/
image_ops.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, os
import tempfile
from bpy.props import *
from bpy_extras.io_utils import ExportHelper
from .common import *
import time
from . import UDIM
def save_float_image(image):
original_path = image.filepath
# Create temporary scene
tmpscene = bpy.data.scenes.new('Temp Scene')
# Set settings
settings = tmpscene.render.image_settings
# Check current extensions
for form, ext in format_extensions.items():
if image.filepath.endswith(ext):
if form == 'OPEN_EXR_MULTILAYER' and image.type != 'MULTILAYER': continue
settings.file_format = form
break
if settings.file_format in {'OPEN_EXR', 'OPEN_EXR_MULTILAYER'}:
settings.exr_codec = 'ZIP'
settings.color_depth = '32'
elif settings.file_format in {'PNG', 'TIFF'}:
settings.color_depth = '16'
#ori_colorspace = image.colorspace_settings.name
full_path = bpy.path.abspath(image.filepath)
image.save_render(full_path, scene=tmpscene)
# HACK: If image still dirty after saving, save using standard save method
if image.is_dirty: image.save()
image.source = 'FILE'
# Delete temporary scene
remove_datablock(bpy.data.scenes, tmpscene)
def pack_float_image(image):
original_path = image.filepath
# Create temporary scene
tmpscene = bpy.data.scenes.new('Temp Scene')
# Set settings
settings = tmpscene.render.image_settings
#if image.filepath == '':
#if original_path == '':
if bpy.path.basename(original_path) == '':
if hasattr(image, 'use_alpha') and image.use_alpha:
settings.file_format = 'PNG'
settings.color_depth = '16'
#settings.color_mode = 'RGBA'
settings.compression = 15
image_name = '_temp_image.png'
else:
settings.file_format = 'HDR'
settings.color_depth = '32'
image_name = '_temp_image.hdr'
else:
settings.file_format = image.file_format
if image.file_format in {'CINEON', 'DPX'}:
settings.color_depth = '10'
elif image.file_format in {'TIFF'}:
settings.color_depth = '16'
elif image.file_format in {'HDR', 'OPEN_EXR_MULTILAYER', 'OPEN_EXR'}:
settings.color_depth = '32'
else:
settings.color_depth = '16'
image_name = bpy.path.basename(original_path)
temp_filepath = os.path.join(tempfile.gettempdir(), image_name)
# Save image
image.save_render(temp_filepath, scene=tmpscene)
image.source = 'FILE'
image.filepath = temp_filepath
if image.file_format == 'PNG':
image.colorspace_settings.name = get_srgb_name()
else: image.colorspace_settings.name = get_noncolor_name()
# Delete temporary scene
remove_datablock(bpy.data.scenes, tmpscene)
# Pack image
image.pack()
#image.reload()
# Bring back to original path
image.filepath = original_path
os.remove(temp_filepath)
def clean_object_references(image):
removed_references = []
if image.yia.is_image_atlas:
for segment in image.yia.segments:
if segment.bake_info.is_baked:
# Check if selected objects data are still accessible on any view layers
indices = []
for i, o in enumerate(segment.bake_info.selected_objects):
if o.object:
if is_bl_newer_than(2, 80):
if not any([s for s in bpy.data.scenes if o.object.name in s.collection.all_objects]):
removed_references.append(o.object.name)
indices.append(i)
else:
if not any([s for s in bpy.data.scenes if o.object.name in s.objects]):
removed_references.append(o.object.name)
indices.append(i)
for i in reversed(indices):
segment.bake_info.selected_objects.remove(i)
# Check if other object's data is still accessible on any view layers
indices = []
for i, o in enumerate(segment.bake_info.other_objects):
if o.object:
if is_bl_newer_than(2, 80):
if not any([s for s in bpy.data.scenes if o.object.name in s.collection.all_objects]):
removed_references.append(o.object.name)
indices.append(i)
else:
if not any([s for s in bpy.data.scenes if o.object.name in s.objects]):
removed_references.append(o.object.name)
indices.append(i)
for i in reversed(indices):
segment.bake_info.other_objects.remove(i)
elif image.y_bake_info.is_baked:
if image.y_bake_info.is_baked:
# Check if selected objects data are still accessible on any view layers
indices = []
for i, o in enumerate(image.y_bake_info.selected_objects):
if o.object:
if is_bl_newer_than(2, 80):
if not any([s for s in bpy.data.scenes if o.object.name in s.collection.all_objects]):
removed_references.append(o.object.name)
indices.append(i)
else:
if not any([s for s in bpy.data.scenes if o.object.name in s.objects]):
removed_references.append(o.object.name)
indices.append(i)
for i in reversed(indices):
image.y_bake_info.selected_objects.remove(i)
# Check if other object's data is still accessible on any view layers
indices = []
for i, o in enumerate(image.y_bake_info.other_objects):
if o.object:
if is_bl_newer_than(2, 80):
if not any([s for s in bpy.data.scenes if o.object.name in s.collection.all_objects]):
removed_references.append(o.object.name)
indices.append(i)
else:
if not any([s for s in bpy.data.scenes if o.object.name in s.objects]):
removed_references.append(o.object.name)
indices.append(i)
for i in reversed(indices):
image.y_bake_info.other_objects.remove(i)
for r in removed_references:
print('Reference for', r, "is removed because it's no longer found!")
def save_pack_all(yp):
images = get_yp_images(yp, get_baked_channels=True, check_overlay_normal=True)
packed_float_images = []
# Temporary scene for Blender 3.3 hack
tmpscene = None
# Save/pack images
for image in images:
if not image or not image.is_dirty: continue
T = time.time()
if image.packed_file or image.filepath == '':
if is_bl_newer_than(2, 80):
image.pack()
else:
if image.is_float:
pack_float_image(image)
packed_float_images.append(image)
else:
image.pack(as_png=True)
print('INFO:', image.name, 'image is packed in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
else:
if image.is_float:
save_float_image(image)
else:
# BLENDER BUG: Blender 3.3 has wrong srgb if not packed first
if is_bl_newer_than(3, 3) and image.colorspace_settings.name in {'Linear', get_noncolor_name()}:
# Create temporary scene
if not tmpscene:
print('INFO: Creating temporary scene for saving some images...')
tmpscene = bpy.data.scenes.new('Temp Save Scene')
try: tmpscene.view_settings.view_transform = 'Standard'
except: print('EXCEPTIION: Cannot set view transform on temporary save scene!')
try: tmpscene.render.image_settings.file_format = 'PNG'
except: print('EXCEPTIION: Cannot set file format on temporary save scene!')
# Get image path
path = bpy.path.abspath(image.filepath)
# Pack image first
image.pack()
image.colorspace_settings.name = get_srgb_name()
# Remove old files to avoid caching (?)
try: os.remove(path)
except Exception as e: print(e)
# Then unpack
default_dir, default_dir_found, default_filepath, temp_path, unpacked_path = unpack_image(image, path)
# Save image
image.save_render(path, scene=tmpscene)
# Set the filepath to the image
image.filepath = path
if bpy.data.filepath != '':
try: image.filepath = bpy.path.relpath(path)
except: pass
# Bring back linear
image.colorspace_settings.name = get_noncolor_name()
# Remove unpacked images in Blender 3.3
remove_unpacked_image_path(image, path, default_dir, default_dir_found, default_filepath, temp_path, unpacked_path)
print('INFO:', image.name, 'image is saved in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
else:
try:
ori_colorspace = image.colorspace_settings.name
image.save()
image.colorspace_settings.name = ori_colorspace
print('INFO:', image.name, 'image is saved in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
except Exception as e:
print(e)
# Delete temporary scene
if tmpscene:
print('INFO: Deleting temporary scene used for saving some images...')
remove_datablock(bpy.data.scenes, tmpscene)
# HACK: For some reason active float image will glitch after auto save
# This is only happen if active object is in texture paint mode
obj = bpy.context.object
if len(yp.layers) > 0 and obj and obj.mode == 'TEXTURE_PAINT':
layer = yp.layers[yp.active_layer_index]
if layer.type == 'IMAGE':
source = get_layer_source(layer)
image = source.image
if image in packed_float_images:
ypui = bpy.context.window_manager.ypui
ypui.refresh_image_hack = True
# Clean object reference on images
if is_bl_newer_than(2, 79):
for image in images:
clean_object_references(image)
class YInvertImage(bpy.types.Operator):
"""Invert Image"""
bl_idname = "node.y_invert_image"
bl_label = "Invert Image"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'image') and context.image
def execute(self, context):
if context.image.yia.is_image_atlas:
self.report({'ERROR'}, 'Cannot invert image atlas!')
return {'CANCELLED'}
# For some reason this no longer works since Blender 2.82, but worked again in Blender 4.2
if not is_bl_newer_than(2, 82) or is_bl_newer_than(4, 2):
override = bpy.context.copy()
override['edit_image'] = context.image
if is_bl_newer_than(4):
with bpy.context.temp_override(**override):
bpy.ops.image.invert(invert_r=True, invert_g=True, invert_b=True)
else: bpy.ops.image.invert(override, invert_r=True, invert_g=True, invert_b=True)
else:
ori_area_type = context.area.type
context.area.type = 'IMAGE_EDITOR'
space = context.area.spaces[0]
space.image = context.image
bpy.ops.image.invert(invert_r=True, invert_g=True, invert_b=True)
context.area.type = ori_area_type
return {'FINISHED'}
class YRefreshImage(bpy.types.Operator):
"""Reload Image"""
bl_idname = "node.y_reload_image"
bl_label = "Reload Image"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'image') and context.image
def execute(self, context):
# Reload image
context.image.reload()
# Refresh viewport and image editor
for area in context.screen.areas:
if area.type in ['VIEW_3D', 'IMAGE_EDITOR', 'NODE_EDITOR']:
area.tag_redraw()
return {'FINISHED'}
class YPackImage(bpy.types.Operator):
"""Pack Image"""
bl_idname = "node.y_pack_image"
bl_label = "Pack Image"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'image') and context.image and not context.image.packed_file
def execute(self, context):
T = time.time()
# Save file to temporary place first if image is float
if is_bl_newer_than(2, 80):
context.image.pack()
else:
if context.image.is_float:
pack_float_image(context.image)
else: context.image.pack(as_png=True)
context.image.filepath = ''
node = get_active_ypaint_node()
tree = node.node_tree
yp = tree.yp
if yp.use_baked and yp.active_channel_index < len(yp.channels):
ch = yp.channels[yp.active_channel_index]
if ch.type == 'NORMAL':
baked_disp = tree.nodes.get(ch.baked_disp)
if baked_disp and baked_disp.image and not baked_disp.image.packed_file:
if is_bl_newer_than(2, 80):
baked_disp.image.pack()
else:
if baked_disp.image.is_float:
pack_float_image(baked_disp.image)
else: baked_disp.image.pack(as_png=True)
baked_disp.image.filepath = ''
baked_vdisp = tree.nodes.get(ch.baked_vdisp)
if baked_vdisp and baked_vdisp.image and not baked_vdisp.image.packed_file:
if is_bl_newer_than(2, 80):
baked_vdisp.image.pack()
else:
if baked_vdisp.image.is_float:
pack_float_image(baked_vdisp.image)
else: baked_vdisp.image.pack(as_png=True)
baked_vdisp.image.filepath = ''
if not is_overlay_normal_empty(yp):
baked_normal_overlay = tree.nodes.get(ch.baked_normal_overlay)
if baked_normal_overlay and baked_normal_overlay.image and not baked_normal_overlay.image.packed_file:
if is_bl_newer_than(2, 80):
baked_normal_overlay.image.pack()
else:
if baked_normal_overlay.image.is_float:
pack_float_image(baked_normal_overlay.image)
else: baked_normal_overlay.image.pack(as_png=True)
baked_normal_overlay.image.filepath = ''
print('INFO:', context.image.name, 'image is packed in', '{:0.2f}'.format((time.time() - T) * 1000), 'ms!')
return {'FINISHED'}
class YSaveImage(bpy.types.Operator):
"""Save Image"""
bl_idname = "node.y_save_image"
bl_label = "Save Image"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return hasattr(context, 'image') and context.image and context.image.filepath != '' and not context.image.packed_file
def execute(self, context):
ori_colorspace = context.image.colorspace_settings.name
if context.image.is_float:
save_float_image(context.image)
else:
context.image.save()
context.image.colorspace_settings.name = ori_colorspace
return {'FINISHED'}
format_extensions = {
'BMP' : '.bmp',
'IRIS' : '.rgb',
'PNG' : '.png',
'JPEG' : '.jpg',
'JPEG2000' : '.jp2',
'TARGA' : '.tga',
'TARGA_RAW' : '.tga',
'CINEON' : '.cin',
'DPX' : '.dpx',
'OPEN_EXR_MULTILAYER' : '.exr',
'OPEN_EXR' : '.exr',
'HDR' : '.hdr',
'TIFF' : '.tif',
'WEBP' : '.webp',
}
def color_mode_items(self, context):
items = []
if self.file_format in {'BMP', 'IRIS', 'PNG', 'JPEG', 'TARGA', 'TARGA_RAW', 'TIFF'}:
items.append(('BW', 'BW', ''))
items.append(('RGB', 'RGB', ''))
if self.file_format not in {'BMP', 'JPEG', 'CINEON', 'HDR'}:
items.append(('RGBA', 'RGBA', ''))
return items
def color_depth_items(self, context):
if self.file_format in {'PNG', 'TIFF'}:
items = (
('8', '8', ''),
('16', '16', '')
)
elif self.file_format in {'JPEG2000'}:
items = (
('8', '8', ''),
('12', '12', ''),
('16', '16', '')
)
elif self.file_format in {'DPX'}:
items = (
('8', '8', ''),
('10', '10', ''),
('12', '12', ''),
('16', '16', '')
)
elif self.file_format in {'OPEN_EXR_MULTILAYER', 'OPEN_EXR'}:
items = (
('16', 'Float (Half)', ''),
('32', 'Float (Full)', '')
)
else:
items = (
('8', '8', ''),
('10', '10', ''),
('12', '12', ''),
('16', '16', ''),
('32', '32', '')
)
return items
def update_save_as_file_format(self, context):
if self.file_format in {'BMP', 'JPEG', 'CINEON', 'HDR'}:
self.color_mode = 'RGB'
else: self.color_mode = 'RGBA'
if self.file_format in {'BMP', 'IRIS', 'PNG', 'JPEG', 'JPEG2000', 'TARGA', 'TARGA_RAW', 'WEBP'}:
self.color_depth = '8'
elif self.file_format in {'CINEON', 'DPX'}:
self.color_depth = '10'
elif self.file_format in {'TIFF'}:
self.color_depth = '16'
elif self.file_format in {'HDR', 'OPEN_EXR_MULTILAYER', 'OPEN_EXR'}:
self.color_depth = '32'
if self.is_float and self.file_format in {'PNG', 'JPEG2000'}:
self.color_depth = '16'
def unpack_image(image, filepath):
# Get blender default unpack directory
default_dir = os.path.join(os.path.abspath(bpy.path.abspath('//')), 'textures')
# Check if default directory is available or not, delete later if not found now
default_dir_found = os.path.isdir(default_dir)
# Blender always unpack at \\textures\file.ext
if image.filepath == '':
default_filepath = os.path.join(default_dir, image.name)
else: default_filepath = os.path.join(default_dir, bpy.path.basename(image.filepath))
# Check if file with default path is already available
temp_path = ''
if os.path.isfile(default_filepath) and default_filepath != filepath:
temp_path = os.path.join(default_dir, '__TEMP__')
os.rename(default_filepath, temp_path)
# Unpack the file
image.unpack()
unpacked_path = bpy.path.abspath(image.filepath)
# HACK: Unpacked path sometimes has inconsistent backslash
folder, file = os.path.split(unpacked_path)
unpacked_path = os.path.join(folder, file)
return default_dir, default_dir_found, default_filepath, temp_path, unpacked_path
def remove_unpacked_image_path(image, filepath, default_dir, default_dir_found, default_filepath, temp_path, unpacked_path):
# Remove unpacked file
if filepath != unpacked_path:
if image.source == 'TILED':
for tile in image.tiles:
upath = unpacked_path.replace('<UDIM>', str(tile.number))
try: os.remove(upath)
except Exception as e: print(e)
else:
os.remove(unpacked_path)
# Rename back temporary file
if temp_path != '':
if temp_path != filepath:
os.rename(temp_path, default_filepath)
else: os.remove(temp_path)
# Delete default directory if not found before
if not default_dir_found:
os.rmdir(default_dir)
class YSaveAllBakedImages(bpy.types.Operator):
"""Save All Baked Images to directory"""
bl_idname = "node.y_save_all_baked_images"
bl_label = "Save All Baked Images"
bl_options = {'REGISTER', 'UNDO'}
# Define this to tell 'fileselect_add' that we want a directoy
directory : bpy.props.StringProperty(
name = 'Outdir Path',
description = 'Where I will save my stuff'
# subtype='DIR_PATH' is not needed to specify the selection mode.
# But this will be anyway a directory path.
)
remove_whitespaces : bpy.props.BoolProperty(
name = 'Remove Whitespaces',
description = 'Remove whitespaces from baked image names',
default = False
)
file_format : EnumProperty(
name = 'File Format',
items = (
('PNG', 'PNG', '', 'IMAGE_DATA', 0),
('TIFF', 'TIFF', '', 'IMAGE_DATA', 1),
('OPEN_EXR', 'OpenEXR', '', 'IMAGE_DATA', 2)
),
default = 'PNG'
)
copy : BoolProperty(
name = 'Copy',
description = 'Create a new image file without modifying the current image in Blender',
default = False
)
def invoke(self, context, event):
# Open browser, take reference to 'self' read the path to selected
# file, put path in predetermined self fields.
# See: https://docs.blender.org/api/current/bpy.types.WindowManager.html#bpy.types.WindowManager.fileselect_add
context.window_manager.fileselect_add(self)
# Tells Blender to hang on for the slow user input
return {'RUNNING_MODAL'}
def draw(self, context):
split = split_layout(self.layout, 0.4)
col = split.column()
col.label(text='Image Format:')
col = split.column()
col.prop(self, 'file_format', text='')
self.layout.prop(self, 'copy')
def execute(self, context):
node = get_active_ypaint_node()
tree = node.node_tree
yp = tree.yp
tmpscene = bpy.data.scenes.new('Temp Save As Scene')
settings = tmpscene.render.image_settings
# Blender 2.80 has filmic as default color settings, change it to standard
if is_bl_newer_than(2, 80):
tmpscene.view_settings.view_transform = 'Standard'
images = []
height_root_ch = get_root_height_channel(yp)
# Baked images
for ch in yp.channels:
if ch.no_layer_using: continue
baked = tree.nodes.get(ch.baked)
if baked and baked.image:
images.append(baked.image)
if ch == height_root_ch:
baked_disp = tree.nodes.get(ch.baked_disp)
if baked_disp and baked_disp.image:
images.append(baked_disp.image)
baked_vdisp = tree.nodes.get(ch.baked_vdisp)
if baked_vdisp and baked_vdisp.image:
images.append(baked_vdisp.image)
if not is_overlay_normal_empty(yp):
baked_normal_overlay = tree.nodes.get(ch.baked_normal_overlay)
if baked_normal_overlay and baked_normal_overlay.image:
images.append(baked_normal_overlay.image)
# Custom bake target images
for bt in yp.bake_targets:
image_node = tree.nodes.get(bt.image_node)
if image_node and image_node.image not in images:
images.append(image_node.image)
original_image_names = []
original_names = []
if self.copy:
copied_images = []
for image in images:
ori_name = image.name
image_copy = duplicate_image(image, ondisk_duplicate=False)
image.name += '____'
image_copy.name = ori_name
original_image_names.append(image.name)
original_names.append(ori_name)
copied_images.append(image_copy)
images = copied_images
for image in images:
settings.file_format = self.file_format
settings.color_depth = '8' if settings.file_format != 'OPEN_EXR' else '16'
if image.is_float:
settings.color_depth = '16' if settings.file_format != 'OPEN_EXR' else '32'
if settings.file_format == 'OPEN_EXR':
settings.exr_codec = 'ZIP'
if image.filepath == '' or '.<UDIM>.' in image.filepath:
image_name = image.name
# Remove addon title from the file names
if image_name.startswith(get_addon_title() + ' '):
image_name = image_name.replace(get_addon_title() + ' ', '')
filename = image_name
filename += '.<UDIM>' if '.<UDIM>.' in image.filepath else ''
filename += format_extensions[settings.file_format]
else:
filename = bpy.path.basename(image.filepath)
ext = os.path.splitext(filename)[1]
if ext != format_extensions[settings.file_format]:
filename = filename.replace(ext, format_extensions[settings.file_format])
if self.remove_whitespaces:
filename = filename.replace(' ', '')
path = os.path.join(self.directory, filename)
# Need to pack first to save the image
if image.is_dirty:
if is_bl_newer_than(2, 80):
image.pack()
else:
if image.is_float:
pack_float_image(image)
else: image.pack(as_png=True)
# Some images need to set to srgb when saving
ori_colorspace = image.colorspace_settings.name
if not image.is_float and image.colorspace_settings.name != get_srgb_name():
image.colorspace_settings.name = get_srgb_name()
# Unpack image if image is packed (Only necessary for Blender 2.80 and lower)
unpacked_to_disk = False
if not is_bl_newer_than(2, 81) and bpy.data.filepath != '' and image.packed_file:
unpacked_to_disk = True
default_dir, default_dir_found, default_filepath, temp_path, unpacked_path = unpack_image(image, path)
# Save image
image.save_render(path, scene=tmpscene)
# Set the filepath to the image
image.filepath = path
if bpy.data.filepath != '':
try: image.filepath = bpy.path.relpath(path)
except: pass
# Set back colorspace settings
if image.colorspace_settings.name != ori_colorspace:
image.colorspace_settings.name = ori_colorspace
# Remove temporarily unpacked image
if unpacked_to_disk:
remove_unpacked_image_path(image, path, default_dir, default_dir_found, default_filepath, temp_path, unpacked_path)
# Remove packed flag
if is_bl_newer_than(2, 81) and image.packed_file:
image.unpack(method='REMOVE')
# Remove copied images
if self.copy:
for image in reversed(images):
remove_datablock(bpy.data.images, image)
# Recover image names
for i, ori_image_name in enumerate(original_image_names):
ori_image = bpy.data.images.get(ori_image_name)
ori_image.name = original_names[i]
# Delete temporary scene
remove_datablock(bpy.data.scenes, tmpscene)
#print("Selected dir: '" + self.directory + "'")
return {'FINISHED'}
def get_file_format_items():
items = [
('BMP', 'BMP', '', 'IMAGE_DATA', 0),
('IRIS', 'Iris', '', 'IMAGE_DATA', 1),
('PNG', 'PNG', '', 'IMAGE_DATA', 2),
('JPEG', 'JPEG', '', 'IMAGE_DATA', 3),
('JPEG2000', 'JPEG 2000', '', 'IMAGE_DATA', 4),
('TARGA', 'Targa', '', 'IMAGE_DATA', 5),
('TARGA_RAW', 'Targa Raw', '', 'IMAGE_DATA', 6),
('CINEON', 'Cineon', '', 'IMAGE_DATA', 7),
('DPX', 'DPX', '', 'IMAGE_DATA', 8),
('OPEN_EXR_MULTILAYER', 'OpenEXR Multilayer', '', 'IMAGE_DATA', 9),
('OPEN_EXR', 'OpenEXR', '', 'IMAGE_DATA', 10),
('HDR', 'Radiance HDR', '', 'IMAGE_DATA', 11),
('TIFF', 'TIFF', '', 'IMAGE_DATA', 12)
]
if is_bl_newer_than(3, 2):
items.append(('WEBP', 'WebP', '', 'IMAGE_DATA', 13))
return items
class YSaveAsImage(bpy.types.Operator, ExportHelper):
"""Save As Image"""
bl_idname = "node.y_save_as_image"
bl_label = "Save As Image"
bl_options = {'REGISTER', 'UNDO'}
file_format : EnumProperty(
name = 'File Format',
items = get_file_format_items(),
default = 'PNG',
update = update_save_as_file_format
)
# File browser filter
filter_folder : BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
filter_image : BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
display_type : EnumProperty(
items = (
('FILE_DEFAULTDISPLAY', 'Default', ''),
('FILE_SHORTDISLPAY', 'Short List', ''),
('FILE_LONGDISPLAY', 'Long List', ''),
('FILE_IMGDISPLAY', 'Thumbnails', '')
),
default = 'FILE_IMGDISPLAY',
options = {'HIDDEN', 'SKIP_SAVE'}
)
copy : BoolProperty(
name = 'Copy',
description = 'Create a new image file without modifying the current image in Blender',
default = False
)
relative : BoolProperty(
name = 'Relative Path',
description = 'Select the file relative to the blend file',
default = True
)
color_mode : EnumProperty(
name = 'Color Mode',
items = color_mode_items
)
color_depth : EnumProperty(
name = 'Color Depth',
items = color_depth_items
)
tiff_codec : EnumProperty(
name = 'Compression',
items = (
('NONE', 'None', ''),
('DEFLATE', 'Deflate', ''),
('LZW', 'LZW', ''),
('PACKBITS', 'Pack Bits', ''),
),
default = 'DEFLATE'
)
exr_codec : EnumProperty(
name = 'Codec',
items = (
('NONE', 'None', ''),
('PXR24', 'Pxr24 (lossy)', ''),
('ZIP', 'ZIP (lossless)', ''),
('PIZ', 'PIZ (lossless)', ''),
('RLE', 'RLE (lossless)', ''),
('ZIPS', 'ZIPS (lossless)', ''),
('DWAA', 'DWAA (lossy)', ''),
),
default = 'ZIP'
)
jpeg2k_codec : EnumProperty(
name = 'Codec',
items = (
('JP2', 'JP2', ''),
('J2K', 'J2K', ''),
),
default = 'JP2'
)
compression : IntProperty(name='Compression', default=15, min=0, max=100, subtype='PERCENTAGE')
quality : IntProperty(name='Quality', default=90, min=0, max=100, subtype='PERCENTAGE')
use_jpeg2k_cinema_48 : BoolProperty(name='Cinema 48', default=False)
use_jpeg2k_cinema_preset : BoolProperty(name='Cinema', default=False)
use_jpeg2k_ycc : BoolProperty(name='YCC', default=False)
use_cineon_log : BoolProperty(name='Log', default=False)
use_zbuffer : BoolProperty(name='Log', default=False)
# Flag for float image
is_float : BoolProperty(default=False)
@classmethod
def poll(cls, context):
return hasattr(context, 'image') and context.image and get_active_ypaint_node()
def draw(self, context):
split = split_layout(self.layout, 0.5)
split.prop(self, 'file_format', text='')
row = split.row(align=True)
row.prop(self, 'color_mode', expand=True)
if self.file_format in {'PNG', 'JPEG2000', 'DPX', 'OPEN_EXR_MULTILAYER', 'OPEN_EXR', 'TIFF'}:
row = self.layout.row()
row.label(text='Color Depth:')
row.prop(self, 'color_depth', expand=True)
if self.file_format == 'PNG':
self.layout.prop(self, 'compression')
if self.file_format in {'JPEG', 'JPEG2000', 'WEBP'}:
self.layout.prop(self, 'quality')
if self.file_format == 'TIFF':
self.layout.prop(self, 'tiff_codec')
if self.file_format in {'OPEN_EXR', 'OPEN_EXR_MULTILAYER'}:
self.layout.prop(self, 'exr_codec')
if self.file_format == 'OPEN_EXR':
self.layout.prop(self, 'use_zbuffer')
if self.file_format == 'CINEON':
self.layout.label('Hard coded Non-Linear, Gamma:1.7')
if self.file_format == 'JPEG2000':
self.layout.prop(self, 'jpeg2k_codec')
row = self.layout.row()
row.prop(self, 'use_jpeg2k_cinema_48')
row.prop(self, 'use_jpeg2k_cinema_preset')
self.layout.prop(self, 'use_jpeg2k_ycc')
if self.file_format == 'DPX':
self.layout.prop(self, 'use_cineon_log')
self.layout.prop(self, 'copy')
if not self.copy:
self.layout.prop(self, 'relative')
def invoke(self, context, event):
self.use_filter_image = True
file_ext = format_extensions[self.file_format]
filename = bpy.path.basename(context.image.filepath)
# Set filepath
if context.image.filepath == '' or filename == '' or '.<UDIM>.' in filename:
yp = get_active_ypaint_node().node_tree.yp
name = context.image.name
name += '.<UDIM>' if '.<UDIM>.' in filename else ''
# Remove addon title from the file names
if yp.use_baked and name.startswith(get_addon_title() + ' '):
name = name.replace(get_addon_title() + ' ', '')
if not name.endswith(file_ext): name += file_ext
self.filepath = name
else:
self.filepath = context.image.filepath
# Pass context.image to self
self.image = context.image
if self.image.yia.is_image_atlas:
return self.execute(context)
# Set default color mode
if self.file_format in {'BMP', 'JPEG', 'CINEON', 'HDR'}:
self.color_mode = 'RGB'
else: self.color_mode = 'RGBA'
if self.image.is_float:
self.is_float = True
#self.file_format = 'OPEN_EXR'
#if self.file_format in {'PNG', 'JPEG2000'}:
if self.color_depth == '8':
self.color_depth = '16'
else:
self.is_float = False
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def check(self, context):
change_ext = False
filepath = self.filepath
file_ext = format_extensions[self.file_format]
if bpy.path.basename(filepath):
# Check current extensions
for form, ext in format_extensions.items():
if filepath.endswith(ext):
filepath = filepath.replace(ext, '')
break
filepath = bpy.path.ensure_ext(filepath, file_ext)
if filepath != self.filepath:
self.filepath = filepath
change_ext = True
return change_ext
#return True
def unpack_image(self, context):
image = self.image
# Get blender default unpack directory
self.default_dir = os.path.join(os.path.abspath(bpy.path.abspath('//')), 'textures')
# Check if default directory is available or not, delete later if not found now
self.default_dir_found = os.path.isdir(self.default_dir)
# Blender always unpack at \\textures\file.ext
if image.filepath == '':
self.default_filepath = os.path.join(self.default_dir, image.name)
else: self.default_filepath = os.path.join(self.default_dir, bpy.path.basename(image.filepath))