[英]How to set object surface to Background in Blender with Python API
如何使用 Python API 在 Blender 中将对象的材质表面设置为背景?
我只能在 UI 中做到这一点:
我用了这个方法
obj = ut.import_objects("models/bbox.fbx")[0] # custom import function
color = (1., 0., 0., 1.)
mat = bpy.data.materials.new("mat")
mat.use_nodes = True
nodes = mat.node_tree.nodes
nodes.clear()
node_background = nodes.new(type='Background')
node_background.inputs[0].default_value = (0,1,0,1)
obj.data.materials.append(mat)
Error: Python: Traceback (most recent call last):
File "/blender_debug2.py", line 40, in <module>
RuntimeError: Error: Node type Background undefined
将表面更改为 Principled BSDF 确实有效。 但mat.node_tree.nodes
不包含“背景”键。
def change_color(obj: bpy.types.Object, color):
mat = bpy.data.materials.new("mat")
mat.use_nodes = True
principled = mat.node_tree.nodes['Principled BSDF']
principled.inputs['Base Color'].default_value = color
obj.data.materials.append(mat)
似乎您走在正确的轨道上,对于节点,您可以在ShaderNodeBackground
中找到背景属性,因此实际代码看起来更像
obj = ut.import_objects("models/bbox.fbx")[0] # custom import function
color = (1., 0., 0., 1.) # red
mat = bpy.data.materials.new("mat")
mat.use_nodes = True
if mat.node_tree:
mat.node_tree.links.clear()
mat.node_tree.nodes.clear()
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output = nodes.new(type='ShaderNodeOutputMaterial')
shader = nodes.new(type='ShaderNodeBackground')
nodes["Background"].inputs[0].default_value = color
links.new(shader.outputs[0], output.inputs[0])
obj.data.materials.append(mat)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.