2
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ There's img.save_render(), have you tried that? $\endgroup$
    – scurest
    Sep 13 at 18:45
  • $\begingroup$ Thanks, I would like to try it but no matter if I run blender in administrator mode (I'm on windows 10 btw) the method cannot write the file, The consoles "Unable to create directory //baked_images\", see edits in the post for more details. $\endgroup$
    – RBancone
    Sep 14 at 8:07
  • 1
    $\begingroup$ See: stackoverflow.com/questions/273192/… and blender.stackexchange.com/a/6846 $\endgroup$
    – brockmann
    Sep 14 at 8:14
  • $\begingroup$ Thanks! I figured out why It didn't write .. That's because I wrote placeholders files before baking it, and save_render() (which seem to work and save the filmic transform!!) wasn't allowed to overwrite them! D'oh... thanks a lot for the help!! $\endgroup$
    – RBancone
    Sep 14 at 8:43
1
$\begingroup$

SOLVED: img.save_render() did the trick, It wasn't able to save because the images were already written and it couldn't overwrite them. Just not writing the images before bake solved the issue!

(thanks to @scurest and @brockmann)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.