Skip to content

Instantly share code, notes, and snippets.

@rafa-br34
Last active May 13, 2024 18:19
Show Gist options
  • Save rafa-br34/7aa90bbf9b2db972df80994416224830 to your computer and use it in GitHub Desktop.
Save rafa-br34/7aa90bbf9b2db972df80994416224830 to your computer and use it in GitHub Desktop.
Interactive way of selecting which compute devices to use when background rendering with Blender
import cycles
import bpy
import sys
# blender -b file.blend -o output_folder/frame_ -P Devices.py
preferences = bpy.context.preferences
cycles_preferences = preferences.addons["cycles"].preferences
cycles_preferences.refresh_devices()
devices = cycles_preferences.devices
def list_enum(enum):
return [opt[0] for opt in enum]
def print_devices():
print("Devices:")
for device in devices:
print(f" [{device.id}]<{device.type}> \"{device.name}\" Using: {device.use}")
print(f"Compute device type: {cycles_preferences.compute_device_type}")
print(f"Cycles device: {bpy.context.scene.cycles.device}")
def select_devices():
if not devices:
raise RuntimeError("Unsupported device type")
for device in devices:
if "y" in input(f"[{device.id}]<{device.type}> \"{device.name}\" Use device? (y/N): ").lower():
device.use = True
else:
device.use = False
cycles_preferences.compute_device_type = input(f"Select the preferred compute device [{', '.join(list_enum(cycles.properties.enum_device_type))}]: ").upper()
bpy.context.scene.cycles.device = input(f"Select the compute device type [{', '.join(list_enum(cycles.properties.enum_devices))}]: ").upper()
def main():
assert sys.stdin, "sys.stdin should be available (is the script running using the --python/-P argument?)"
select_devices()
print_devices()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment