Disclosure: this article was created with the help of AI. I'm the author of a Blender add-on in this space, but this post is a standalone tutorial and doesn't link to it.
The problem
bpy.ops.object.bake() blocks Blender's UI thread. A long or stuck bake means a frozen window, and a crash can cost you unsaved work.
The pattern: one background process per map
- Save a temporary copy of the .blend (never bake on the file you have open):
bpy.ops.wm.save_as_mainfile(filepath=tmp_path, copy=True)
- Launch a worker per map:
blender -b --factory-startup tmp.blend --python bake_one.py -- --map normal --out /path
- In bake_one.py: parse your own args after "--" (sys.argv[sys.argv.index("--")+1:]), make the target mesh selected and active, use the Cycles render engine (bake only works in Cycles), add an Image Texture node with a new image to the object's material and make that node the active node, then call bpy.ops.object.bake(type='NORMAL') (or 'ROUGHNESS', 'AO', ...). The mesh needs a UV map. The baked image is only in memory: set image.filepath_raw and call image.save(), or the result is lost when the worker exits.
- In the parent, use subprocess.Popen and poll; enforce a timeout (e.g. 3× an estimate, minimum 60 s). On overrun call terminate(), wait a few seconds, then kill() if it is still alive, and continue with the next map.
- Cancel = terminate the child process, not just set a flag.
Gotchas
- --factory-startup avoids user add-ons interfering, but you must re-enable anything the bake needs.
- Normal map conventions differ (OpenGL vs DirectX); check your target engine.
- Test on the Blender versions you support; behaviour around extensions changed in 4.2. (The steps above were run on Blender 4.2.23 LTS and 5.2.2 LTS in background mode.)
This article was written with AI assistance; the code steps were run and checked in Blender 4.2 and 5.2 before publishing.
Top comments (0)