Skip to content

Instantly share code, notes, and snippets.

@bholzer
Last active August 7, 2023 11:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bholzer/137978e86e81377de0024afc33086923 to your computer and use it in GitHub Desktop.
Save bholzer/137978e86e81377de0024afc33086923 to your computer and use it in GitHub Desktop.
Blender rendering from the command line.
import sys
import bpy
import addon_utils
argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"
C = bpy.context
bpy.context.scene.cycles.device = 'GPU'
cycles_prefs = C.user_preferences.addons['cycles'].preferences
cycles_prefs.compute_device_type = "CUDA"
# Don't step on the work of the other threads
bpy.data.scenes["Scene"].render.use_overwrite = False
bpy.data.scenes["Scene"].render.use_placeholder = True
# Turn off all GPUs
for device in cycles_prefs.devices:
device.use = False
# And then choose only the one we want
cycles_prefs.devices[int(argv[0])].use = True
bpy.ops.render.render(animation=True)
import sys
import bpy
C = bpy.context
bpy.context.scene.cycles.device = 'GPU'
cycles_prefs = C.user_preferences.addons['cycles'].preferences
cycles_prefs.compute_device_type = "CUDA"
for device in cycles_prefs.devices:
device.use = True
# When rendering an animation, you should use a blender process for each GPU on the system. If you have 4 GPUs, for example:
blender -b myanimation.blend -noaudio -nojoystick --use-extension 1 -E CYCLES -s 0 -e 250 -o ~/myanimation -P animation_config.py -- 1
blender -b myanimation.blend -noaudio -nojoystick --use-extension 1 -E CYCLES -s 0 -e 250 -o ~/myanimation -P animation_config.py -- 2
blender -b myanimation.blend -noaudio -nojoystick --use-extension 1 -E CYCLES -s 0 -e 250 -o ~/myanimation -P animation_config.py -- 3
blender -b myanimation.blend -noaudio -nojoystick --use-extension 1 -E CYCLES -s 0 -e 250 -o ~/myanimation -P animation_config.py -- 4
# The number at the end of each line is passed as an argument to the script, and it chooses which GPU to use for that process.
# The 0th device is the CPU, which is why these start at 1.
# To render just a single frame, it's pretty straightforward:
blender -b myanimation.blend -noaudio -nojoystick --use-extension 1 -E CYCLES -o ~/myanimation -P image_config.py -f 1
#NOTE: option order matters, -f must go at the end of that command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment