Can't bake to external textures (Python)
Open, Needs TriagePublic


Hello, when trying to bake an object to an external texture, Blender seems to ignore the "save_mode" and bakes the texture to an internal buffer. I've tried two ways:

import bpy

# Bake selected objects
for obj in filter(lambda x: x.type == 'MESH',
	print('Selected: ' +
	# Select current object
	bpy.ops.object.select_all(action='DESELECT') = True = obj
	# Bake
	bpy.ops.object.bake(type='COMBINED', save_mode='EXTERNAL', filepath='E:\\baked.png', use_automatic_name=True, width=512, height=512)
	# Other version, doesn't work either
	# bpy.ops.object.bake(filepath="//baked.png", save_mode='EXTERNAL')

And also setting the parameters in a different way:

bpy.context.scene.render.bake.filepath = 'E:\\baked.png'
bpy.context.scene.render.bake.save_mode = 'EXTERNAL'
bpy.context.scene.render.bake.use_split_materials = False

Always, the execution of the previous code returns the following and does not create an image file:

Selected: Cube
Info: Baking map saved to internal image, save it externally or pack it
Selected: Floor
Info: Baking map saved to internal image, save it externally or pack it

Tried with different paths and parameters for the bake funcion, also creating image nodes and selecting / deselecting them in the node editor for the material.

Also, I found someone who had the same problem as me, and found no solution:

Exact steps for others to reproduce the error
Find attached a blend file:

It's a simple cycles scene with a cube and a floor, Cycles renderer. It bakes correctly to textures (internal buffer).

  • Open the blend file or create your own scene
  • Run the code written above
  • Blender won't create a bake file, in the console you'll see "Baking map saved to internal image, save it externally or pack it"

System Information
Windows 10, nVidia Quadro K5100M

Blender Version
Broken: 2.79b f4dc9f9d68b, 2.77a abf6f08

Thank you



Found this from 2014 in blender/source/blender/editors/object/object_bake_api.c at the end of:
static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr)

97641a0ec946 (Dalai Felinto 2014-01-02 19:05:07 -0200 1166) /* XXX hack to force saving to always be internal. Whether (and how) to support
97641a0ec946 (Dalai Felinto 2014-01-02 19:05:07 -0200 1167) * external saving will be addressed later */
97641a0ec946 (Dalai Felinto 2014-01-02 19:05:07 -0200 1168) bkr->save_mode = R_BAKE_SAVE_INTERNAL;

@Dalai Felinto (dfelinto) Is this something that would be hard to implement, or has it just been forgotten in the annals of time?

The hard bit about implementing this is in regard to the usability side of things. Right now each object can have its own materials, that in turn use their own textures.
To bake only to external textures means you are restrict to a single texture. Not the end of the world (and probably not hard to implement). But this is the historical reason for this missing bit.

Hi Karl, Dalai, thank you for taking a look at this issue.

I'll tell you my objective for baking external textures: I work for a company that makes software for broadcast environments; in particular, virtual sets. I want to promote Blender between our customers, and they need baking to create realistic virtual sets.

In 3D Max Cinema4D it's trivially easy to bake textures for whole sets with lots of objects; in fact, they bake a file for each object (well, they can create multiple files like for separate diffuse / AO). I tried to replicate that behavior creating a script that bakes each object individually and then relinks its material to each baked texture.

This behavior (preventing external baking, even with Python) makes it difficult to integrate Blender with our workflow, and making a workaround for this might be too expensive for me to implement.

Thank you, and greetings