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

Blender Python Programming: Creating Emitting Materials

編輯:Python

正文第一句加入“我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!”

前言

Blender Not the only one that allows you to program and automate tasks for scenes3D軟件; With each new release,Blender is gradually becoming a reliable CG Create an all-in-one solution,From storyboarding with grease pencils to node-based compositing.

事實上,你可以使用 Python Script and some extra packages to batch your object instantiations,Generate stuff programmatically,Configure your render settings,Even get custom stats for your current project,這是非常棒的功能! It's a way to lighten the burden of tedious tasks,It also enables developers to participate in this creative tools community,Not just artists.

正文

在 Blender 中,Shaders are usually done through a node-based graph editor(可以在 “Shading” 選項卡中找到)來編輯的,It lets you link and combine as many built-in nodes as you want to build more or less complex shading pipelines.然而,在本文中,We're going to make a super simple shader,只有一個節點,所以我們將在 Python 腳本 do everything in it.

Those shaders are used after the material is created,and apply to yours 3D on geometric objects.An object may have several 材質槽,例如,It can use different shaders for different parts of the geometry,But we won't discuss this complex situation today,我們將只考慮 One material slot per object 的情形.

發光材質 為例.You can think of it as having an intensity(strength 參數)and color bulbs.It will make yours 3D Object glows(So your object will be the light source in your scene,will interact with the rest of the grid!)

PS: 選擇 Shader 類型
對於這個項目,我使用的 EEVEE 引擎,It also works with shader nodes,即使它與 Cycles Engines Not all node types are the same.

在 Python Create a shader in the script

To create and assign a shader entirely through scripting,我們必須:

  • 使用 bpy.data Submodules create a new shader resource
  • “編輯” this shader,Just like we did with the shader node editor: We will delete some nodes,添加其他節點,Set their properties and link them together
  • Retrieve a reference to the newly created material
  • 最後,Add it to our object's material data(i.e. slot)中

Let's do this step by step.We will create a file named create_emission_shader() The new function starts

It will receive some 強度Strength) 和 顏色Color) 參數,and use these parameters to set a basic one 2 節點圖,其中有一個發光Emission)節點和一個輸出Output)節點.

我們的想法

  1. Start with a basic node template,Clear all initial nodes
  2. 然後,Can add ours 發光 節點和 輸出 節點,Configured by updating the value of its input field 發光 節點
  3. and create a link between the two nodes

Specific code and comments

Create an emissive material

def create_emission_shader(color, strength, mat_name):
# create a new material resource (with its
# associated shader)
mat = bpy.data.materials.new(mat_name)
# enable the node-graph edition mode
mat.use_nodes = True
# clear all starter nodes
nodes = mat.node_tree.nodes
nodes.clear()
# add the Emission node
node_emission = nodes.new(type="ShaderNodeEmission")
# (input[0] is the color)
node_emission.inputs[0].default_value = color
# (input[1] is the strength)
node_emission.inputs[1].default_value = strength
# add the Output node
node_output = nodes.new(type="ShaderNodeOutputMaterial")
# link the two nodes
links = mat.node_tree.links
link = links.new(node_emission.outputs[0], node_output.inputs[0])
# return the material reference
return mat
復制代碼

Now it's easy to use this method to create our material assets,and apply them to our objects.

例如,We will need a white glowing material for the halo,A yellow glowing material is the sun,Create a random color glow material for each planet(Although I will add more blue,For better overall color balance ):

具體調用代碼

# ...
N_PLANETS = 6
ring_mat = create_emission_shader(
(1, 1, 1, 1), 1, "RingMat"
)
for n in range(N_PLANETS):
# ...
planet = create_sphere(r, d, "Planet-{:02d}".format(n))
planet.data.materials.append(
create_emission_shader(
(random(), random(), 1, 1),
2,
"PlanetMat-{:02d}".format(n)
)
)
# add the radius ring display
ring = create_torus(d, "Radius-{:02d}".format(n))
ring.data.materials.append(ring_mat)
# add the sun sphere
sun = create_sphere(12, 0, "Sun")
sun.data.materials.append(
create_emission_shader(
(1, 0.66, 0.08, 1), 10, "SunMat"
)
)
復制代碼

如果你在 3D Change the shadow mode to “rendering”,and deletes all objects in the current scene,Then run the above script,You'll see that they now have nice glowing materials!

  • Before creating the material
  • After creating the material

注意,在運行腳本後,You can even enter “Shading” 選項卡,Select an object with a shader and view its shader graph.

It looks as expected: 兩個節點,一個 Emission 和一個 Output,兩者之間的鏈接,以及一些StrengthColor A custom value for the property.


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