Sub topic 14
Scripting Renders with Python
Assume you know Blender’s interface, have a scene with a camera, lights, and objects, and can run a basic Python script. Blender’s Python API, exposed through the bpy module, lets you set render properties and start renders without repeatedly clicking the interface.
Render a Still Image
The active scene is available as bpy.context.scene. Set its render properties, then call bpy.ops.render.render(write_still=True). The write_still argument saves the result to the path in scene.render.filepath.
import bpy
scene = bpy.context.scene
scene.render.engine = 'BLENDER_EEVEE_NEXT'
scene.render.resolution_x = 1920
scene.render.resolution_y = 1080
scene.render.resolution_percentage = 100
scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = '//renders/still.png'
bpy.ops.render.render(write_still=True)The // prefix means a path relative to the directory containing the current .blend file. Create the renders directory first, or choose an existing directory. A relative path makes a script easier to move between computers.
The script uses the scene’s active camera automatically. It does not change which camera is active. Check scene.camera before rendering:
if scene.camera is None:
raise RuntimeError("The scene has no active camera")Render an Animation to Video
For an animation, set the frame range and frame rate. Blender then evaluates the scene at each frame. The following script writes an H.264 video inside an MPEG-4 container.
import bpy
scene = bpy.context.scene
if scene.camera is None:
raise RuntimeError("The scene has no active camera")
scene.frame_start = 1
scene.frame_end = 120
scene.render.fps = 24
scene.render.resolution_x = 1920
scene.render.resolution_y = 1080
scene.render.resolution_percentage = 100
scene.render.image_settings.file_format = 'FFMPEG'
scene.render.ffmpeg.format = 'MPEG4'
scene.render.ffmpeg.codec = 'H264'
scene.render.ffmpeg.constant_rate_factor = 'MEDIUM'
scene.render.ffmpeg.audio_codec = 'NONE'
scene.render.filepath = '//renders/clip.mp4'
bpy.ops.render.render(animation=True)A container, such as MPEG-4, packages the streams. A codec, such as H.264, compresses the video frames. The constant rate factor controls the quality and file size trade-off. Check the available settings for your Blender version when a property differs.
Choose Between a Script and the Interface
A script is useful when you need repeatable settings, batch jobs, or renders on a render server. Store important values near the top of the script so another person can audit them. Avoid relying on whatever settings happen to be selected in the current file.
Common Scripting Errors
write_still=Trueis missing, so Blender renders an image but does not save it.scene.render.filepathpoints to a directory or an unexpected relative location.scene.cameraisNone, or the active camera points away from the objects.- The frame range is incorrect, so the animation is too short or too long.
- Reusing the same output path overwrites an earlier render.
Exercise
Which argument saves a still render to the configured output path, and which argument starts an animation render?
Show answer
Use write_still=True for a still image and animation=True for an animation.