2
$\begingroup$

Blender allows the user to select "GPU Compute" as the rendering device even if the computer has no valid GPU device. It simply Greys out the drop down menu:

enter image description here

To a Python script, it then looks like a GPU compute device is in use:

enter image description here

I need a way to check from my Python script if an actual valid GPU device will be used for rendering or not.

Cycles will silently default to the CPU if the user selects GPU but doesn't have one, but I don't know how to detect this from a script.

$\endgroup$

1 Answer 1

2
$\begingroup$

A bit of ferreting around yielded this:

import bpy
import _cycles

cycles_preferences = bpy.context.preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = cycles_preferences.get_devices()
for device in cuda_devices:
    if device.use == True:
        print("Device -", device.name, "is", device.use)

Basically, it will iterate through the available devices in the "cuda_devices" (you can iterate "opencl_devices" too) and "device.use" will be true if it is active. So if anything returns True you know a valid rendering device is active. You can adapt it to suit your application.

$\endgroup$
1
  • 3
    $\begingroup$ Good solution, I think. But I've found a simpler one over on the dev forums. You can just call context.preferences.addons["cycles"].preferences.has_active_device(). It'll only return true if a GPU device is configured. False means no active device and CPU will be used. Cheers. $\endgroup$
    – Lewis
    Jan 14, 2021 at 9:51

You must log in to answer this question.

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