在使用bpy编写代码的时候,出现报错
RuntimeError: 错误: Python: Traceback (most recent call last):
File "c:\Users\ASUS\.vscode\extensions\jacqueslucke.blender-development-0.0.25\pythonFiles\include\blender_vscode\operators\script_runner.py", line 17, in execute
runpy.run_path(self.filepath, init_globals={"CTX": ctx})
File "D:\Software\blender-3.6.18-windows-x64\3.6\python\lib\runpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "D:\Software\blender-3.6.18-windows-x64\3.6\python\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "D:\Software\blender-3.6.18-windows-x64\3.6\python\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "e:\blender_project\doorway.py", line 47, in <module>
bsdf = material.node_tree.nodes["Principled BSDF"]
KeyError: 'bpy_prop_collection[key]: key "Principled BSDF" not found'
位置: D:\Software\blender-3.6.18-windows-x64\3.6\scripts\modules\bpy\ops.py:111
错误代码行:
# 创建材质并添加纹理
material = bpy.data.materials.new(name=f"{asset_id}_Material")
material.use_nodes = True
bsdf = material.node_tree.nodes["Principled BSDF"] # 这一行出错
# 加载纹理图像
texture_image = bpy.data.images.load(texture_path)
# 创建纹理节点
texture_node = material.node_tree.nodes.new(type='ShaderNodeTexImage')
texture_node.image = texture_image
material.node_tree.links.new(bsdf.inputs['Base Color'], texture_node.outputs['Color'])
gpt给出的回答是不存在"Principled BSDF"节点,根据gpt修改了代码,相当于创建了"Principled BSDF"节点再继续操作(这里就不给出了),随后查看blender,发现在blender着色器中查看会发现有两个"原点BSDF"节点。
直观一点,在info中查看,会发现中文设置下"Principled BSDF"变成了"原点BSDF"。
突然意思到我的blender语言设置成了中文,怪不得一开始找不到"Principled BSDF",把blender语言修改为英文之后,使用原来的代码就跑通了orz。