本文介紹如何使用 Python 創建新材質、添加著色器、創建新對象並將材質分配給 Blender 中的對象。
首先,創建一種新材質。該函數將字符串作為新材質的名稱:
import bpy
def newMaterial(id):
mat = bpy.data.materials.get(id)
if mat is None:
mat = bpy.data.materials.new(name=id)
mat.use_nodes = True
if mat.node_tree:
mat.node_tree.links.clear()
mat.node_tree.nodes.clear()
return mat
然後為材質添加著色器。輸入著色器的類型(即漫反射、發射、光澤)和 rgb 顏色:
def newShader(id, type, r, g, b):
mat = newMaterial(id)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output = nodes.new(type='ShaderNodeOutputMaterial')
if type == "diffuse":
shader = nodes.new(type='ShaderNodeBsdfDiffuse')
nodes["Diffuse BSDF"].inputs[0].default_value = (r, g, b, 1)
elif type == "emission":
shader = nodes.new(type='ShaderNodeEmission')
nodes["Emission"].inputs[0].default_value = (r, g, b, 1)
nodes["Emission"].inputs[1].default_value = 1
elif type == "glossy":
shader = nodes.new(type='ShaderNodeBsdfGlossy')
nodes["Glossy BSDF"].inputs[0].default_value = (r, g, b, 1)
nodes["Glossy BSDF"].inputs[1].default_value = 0
links.new(shader.outputs[0], output.inputs[0])
return mat
然後創建對象,分配材質並調用函數:
def drawObject():
mat = newShader("Shader1", "diffuse", 1, 1, 1)
bpy.ops.mesh.primitive_cube_add(size=2, align='WORLD', location=(0, 0, 0))
bpy.context.active_object.data.materials.append(mat)
drawObject()
原文鏈接:Blender程序化分配材質 — BimAnt