0
$\begingroup$

I am writing python scripts in blender for a team of 3D-Artists. The scripts and the blender version are on a network drive "R:". When calling the script

R:\blender-2.82a\blender.exe -b --python-expr "import bpy; print(bpy.context.preferences.addons['cycles'].preferences.devices.keys());"

The output for everybody is:

['GeForce GTX 1050', 'AMD Ryzen 5 3400G with Radeon Vega Graphics']

Which are my devices. The 3D-Artists have other devices. Can it be that the devices listed in the cycles addon are not derived at runtime? Or how could that happen? I want to activate the gpu for every 3D-Artist respectively, based on their own hardware.

Best, Manuel

$\endgroup$
  • 1
    $\begingroup$ You haven't retrieved the current list of devices through get_devices(). See this answer for reference. Note that the answer hasn't been updated to include OptiX. $\endgroup$ – Robert Gützkow Jun 17 at 13:22
0
$\begingroup$

Okay. Robert's answer was the key. Apparently the array in bpy.context.preferences.addons['cycles'].preferences.devices holds all devices that have ever been activated and saved in the blender gui. In order to get the current hardware and to update the devices array, you call:

preferences = bpy.context.preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = preferences.get_devices()

And then you do something like:

gpu_device = None

for device in preferences.devices:
    device.use = False #reset all other devices
    if device.type == "CUDA" and device in cuda_devices:
        gpu_device = device

bpy.context.scene.cycles.device = "GPU"
bpy.context.preferences.addons["cycles"].preferences.compute_device_type = gpu_device.type
gpu_device.use = True

for scene in bpy.data.scenes:
    scene.render.engine = "CYCLES"
    scene.cycles.device = "GPU"
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.