Sub topic 14
Selecting an engine from Python and the command line
This chapter shows how to choose the render engine without opening the Properties editor. You will set the engine from a Python script, pick the right EEVEE identifier for the Blender you are running, and render from a terminal. You will also configure Cycles devices for unattended renders.
It assumes you can run Blender from a terminal, you know the basic bpy pattern (bpy.context.scene and property assignment), and you know the Render properties tab. Earlier chapters covered what each engine does, so this one is about control.
Why script the engine choice
Scripted selection matters in three situations:
- Batch and farm rendering. A machine with no display renders a queue of files, and nobody is there to click the engine dropdown.
- Reproducibility. A script records the exact settings used, so a teammate can rerun it and get the same result.
- Overrides. You may want a quick EEVEE preview or a final Cycles pass from one
.blendfile without saving the file twice.
The engine is a property of each scene. It is stored in scene.render.engine and saved in the .blend file. A script or command-line flag changes the value in memory, and the change persists only if you save the file.
Engine identifiers
scene.render.engine takes a string identifier, not the name shown in the interface. Assigning a string that is not valid in the running build raises a TypeError.
| Engine | Identifier | Notes |
|---|---|---|
| Cycles | `CYCLES` | Stable across versions |
| EEVEE | `BLENDER_EEVEE` or `BLENDER_EEVEE_NEXT` | Depends on the Blender version |
| Workbench | `BLENDER_WORKBENCH` | Viewport-style rendering, no materials |
The EEVEE identifier has changed between releases. Older versions used BLENDER_EEVEE. The rewritten EEVEE introduced in Blender 4.2 was exposed as BLENDER_EEVEE_NEXT. Later releases may use BLENDER_EEVEE again for the rewritten engine. Hard-coding either string can break a script when you upgrade, so ask the running Blender instead.
Setting Cycles from Python
Cycles is the easy case because its identifier is stable. The script below sets the engine, requests the GPU, sets the sample count, and enables denoising:
import bpy
scene = bpy.context.scene
scene.render.engine = 'CYCLES'
scene.cycles.device = 'GPU'
scene.cycles.samples = 128
scene.cycles.use_denoising = TrueThe scene.cycles settings group exists whether or not Cycles is the active engine, so you can configure it before switching to it.
Setting scene.cycles.device = 'GPU' only requests GPU rendering. If no compute device is enabled in Preferences, Cycles falls back to the CPU without raising an error. The render finishes, but slower than you expected. The next sections cover how to detect this.
Finding the EEVEE identifier at runtime
Every engine appears in the enum items of the engine property. The script below lists them and picks the one that starts with BLENDER_EEVEE:
import bpy
scene = bpy.context.scene
engine_ids = [item.identifier for item in scene.render.bl_rna.properties['engine'].enum_items]
print(engine_ids)
eevee_id = next(e for e in engine_ids if e.startswith('BLENDER_EEVEE'))
scene.render.engine = eevee_idThe print call is worth keeping in the logs of a render job. When a job misbehaves after a Blender upgrade, the list shows which engines the build offered.
The startswith match works because only the EEVEE identifiers share that prefix. The next(...) call raises StopIteration if nothing matches. That is acceptable in a batch job, since a loud failure beats rendering with the wrong engine.
A reusable helper
A small function keeps this logic in one place. It accepts a friendly name and returns the identifier the running build understands:
import bpy
def set_engine(scene, name):
available = [i.identifier for i in scene.render.bl_rna.properties['engine'].enum_items]
if name == 'EEVEE':
matches = [e for e in available if e.startswith('BLENDER_EEVEE')]
else:
matches = [e for e in available if e == name]
if not matches:
raise RuntimeError(f"Engine {name!r} not available. Choices: {available}")
scene.render.engine = matches[0]
return matches[0]
used = set_engine(bpy.context.scene, 'EEVEE')
print("Using engine:", used)The error message lists the valid choices, so a typo is easy to diagnose from a log file.
Confirming that the GPU will really be used
Enable the devices through the Cycles add-on preferences. These are user preferences, not scene data. Setting them in a script affects the current Blender session. Saving them to your user preferences is a separate step.
import bpy
prefs = bpy.context.preferences.addons['cycles'].preferences
prefs.compute_device_type = 'OPTIX' # or 'CUDA', 'HIP', 'METAL', 'ONEAPI'
prefs.get_devices()
for device in prefs.devices:
device.use = device.type != 'CPU'
print(device.name, device.type, device.use)
scene = bpy.context.scene
scene.render.engine = 'CYCLES'
scene.cycles.device = 'GPU'Choose the compute type that matches your hardware. Assigning one the build does not offer raises a TypeError. The printed device list is your confirmation. If every GPU shows False, or the list holds only the CPU, the render will run on the CPU.
Command-line rendering
Blender can render with no interface. The -b flag (background) loads the file and exits when the work is done. The command below renders one frame with Cycles:
blender -b scene.blend -E CYCLES -o //render_#### -f 1| Argument | Meaning |
|---|---|
| `-b` | Run in the background, with no window |
| `-E CYCLES` | Set the render engine for the scene (`--engine` also works) |
| `-o //render_####` | Output path. `//` means relative to the `.blend` file, and `####` becomes the zero-padded frame number |
| `-f 1` | Render frame 1 and exit |
Two more flags are useful. -a renders the whole animation range, and -s and -e override the start and end frames. Running blender -b -E help lists the engine names that build accepts.
Argument order matters
Blender processes arguments from left to right. A flag takes effect only for the actions that come after it. The -f and -a flags trigger the render, so put -E and -o before them. This command renders with the engine saved in the file, because the engine flag arrives too late:
blender -b scene.blend -f 1 -E CYCLESThe file is also loaded in order. Put the .blend file first, so later flags apply to it.
Combining a script with the command line
When settings go beyond what flags can express, run a script with --python. The script runs when Blender reaches it in the argument list, so it should come after the file and before the render flag:
blender -b scene.blend --python set_engine.py -o //render_#### -f 1The --python-expr flag takes a one-line Python string instead of a file. Add --python-exit-code 1 so that an exception in your script makes Blender exit with a nonzero status. Without it, a failed script can still be followed by a render, and a job runner would report success.
Cycles also accepts a device override on the command line. Arguments after a lone -- are passed to scripts and add-ons instead of Blender itself:
blender -b scene.blend -E CYCLES -f 1 -- --cycles-device OPTIXUse CPU, CUDA, OPTIX, HIP, METAL or ONEAPI to match your hardware. This route avoids editing preferences in a script.
Gotchas
- Several scenes.
bpy.context.sceneis only the active scene. A file with several scenes needs a loop overbpy.data.scenes, and the engine flag-Ealso acts on the active scene only. - Saved files. Changing the engine in a script does not touch the file on disk. Call
bpy.ops.wm.save_mainfile()if you want to keep the change. - EEVEE with no display. EEVEE draws with the GPU through OpenGL, so a headless server may fail to start it. A virtual display such as Xvfb can supply a context. Cycles on the CPU has no such need.
- Switching alone is not conversion. Setting the identifier changes the engine but not the scene. Settings such as EEVEE's screen-space effects or Cycles light paths still need review, as the earlier chapter on switching engines described.
- Silent GPU fallback. Check the device list as shown above, and watch the render log, which names the devices in use.
Exercises
Exercise
A script contains scene.render.engine = 'BLENDER_EEVEE'. It works on your machine but raises a TypeError on a render server running a newer Blender. What is the likely cause, and how do you fix the script?
Show answer
The EEVEE identifier differs between Blender versions, and the server's build does not offer the string you hard-coded. Read the identifiers from scene.render.bl_rna.properties['engine'].enum_items and choose the one that starts with BLENDER_EEVEE, as in next(e for e in engine_ids if e.startswith('BLENDER_EEVEE')).
Exercise
You run blender -b shot.blend -f 10 -E CYCLES -o //out_####. The file was saved with EEVEE and the render comes out in EEVEE. Why, and what is the corrected command?
Show answer
Arguments run from left to right, and -f 10 starts the render before -E and -o are read. The corrected command is blender -b shot.blend -E CYCLES -o //out_#### -f 10.
Exercise
A Cycles script sets scene.cycles.device = 'GPU' and the job finishes without errors, but it takes as long as a CPU render. Name one way to confirm what happened and one fix.
Show answer
Loop over bpy.context.preferences.addons['cycles'].preferences.devices after calling get_devices() and print each device's use value. If no GPU has use set to True, Cycles fell back to the CPU. Fix it by setting compute_device_type to the right type and setting use = True on the GPU devices, or by passing -- --cycles-device OPTIX (or the type that matches your hardware) on the command line.