Need a way to detect if a valid GPU device is available from Python

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:

image

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

image

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.

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

Can anyone help?

If you look at the UI code that draws those dropdown boxes you will see that it calls a function named show_device_active(context). The method code itself looks like this:

def show_device_active(context):
    cscene = context.scene.cycles
    if cscene.device != 'GPU':
        return True
    return context.preferences.addons[__package__].preferences.has_active_device()

The relevant part is the last line. You can replicate this in an own Add-on or script by calling context.preferences.addons[__package__].preferences.has_active_device()

1 Like

Hi

I actually tried that already, but context.preferences.addons[__package__].preferences.has_active_device() seemed to always return False, even on my computer with an Nvidia GPU (note I had to change __package__ to "cycles" in that code.

I probably made a mistake. I will revisit it.

Thanks for the help.

Yes this does work. I must have made some other error when I first tried it.

Many thanks for the help

1 Like