Sub topic 12
Building the Same Graph with Python
This chapter assumes that you can navigate Blender, open the Shader Editor, and read a node graph. You should also know the Principled BSDF node and the difference between object and UV coordinates. Python is useful when you need to create the same material repeatedly or generate many related materials.
Blender exposes its data through the bpy Python module. A material's shader graph is stored in a node tree. The node tree contains nodes and links:
- A node performs an operation, such as generating noise or converting a value to a colour.
- A socket is an input or output on a node.
- A link connects one output socket to one input socket.
The following graph uses object coordinates, a Mapping node, a Noise Texture, a Colour Ramp, and a Principled BSDF.
Accessing and rebuilding a node tree
Start by creating a material and enabling nodes. The nodes collection contains the graph's nodes, while the links collection contains connections between sockets.
Removing the default nodes makes the script predictable. This is useful for a demonstration, but avoid doing it to a material that contains work you need to preserve.
import bpy
# Use the active object as the material's target.
obj = bpy.context.active_object
if obj is None:
raise RuntimeError("Select an object before running this script.")
mat = bpy.data.materials.new(name="Scripted Noise Ramp")
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
nodes.clear()
output = nodes.new("ShaderNodeOutputMaterial")
output.name = "Material Output"
output.location = (600, 0)
bsdf = nodes.new("ShaderNodeBsdfPrincipled")
bsdf.name = "Principled BSDF"
bsdf.location = (300, 0)
bsdf.inputs["Roughness"].default_value = 0.65
coord = nodes.new("ShaderNodeTexCoord")
coord.name = "Texture Coordinate"
coord.location = (-800, 0)
mapping = nodes.new("ShaderNodeMapping")
mapping.name = "Mapping"
mapping.location = (-600, 0)
mapping.inputs["Scale"].default_value = (1.0, 1.0, 1.0)
noise = nodes.new("ShaderNodeTexNoise")
noise.name = "Noise Texture"
noise.location = (-350, 0)
noise.inputs["Scale"].default_value = 6.0
noise.inputs["Detail"].default_value = 8.0
noise.inputs["Roughness"].default_value = 0.6
ramp = nodes.new("ShaderNodeValToRGB")
ramp.name = "Colour Ramp"
ramp.location = (-50, 0)
ramp.color_ramp.elements[0].position = 0.35
ramp.color_ramp.elements[0].color = (0.05, 0.03, 0.02, 1.0)
ramp.color_ramp.elements[1].position = 0.65
ramp.color_ramp.elements[1].color = (0.6, 0.5, 0.4, 1.0)
links.new(coord.outputs["Object"], mapping.inputs["Vector"])
links.new(mapping.outputs["Vector"], noise.inputs["Vector"])
links.new(noise.outputs["Fac"], ramp.inputs["Fac"])
links.new(ramp.outputs["Color"], bsdf.inputs["Base Color"])
links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
if len(obj.data.materials) == 0:
obj.data.materials.append(mat)
else:
obj.data.materials[0] = matdefault_value changes a socket's value when that socket is not connected. Once a socket has a link, the link controls the value instead. For example, the Noise Texture's Fac output controls the Colour Ramp's Fac input, so the ramp positions determine where the dark and light colours appear.
Node locations affect only the editor layout. They do not change how the material renders. Naming nodes is still useful because it makes the graph easier to inspect and lets later code find a node by name.
Making scripts safer to modify
For a reusable script, put adjustable values near the beginning:
noise_scale = 6.0
noise_detail = 8.0
dark_colour = (0.05, 0.03, 0.02, 1.0)
light_colour = (0.6, 0.5, 0.4, 1.0)Then assign those variables to the relevant sockets. This turns the script into a small material generator rather than a one-time setup script.
Common Python errors
- A misspelled node type, such as
ShaderNodeNoise, fails because Blender expectsShaderNodeTexNoise. - A misspelled socket name raises a key error. Check the socket name in the Shader Editor or Blender's API documentation.
- A script can create a material without displaying it if no object uses that material. Assign it to an object's material slot.
- Running the script repeatedly creates materials with new names. Reuse a named material if you need updates instead of duplicates.
Exercise
Which line makes the noise pattern control the material's base colour?
Show answer
links.new(noise.outputs["Fac"], ramp.inputs["Fac"]) sends the noise value to the ramp, and links.new(ramp.outputs["Color"], bsdf.inputs["Base Color"]) sends the ramp's colour to the Principled BSDF.