Baking textures from python?

Is it possible to bake textures to active UV layer from Python? I’m working on a script that will bake the textures of each object in a group called “Scene” to a UV layer called “Bake”. Here is what I have so far:


import bpy
import uvcalc_lightmap as uvpack
	
def export():
	# Get the scene group (contains all objects to be exported)
	sceneGroup = bpy.data.groups["Scene"]
	
	# Loop through each object in the scene group
	for obj in sceneGroup.objects:
		# Get the mesh data for the current object
		mesh = obj.getData(mesh=1)
		# Get a list of the uv layers for the current mesh
		uvLayers = mesh.getUVLayerNames()
		# if the mesh doesn't have a uvlayer for baking textures
		if "Bake" not in uvLayers:
			# Add new uv layer for baking
			mesh.addUVLayer("Bake")
		# Set the new uv layer as active layer
		mesh.activeUVLayer = "Bake"			
		# unwrap the mesh
		uvpack.lightmap_uvpack([mesh])
		# Now i need to bake the textures here
		# ???

export()

At the moment, I loop through each object in the “Scene” group, get the mesh data for the object, check if the mesh has a UV layer named “Bake”, if it doesn’t, I add the “Bake” UV layer and set it as the active UV layer. I then unwrap the UV’s to the active layer, now I need to actually bake the textures. What modules/functions do I need to be looking at in order to do this?

Thank You.

EDIT: Typical, after posting this I found what object I was looking for, http://www.zoo-logique.org/3D.Blender/scripts_python/API/

See that: http://blenderartists.org/forum/showthread.php?t=196005
Bye xD

Thanks vitorbalbio, your project looks interesting, unfortunately baking the textures is only a small part of my overall goal. Along with baking textures for each object, I’m also exporting object data, saving the baked textures and creating a scene file (to be interpreted within my app). Thats so I can create my scene in blender, run one script and have everything written to the filesystem ready to be loaded in my app. Everythings working well now, but thank you anyway vitorbalbio.

I hope my code can help you as a Code snippet or exemple :wink:
Good Luck in your projects and Keep Blending!!! :smiley: