8

When I run the following script, current_frame is set AFTER the render.opengl():

bpy.context.area.type = 'VIEW_3D'
bpy.context.scene.frame_current = 234
bpy.data.scenes['Scene'].frame_current = 234
bpy.ops.render.opengl(view_context=False)

If I run the script again, the correct frame is rendered. How can I force the frame_current to be applied before the render?

(A solution without using an operator is also welcome, as I understand that ops are more for user interaction that for scripts and are therefor very dependent on the correct context.)

Thanks!

p2or
  • 15,860
  • 10
  • 83
  • 143
Erik Bongers
  • 321
  • 2
  • 8
  • 3
    You can use bpy.context.scene.frame_set(234) to change the frame before rendering. See this answer: http://blender.stackexchange.com/questions/8387/how-to-get-keyframe-data-from-python – p2or Mar 24 '15 at 13:05

1 Answers1

11

You have to call Scene.frame_set() instead of setting the property Scene.frame_current:

bpy.context.scene.frame_set(234)

Only frame_set() updates animation data correctly. frame_current is mostly intended for reading access to determine the current frame.


Following example renders frame 0, 230, 99 and writes them to the disk as test_000.png, test_099.png and test_230.png.

import bpy
import os

# format integer with leading zeros
def formatNumbers(number, length):
    return '_%0*d' % (length, number)

# get the scene
scn = bpy.context.scene

# get the output path
output_path = scn.render.filepath

# set filename
filename = "test"

# set render frames
render_frames = [0, 230, 99]

# iterate through render frames
for f in render_frames:
    # set the frame
    scn.frame_set(f)
    # set filepath
    scn.render.filepath = os.path.join(
            output_path,
            filename + formatNumbers(f, 3) + ".jpg",
            )
    # render opengl
    bpy.ops.render.opengl(write_still=True)

# reset internal filepath
bpy.context.scene.render.filepath = output_path

Note: .jpg is only a placeholder and will be overwritten by the settings in the render panel.

p2or
  • 15,860
  • 10
  • 83
  • 143
Erik Bongers
  • 321
  • 2
  • 8