Baking Texture-Only

I’m working on a script that, when all is said and done, will bake several textures and save them out as .tgas.

I’ve figured pretty much everything out except one thing – I cannot bake the material texture without having shadows. The ctrl+alt+b > Textures function does exactly what I want, but I would like to automate this particular step.

My script will bake a rendered version of the texture including shadows using the .bake() function. How do you get a non-shadowed, full-intensity texture bake?

What I like to do is go to the bake panel and hit the ‘texture’ button :smiley:
Is there a way to see the internal python commands when you use the regular ‘texture only’ bake button?
I’m thinking in the command prompt, but better…There’s got to be a way to see that eh?

:stuck_out_tongue:

Is there a way to see the internal python commands when you use the regular ‘texture only’ bake button?
I’m thinking in the command prompt, but better…There’s got to be a way to see that eh?
I would rather know that, than simply the answer to my original question :wink:

I do a lot of VBA at work, and the record function helps tremendously. If only Blender had such a thing, eh?

There is already this :
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_mesh3d2uv2d_en.htm

and, of course, as shadow can be disabled in each material or as you can use the shadeless option, you can bake with blender itself.

Thanks, jms, but that’s not at all what I’m trying to do.

All I need is to bake the existing materials onto a new, blank image, exactly like “Render > Bake Render Meshes > Texture Only”.

Surely there is a quick and easy way to do it in python…

I think you would use the attribute Render.RenderData.bakeMode for that. Here’s a list of available modes.

I haven’t used it myself though and can’t tell you if it works. :slight_smile:

That’s what I’ve been looking at, Sanne. Unfortunately, the API docs didn’t mention that the bake modes were integers. (bakeMode = 1) I also discovered that half of the items in the API were actually links (don’t know how I missed that, either), so I was able to drill down and find everything I needed.

Of course, I don’t have the script in front of me, but I’ll paste the snippet that answered my question here when I get back to my Blender computer.

Such constants usually are integers mapped to a descriptive expression. I would guess one can use them like this to set the bake mode:

bakeMode = Blender.Scene.Render.BakeModes.SHADOW

which is really an integer, namely 6, so you could also say

bakeMode = 6

and get the same result, but less descriptive.

Ah. That makes sense. I was just doing bakeMode = ‘TEXTURE’, not the full Blender.Scene… part.

Though, now I’m stuck with one issue – how do you create a new, blank (all black) UV image and assign it to all selected objects?

Image.New('imagename', 2048, 2048, 24)

makes my image for me, and then

image = Image.Get('imagename')
image.useCurrent()

sets the UV layer, but I don’t see the actual blank image there.

When I then run my bake, I get the “no images” error. That implies that the image isn’t actually active on the various objects. How can I set a particular image as the active UV layer for all selected objects?

EDIT:
I think this may solve my problem, with some simple modification:
http://blenderartists.org/forum/showthread.php?t=115850

Image.New() already returns a handle to the new image object, so you could save a line if you say:

image = Image.New('imagename', 2048, 2048, 24)
image.useCurrent()

For assigning this image as texture to all faces of the object, I believe (I hope it’s the correct way) you need to go via the Mesh module. Get the mesh data from your object, then get the faces for this mesh, loop through them and set the image as texture to each face. Look here for the image attribute of Blender.Mesh.MFace.

That just adds a new Texture to the mesh’s material.

here’s my script, thus far. It works for any number of selected objects, as long as they all have an assigned UV Layer. http://www.coffeebot.net/files/trio_bake.txt

How can I get this to cycle through each object, assign a UV layer, and then perform the bake?

Here’s an example that loops through all selected mesh objects in a scene, adds a new uv layer to those objects and sets the last added uv layer to the active layer for each mesh. After looping, the button windows are redrawn to update the uv layer list (in case it is open).

I hope this helps you with your script.

import Blender

scn = Blender.Scene.GetCurrent()
selobs = [ob for ob in scn.objects.selected if ob.type == "Mesh"]

for ob in selobs:
	me = ob.getData(mesh=True)
	me.addUVLayer("myuv")
	me.activeUVLayer = me.getUVLayerNames()[-1]
	me.update()

Blender.Window.Redraw(Blender.Window.Types.BUTS)