I'm a noob when it comes to scripting so I came across this solution: Baking textures on headless machine (batch baking) But unfortunately, I can't make it work for 2.8+:
bake_all.py
import bpy
import os
scene = bpy.context.scene
for obj in scene.objects:
obj.select = False
for obj in scene.objects:
if obj.type != 'MESH':
continue
scene.objects.active = obj
obj.select = True
bpy.ops.object.bake(type='COMBINED')
obj.select = False
save all baked images
for image in bpy.data.images:
if image.is_dirty:
# foo.png -> foo_bake.png
filepath, filepath_ext = os.path.splitext(image.filepath_raw)
image.filepath_raw = filepath + "_bake" + filepath_ext
image.save()
Q: How to execute the script in latest versions?
Updated the code based on the comments and it still doesn't work:
import bpy
import os
scene = bpy.context.scene
scene.render.engine = 'CYCLES'
for obj in scene.objects:
obj.select_set(False)
for obj in scene.objects:
if obj.type != 'MESH':
continue
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.bake(type='COMBINED')
obj.select_set(False)
save all baked images
for image in bpy.data.images:
if image.is_dirty:
# foo.png -> foo_bake.png
filepath, filepath_ext = os.path.splitext(image.filepath_raw)
image.filepath_raw = filepath + "_bake" + filepath_ext
image.save()
bpy.ops.image.save
This is not working on my remote PC, it works fine on my local PC but not on my remote farm. What am I doing wrong?
this is not doing anything
# save all baked images
for image in bpy.data.images:
if image.is_dirty:
# foo.png -> foo_bake.png
filepath, filepath_ext = os.path.splitext(image.filepath_raw)
image.filepath_raw = filepath + "_bake" + filepath_ext
image.save()
in terminal it says
Info: Baking map saved to internal image, save it externally or pack it
only the bake file is created that is not usable... do I need to add the filepath?
I tried to add save file and download blender file to open it locally but no baked textures within...

line 6, in <module> obj.select = False AttributeError: 'Object' object has no attribute 'select'– sephiroth85 Dec 15 '20 at 10:43obj.selectbecame a methodobj.select_set(state)and now with view layers you have to set the active object per view layer.scene.objects.active = objbecomesbpy.context.view_layer.objects.active = obj– Gorgious Dec 15 '20 at 10:44