0
$\begingroup$

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...

$\endgroup$
13
  • $\begingroup$ I get line 6, in <module> obj.select = False AttributeError: 'Object' object has no attribute 'select' $\endgroup$ Dec 15, 2020 at 10:43
  • $\begingroup$ Okay, my bad it did have some outdated lines. Please check the answer you provided, I updated the code for 2.8+. basically obj.select became a method obj.select_set(state) and now with view layers you have to set the active object per view layer. scene.objects.active = obj becomes bpy.context.view_layer.objects.active = obj $\endgroup$
    – Gorgious
    Dec 15, 2020 at 10:44
  • $\begingroup$ Does the update solve your issue? @sephiroth85 $\endgroup$
    – brockmann
    Dec 15, 2020 at 11:36
  • $\begingroup$ no, i updated it but still getting this $\endgroup$ Dec 15, 2020 at 11:52
  • $\begingroup$ line 15, in <module> bpy.ops.object.bake(type='COMBINED') $\endgroup$ Dec 15, 2020 at 11:54

0

You must log in to answer this question.

Browse other questions tagged .