Blender bpy Module Ignores GPU Configuration for Rendering

87 Views Asked by At

I'm attempting to use bpy (Blender) to render a scene using Python 3.10.0 on Windows. My laptop is equipped with an NVIDIA GeForce RTX 3050. Despite configuring the GPU, I consistently observe the CPU being used instead of the GPU. What am I missing?

def foo():
    bpy.ops.wm.open_mainfile(filepath=f"./sample4.blend")

    duration = 10
    frames = int(math.ceil(duration * 30))

    # Set GPU rendering options
    preferences = bpy.context.preferences
    cycles_preferences = preferences.addons['cycles'].preferences
    cycles_preferences.compute_device_type = 'CUDA'
    bpy.context.scene.cycles.device = 'GPU'
    cycles_preferences.get_devices()
    # I verified that [0] is a NVIDIA GeForce RTX 3050 Laptop GPU
    bpy.context.preferences.addons['cycles'].preferences.devices[0].use = True 

    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.cycles.device = 'GPU'
    bpy.context.scene.cycles.feature_set = 'SUPPORTED'
    bpy.context.scene.cycles.compute_device_type = 'CUDA'
    bpy.context.scene.cycles.device = 'GPU'
    bpy.context.scene.cycles.use_cpu = False  # Disable CPU rendering


    bpy.context.scene.render.resolution_x = 1280
    bpy.context.scene.render.resolution_y = 720
    bpy.context.scene.render.fps = 30
    bpy.context.scene.frame_start = 1
    bpy.context.scene.frame_end = frames
    bpy.context.scene.render.image_settings.file_format = "FFMPEG"
    bpy.context.scene.render.ffmpeg.format = "MPEG4"
    bpy.context.scene.render.ffmpeg.codec = "H264"
    bpy.context.scene.render.filepath = r"C:/.../final.mp4"  # Modify this path
1

There are 1 best solutions below

0
35308 On

It could be a driver issue, assuming you do have recent drivers you should check your Cycles Render Devices setting (you can find those under Edit->Preferences->System->Cycles Render Devices). Setting it to OptiX is probably your best bet (you can save that to your default configuration), I've found it to be lighter on the CPU but if what you're seeing is Blender just not using your GPU at all it's probably not that. I found https://blender.stackexchange.com/questions/104651/selecting-gpu-with-python-script on the Blender SE, if you haven't already you should check it out. In general you may have better luck asking your question on the Blender SE.