You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blender bpy模块渲染时忽略GPU配置问题排查

问题:Windows下用bpy调用Cycles渲染时GPU未工作,仅CPU运行

我在Windows系统下使用Python 3.10.0调用bpy(Blender)模块渲染场景,笔记本搭载NVIDIA GeForce RTX 3050显卡。尽管已配置GPU渲染选项,但始终是CPU在工作而非GPU,请问我遗漏了什么配置?

附原代码:

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

核心问题与修正方案

你的代码存在3个关键疏漏,导致GPU未被正确激活:

1. 配置顺序错误

打开.blend文件会覆盖当前的偏好设置,你应该先配置GPU设备,再加载目标场景文件,否则之前的GPU配置会被文件自带的设置冲掉。

2. 未禁用CPU设备

你只启用了单GPU设备,但默认CPU设备仍处于启用状态,Cycles会优先选择CPU。需要遍历所有设备,禁用GPU以外的设备。

3. 冗余配置+未利用RTX特性

多次重复设置cycles.device = 'GPU'无意义,且RTX 3050支持性能更好的OptiX渲染后端,没必要局限于CUDA。

修正后的完整代码

import bpy
import math

def foo():
    # 第一步:先配置GPU渲染偏好,再打开场景文件
    preferences = bpy.context.preferences
    cycles_preferences = preferences.addons['cycles'].preferences
    
    # 优先启用OptiX(RTX 3050原生支持),失败则 fallback 到CUDA
    try:
        cycles_preferences.compute_device_type = 'OPTIX'
    except TypeError:
        cycles_preferences.compute_device_type = 'CUDA'
    
    # 刷新设备列表,确保识别到所有硬件
    cycles_preferences.get_devices()
    
    # 启用所有GPU设备,禁用CPU/其他设备
    for device in cycles_preferences.devices:
        if device.type == 'GPU':
            device.use = True
            print(f"启用GPU设备: {device.name}")
        else:
            device.use = False
            print(f"禁用非GPU设备: {device.name}")
    
    # 第二步:加载场景文件
    bpy.ops.wm.open_mainfile(filepath=f"./sample4.blend")

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

    # 配置场景渲染参数
    scene = bpy.context.scene
    scene.render.engine = 'CYCLES'
    scene.cycles.device = 'GPU'
    scene.cycles.feature_set = 'SUPPORTED'
    # 同步场景计算设备类型与偏好设置一致
    scene.cycles.compute_device_type = cycles_preferences.compute_device_type
    scene.cycles.use_cpu = False  # 强制禁用CPU
    
    # 输出配置
    scene.render.resolution_x = 1280
    scene.render.resolution_y = 720
    scene.render.fps = 30
    scene.frame_start = 1
    scene.frame_end = frames
    scene.render.image_settings.file_format = "FFMPEG"
    scene.render.ffmpeg.format = "MPEG4"
    scene.render.ffmpeg.codec = "H264"
    scene.render.filepath = r"C:/.../final.mp4"  # 修改为实际输出路径
    
    # 启动动画渲染(原代码缺失此关键步骤)
    bpy.ops.render.render(animation=True)

额外排查步骤

如果修正后GPU仍未工作,检查以下几点:

  • 驱动与Blender版本匹配:确保NVIDIA驱动版本≥450.80.02(OptiX要求),Blender版本≥2.90(支持RTX 30系列OptiX)。
  • Python与bpy版本匹配:你的Python 3.10.0必须对应Blender 3.0+版本的内置Python,版本不匹配会导致设备识别失败。
  • 笔记本独显切换:在NVIDIA控制面板中设置你的Python进程优先使用独立显卡,避免系统自动切换到核显。
  • 验证设备识别:添加以下代码确认GPU被正确识别:
    for device in cycles_preferences.devices:
        print(f"设备名称: {device.name}, 类型: {device.type}, 已启用: {device.use}")
    

内容的提问来源于stack exchange,提问作者Pavel Angel Mendoza Villafane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:24:57