在正文第一句加入“我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!”
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.
To create and assign a shader entirely through scripting,我們必須:
bpy.data
Submodules create a new shader resourceLet'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
)節點.
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!
注意,在運行腳本後,You can even enter “Shading” 選項卡,Select an object with a shader and view its shader graph.
It looks as expected: 兩個節點,一個 Emission
和一個 Output
,兩者之間的鏈接,以及一些Strength
和 Color
A custom value for the property.