8
$\begingroup$

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!

$\endgroup$
1

1 Answer 1

11
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Might be better to write a real answer that's helpfull for others too? $\endgroup$
    – p2or
    Mar 25, 2015 at 9:58
  • $\begingroup$ I added some bits, feel free to improve it. $\endgroup$
    – CodeManX
    Mar 25, 2015 at 10:34
  • 1
    $\begingroup$ @CoDEmanX really nice, will add my example to it, great teamwork :) $\endgroup$
    – p2or
    Mar 25, 2015 at 10:35

You must log in to answer this question.

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