Baking with Python impossible?

Trying to bake in Blender Internal to achieve equirectangular rendering with Blender Internal.

this is the code:


import bpy

# remember current frame to switch back after the script is finished
current_frame = bpy.context.scene.frame_current

mesh = bpy.context.scene.objects.active.data

for i in range(bpy.context.scene.frame_start, bpy.context.scene.frame_end+1):
    # set current frame
    bpy.context.scene.frame_current = i
    
    # create an image which will store the baked data
    image = bpy.data.images.new(str(i)+'.png', bpy.context.scene.render.resolution_x, bpy.context.scene.render.resolution_y, alpha = 0)
    image.file_format = 'PNG'
    image.filepath = "//"+str(i)+".png"
    #image.save()
    
    #set the active image for each uv:
    for uvface in mesh.uv_textures.active.data:
        uvface.image = image
        
    # set texture as active for the material
    # add texture (slot) if missing
    if mesh.materials[0].texture_slots[0] == None:
        mesh.materials[0].texture_slots.add()
        mesh.materials[0].texture_slots[0].texture = bpy.data.textures.new(str(i), type = 'IMAGE')
    mesh.materials[0].texture_slots[0].texture.image = image
    mesh.materials[0].active_texture = mesh.materials[0].texture_slots[0].texture
    
    # bake
    bpy.ops.object.bake(type='COMBINED')
    
    # resave image with baked data
    image.save()
    
    print ("Frame "+str(i)+" complete")
    
# switch back to the frame you were on before running this code
bpy.context.scene.frame_current = current_frame


This is the blend file: http://www.pasteall.org/blend/37804

THis is the error:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\internal.blend\Text", line 30, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops
.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: No active image found in material "Material.002" (0) for ob
ject "Sphere"

Is there an image in Material.002? How is the “active” state of an image determined in GUI? (baking is alien to me)

Yeah, you can check the blend file I attached. The script is also in there.

I have no idea how “active” is determined, nor what “active image” means. If the log said “active texture” that would make sense.

Anyway, if you open the blend file and do

you will see the image baked nicely.

For future reference, Active Image for Blender Internal is the image that appear in the image editor when the target object (the one of which texture will be changed) is in edit mode. It can be set by using the following code:-


for area in bpy.context.screen.areas:
         if area.type == 'IMAGE_EDITOR':
                 area.spaces.active.image = some_image
bpy.ops.object.bake_image()