0
$\begingroup$

Need to render animated cubemaps for use in another program. UI allows to save the cubemap of current frame.

Trying to do it in Python. Getting a context error:

import bpy

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

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()

    # need to render for some reason to save the cubemap below
    bpy.ops.render.render()

    # save cubemap image
    # needs proper context
    area = bpy.context.area
    old_type = area.type
    area.type = 'PROPERTIES'
    bpy.ops.texture.envmap_save(filepath=str(i)+'.png')
    area.type = old_type

    print ("Frame "+str(i)+" complete")

# switch back to the frame you were on before running this code
bpy.context.scene.frame_current = current_frame
$\endgroup$
4
  • 1
    $\begingroup$ Use scene.frame_set(i) to change the frame, and as well as being a properties area it needs to be in texture context area.spaces.active.context = 'TEXTURE' which I think will fix your context woes. $\endgroup$
    – batFINGER
    Commented Sep 3, 2015 at 20:32
  • $\begingroup$ hm, TypeError: bpy_struct: item.attr = val: enum "TEXTURE" not found in () $\endgroup$
    – Leo Ervin
    Commented Sep 3, 2015 at 20:53
  • $\begingroup$ bpy.context.space_data.context = 'TEXTURE' $\endgroup$
    – batFINGER
    Commented Sep 3, 2015 at 21:00
  • $\begingroup$ still. RuntimeError: Operator bpy.ops.texture.envmap_save.poll() failed, context is inc orrect $\endgroup$
    – Leo Ervin
    Commented Sep 3, 2015 at 21:09

1 Answer 1

1
$\begingroup$

That did the trick for me once the env map image source was set to 'GENERATED'

import bpy

# remember current frame to switch back after the script is finished

context = bpy.context
scene = context.scene
current_frame = scene.frame_current
area = context.area
old_type = area.type
area.type = 'PROPERTIES'
context.space_data.context = 'TEXTURE'

for i in range(scene.frame_start, scene.frame_end+1):
    # set current frame
    scene.frame_set(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()

    # need to render for some reason to save the cubemap below
    bpy.ops.render.render()

    # save cubemap image
    # needs proper context


    bpy.ops.texture.envmap_save(filepath=str(i)+'.png')


    print ("Frame "+str(i)+" complete")

area.type = old_type
# switch back to the frame you were on before running this code
scene.frame_set(current_frame)
$\endgroup$
4
  • $\begingroup$ "env map image source was set to 'GENERATED'". What do you mean? Still says context incorrect for me. $\endgroup$
    – Leo Ervin
    Commented Sep 3, 2015 at 21:22
  • $\begingroup$ # Save the current generated Environment map to an image file bpy.ops.texture.envmap_save(layout=(0, 0, 1, 0, 2, 0, 0, 1, 1, 1, 2, 1), filepath="",) ... I was under the impression that operator only polled if the env map image source was set to 'GENERATED' otherwise it is greyed out (doesn't poll) in the UI. Properties > texture > Environment Map $\endgroup$
    – batFINGER
    Commented Sep 3, 2015 at 21:51
  • $\begingroup$ So is it working for you? $\endgroup$
    – Leo Ervin
    Commented Sep 4, 2015 at 4:24
  • $\begingroup$ I still get a context error, do you? $\endgroup$
    – Leo Ervin
    Commented Sep 4, 2015 at 21:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .