程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Blender programmatically set materials [Python]

編輯:Python

This article describes how to use Python to create new materials, add shaders, create new objects, and assign materials to objects in Blender.

First, create a new material.The function takes a string as the name of the new material:

import bpydef newMaterial(id):mat = bpy.data.materials.get(id)if mat is None:mat = bpy.data.materials.new(name=id)mat.use_nodes = Trueif mat.node_tree:mat.node_tree.links.clear()mat.node_tree.nodes.clear()return mat

Then add shaders to the material.Input shader type (i.e. diffuse, emissive, glossy) and rgb color:

def newShader(id, type, r, g, b):mat = newMaterial(id)nodes = mat.node_tree.nodeslinks = mat.node_tree.linksoutput = 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 = 1elif 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 = 0links.new(shader.outputs[0], output.inputs[0])return mat

Then create the object, assign the material and call the function:

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()

Original link: Blender Procedural Assignment of Materials — BimAnt


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved