TransWikia.com

Selecting GPU with Python script

Blender Asked by user3922088 on January 3, 2021

Alright, so i have a slightly strange system configuration. I’m running Zorin OS 12, which is roughly equivalent to Ubuntu 16.04. I am also running Mesa, but i use AMD’s OpenCL. It works just fine with various miners i tested.

Now i want blender to actually render on the GPU. Blender detects it when i use the split kernel, but for some reason, whatever i do, it always falls back to the cpu.

Here’s the script i use:

import bpy, _cycles
bpy.ops.wm.open_mainfile(filepath="/home/xxx/xxx.blend")

for scene in bpy.data.scenes:
    bpy.context.scene.render.engine = 'CYCLES' 
    bpy.data.scenes["Scene"].cycles.device='GPU' 
    bpy.context.scene.cycles.device = 'GPU'

bpy.context.user_preferences.addons['cycles'].preferences.compute_device_type = "OPENCL"
bpy.context.user_preferences.addons['cycles'].preferences.devices[0].use = True

bpy.ops.render.render(True)
img_path = "/home/xxx/Bilder/test_1.png"
rendered_image = bpy.data.images["Render Result"]
rendered_image.save_render(filepath=img_path)

I am really at a loss here, but i think this could work. I mean even if the GPU for some reason does not work, it should not just fall back to the CPU, but give me some sort of error, right?

uname -r

4.15.1-041501-generic

clinfo

 Platform Name                                   AMD Accelerated Parallel Processing
 Platform Vendor                                 Advanced Micro Devices, Inc.
 Platform Version                                OpenCL 2.1 AMD-APP (2527.3)
 Platform Profile                                FULL_PROFILE
 Platform Extensions                             cl_khr_icd cl_amd_event_callback cl_amd_offline_devices 
 Platform Host timer resolution                  <printPlatformInfo:5: get CL_PLATFORM_HOST_TIMER_RESOLUTION : error -30>
 Platform Extensions function suffix             AMD

EDIT

For reasons unknown to me, installing Radeon RenderPro for Blender has fixed the problem!

https://pro.radeon.com/en/software/prorender/blender/

3 Answers

For reasons unknown to me, installing Radeon RenderPro for Blender has fixed the problem!

https://pro.radeon.com/en/software/prorender/blender/

Correct answer by user3922088 on January 3, 2021

Right now, I am using Blender 2.90. Here is my Python code to detect and use GPU in Blender.

import bpy
bpy.data.scenes[0].render.engine = "CYCLES"

# Set the device_type
bpy.context.preferences.addons[
    "cycles"
].preferences.compute_device_type = "CUDA" # or "OPENCL"

# Set the device and feature set
bpy.context.scene.cycles.device = "GPU"

# get_devices() to let Blender detects GPU device
bpy.context.preferences.addons["cycles"].preferences.get_devices()
print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
    d["use"] = 1 # Using all devices, include GPU and CPU
    print(d["name"], d["use"])
```

Answered by Yang on January 3, 2021

Looks like you have multiple scenes in your file. To cycle through them and set them all use:

for scene in bpy.data.scenes:
    scene.cycles.device = 'GPU'

bpy.context refers to the to the area of blender which is currently being accessed by the user, not the script loop. If you don't have the file open, I would avoid using bpy.context calls and instead access bpy.data.

Answered by Jaroslav Jerryno Novotny on January 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP