I've created a simple script that bakes lightmaps on a lot of selected objects in blender 2.93.4.
The last line of the code simply saves all the images, the problem is that I need them to be saved as render , using the filmic transform applied to the final images (like when you save every image manually and check "save as render" in the file browser save window). How can I do that? Right now it simply saves them in sRGB color transform. Of course filmic is active and selected as view transform in color management options, and I know I can set the display image option changing this:
bpy.types.Image.view_as_render
but it only affect view, and not saving.
I'm also aware of
bpy.ops.image.save_as()
Which has a save_as_render parameter, but It's an operator, and it needs context, while in my case I just need to save all my images, even if the Image editor is not even open.
Thanks in advance, here is the code:
import bpy
bpy.ops.outliner.orphans_purge(do_recursive = True)
objects = bpy.context.selected_objects
for obj in objects:
if obj.type == 'MESH':
mat = obj.material_slots[0].material
obj_name = obj.name
obj_name_len = len(obj_name)
mat.use_nodes = True
node_tree = mat.node_tree
nodes = node_tree.nodes
tex_node_exists = False
for node in nodes:
node_name_prefix = node.name[:obj_name_len]
node_name = node.name
if node_name_prefix == obj_name:
tex_node_exists = True
if tex_node_exists == False:
tex_image = bpy.data.images.new(obj_name + "_LM", bake_resolution, bake_resolution, alpha=True)
tex_image.filepath = "//" + obj_name + "_LM.png"
tex_image.save()
tex_image.reload()
tex_node = node_tree.nodes.new('ShaderNodeTexImage')
tex_node.image = tex_image
tex_node.select = True
nodes.active = tex_node
tex_node_exists = True
bpy.ops.object.bake(save_mode='EXTERNAL',filepath= "//")
bpy.ops.image.save_all_modified()
Edit:
As @scurest suggested, I tried with save_render() at the end of the script (of course i create the "baked_images" list and populate it before):
path = "//baked_images\\"
print(path)
for img in baked_images:
img.save_render(filepath= path)
This is what i get in the console (line 132 is where the img.save_render()) method is called), Even if I run blender as admin (Windows 10):
Info: Deleted 1 data-block(s) Info: Baking map saved to internal image, save it externally or pack it //baked_images\ Unable to create directory //baked_images\ imb_savepng: Cannot open file for writing: '//baked_images\' //baked_images\: No such file or directory Traceback (most recent call last): File "C:\Users\User\Desktop\Lightmap_Bake\lightmap_bake_v02.blend\bake_lightmap",
line 132, in RuntimeError: Error: Could not write image: No such file or directory, '//baked_images'
Error: Python script failed, check the message in the system console
I don't know if it can be related to this:
[https://developer.blender.org/T63853][1]
Any Ideas?
img.save_render()
, have you tried that? $\endgroup$