I created a patch that enables external baking, in a proper way:
- Allows to bake to external textures through the python API (as described in the docs)
- Creates its own image buffer (and frees it) so the designer doesn't need to create an image node for each material. yay! :)
- Fixes a bug: if we set the path to manual instead of automatic, the path parameter was discarded
This patch is meaningful for the following workflow:
- We want to bake a scene with lots of objects
- These objects share materials between themselves
- We want to export the baked result to an OBJ, with a single texture for the whole object.
- The designer is lazy and wants something like 3D Max or Cinema4D: just click and bake. No creating nodes, additional buffers, etc. by hand
- This patch is the first step to automating the above steps through a simple Python script.
I've developed it on Blender 2.79, but rebased it to the latest 2.8 git.
- Go to Cycles mode
- Select the default cube, use nodes, create a material for each face with a different color
- UV Unwrap the cube
- No need to create image buffers o image nodes pointing to them.
- With the cube selected in object mode, execute:
bpy.ops.object.bake(type='COMBINED', save_mode='EXTERNAL', filepath=r'/tmp/cube_baked.png', use_automatic_name=False, width=512, height=512)
As you can see, it doesn't require a dummy node, making it transparent for the graphic designer. We could script a batch export for the scene with minimal effort.
About the implementation: The bake code just requires an image buffer, but doesn't seem to do anything useful with it when baking to external textures. Since I don't know the code, I applied the least intrusive method instead of branching the render code (tried that and didn't go well).
This is my first contribution to an open source project, please go easy on me :)
There may be faults, please look at the malloc() I used and more other things, I don't know if you use some wrappers above it.