Sub topic 2
Shader nodes, sockets and the Material Output
This chapter assumes you can move around the Blender viewport, switch editors, and select objects. It also assumes you have already created a material and assigned it to an object, which the previous chapter covered. Here you will learn how the node graph that defines a material is built, how data moves through it, and why the Material Output node decides what the renderer sees.
Why materials are node graphs
A material in Blender is a small program drawn as a graph. Each box in the graph is a node. A node takes some inputs, does one job, and produces outputs. You connect the output of one node to the input of another with a line called a link. The renderer reads the graph from the end and works backward to find out how light should behave at each point on a surface.
You edit this graph in the Shader Editor. Select an object, open the Shading workspace or change any editor to Shader Editor, and the active material's nodes appear. A new material with nodes enabled contains two nodes: a Principled BSDF connected to a Material Output.
Anatomy of a node
Every node has the same parts.
- Header: shows the node's name and a collapse arrow. Click the arrow to hide the body of the node when it gets crowded.
- Output sockets: small colored circles on the right edge. They send values out.
- Input sockets: small colored circles on the left edge. They receive values.
- Properties: dropdowns and checkboxes inside the body that choose a mode, such as the interpolation type on an image texture. These are not sockets, so you cannot link to them.
A socket is a connection point for one value. An input socket that has no link shows its own value, such as a color swatch or a number field. That value is the default. When you link something to the socket, the default is ignored and the linked value is used instead. The number field disappears and the socket shows only its label.
Socket colors and types
The color of a socket tells you what kind of data it carries.
| Socket color | Data type | Typical use |
|---|---|---|
| Gray | Float (a single number) | Roughness, Metallic, strength values |
| Yellow | Color (red, green, blue, alpha) | Base Color, image output |
| Purple | Vector (three numbers) | Coordinates, normals, directions |
| Green | Shader | The result of a BSDF, mixed or added shaders |
| Blue-gray | Integer or boolean | Counts and on/off switches in some nodes |
Blender converts between types when you connect them. A float linked into a color input becomes a gray color. A color linked into a float input is converted to a single brightness value. A color linked into a vector input uses its red, green and blue channels as X, Y and Z. These conversions are convenient but are a common source of confusion, because the numbers may not mean what you expect.
The shader type is the exception. A green shader output can only connect to a green shader input. You cannot plug a color into a shader socket to make a surface that color. The color must go into a shader node's Base Color input, and that node's shader output goes on toward the Material Output.
Nodes that make shaders versus nodes that make data
It helps to sort nodes into two groups.
- Shader nodes produce a green shader output. They describe how a surface interacts with light. Examples are Principled BSDF, Diffuse BSDF, Glossy BSDF, Emission and Transparent BSDF.
- Data nodes produce floats, colors or vectors. Examples are Image Texture, Noise Texture, Color Ramp, Mapping and Math. They feed values into shader nodes.
Mix Shader and Add Shader are a special case. They take two shader inputs and produce one shader output, so they combine surfaces rather than values. Mix Shader uses a Fac input (factor) between 0 and 1 to blend. At 0 you get the first shader, at 1 the second, and at 0.5 an even blend.
The Material Output node
The Material Output node is the end of the graph. It has three input sockets.
- Surface: the shader that describes how light bounces off or passes through the outside of the object. This is where your Principled BSDF connects.
- Volume: a shader that describes what happens to light inside the object, for example fog, smoke or the color of a thick liquid.
- Displacement: a vector or height value that moves the surface. It needs the right displacement setting on the material to take effect, which a later chapter covers.
The node also has a Target dropdown with three options: All, Eevee and Cycles. With All, the output is used by both render engines. If you set one output to Eevee and add a second output set to Cycles, each engine uses its own output. This lets one material look different in each engine without a separate material.
Nodes that are not connected to the active Material Output have no effect on the render, even if they are visible and filled in. This is the most common reason a change seems to be ignored.
Building a graph by hand
Follow these steps to rebuild the default setup from scratch, which shows how each piece connects.
- Select a mesh object and open the Shading workspace.
- In the Shader Editor, click New if the object has no material. Press A to select all nodes and X to delete them. The graph is now empty.
- Press Shift+A, open Output, and choose Material Output.
- Press Shift+A, open Shader, and choose Principled BSDF.
- Drag from the BSDF output socket, labeled BSDF, to the Material Output's Surface socket.
- Change Base Color on the BSDF and watch the viewport update, if the viewport is in Material Preview mode.
A quicker way to connect nodes is to drop a new node onto an existing link. Blender inserts it in the middle and connects the matching sockets automatically.
Reading and editing the graph with Python
The same graph is available through the bpy module. Nodes live in material.node_tree.nodes, and links live in material.node_tree.links. The script below builds the graph from the steps above and prints every link, which is a quick way to inspect a material you did not make.
import bpy
mat = bpy.data.materials.new(name="GraphDemo")
mat.use_nodes = True
tree = mat.node_tree
# Start from an empty graph
tree.nodes.clear()
# Create the two nodes
output = tree.nodes.new("ShaderNodeOutputMaterial")
bsdf = tree.nodes.new("ShaderNodeBsdfPrincipled")
bsdf.location = (-300, 0)
# Link the BSDF output socket to the Surface input socket
tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
# Print every link as "node.socket -> node.socket"
for link in tree.links:
print(
f"{link.from_node.name}.{link.from_socket.name} -> "
f"{link.to_node.name}.{link.to_socket.name}"
)The output reads Principled BSDF.BSDF -> Material Output.Surface. Sockets are looked up by name, such as inputs["Surface"], or by position, such as inputs[0]. Names are safer because the order of sockets can change between Blender versions. Names can also change, so a script written for one version may need small edits for another.
How the renderer reads the graph
When a ray of light hits the surface, the renderer starts at the active Material Output. It asks the node linked to Surface what to do. That node asks the nodes linked to its own inputs for their values, and so on back to the beginning. Image textures are sampled at the point being shaded, using that point's coordinates.
This backward reading explains why disconnected nodes cost nothing. The renderer never visits them.
Common mistakes
- Editing a node that is not linked to the active output. The change appears in the node but not in the render. Follow the links from the node to the output to confirm.
- Expecting a color to connect to a shader socket. The socket types do not match. Use the color on a shader's Base Color or Color input instead.
- Forgetting to enable nodes in a script. Without
use_nodes = Trueon older versions,material.node_treeisNone, and any attempt to readnodesraises anAttributeError. - Looking up
"Principled BSDF"by name after clearing nodes. The name exists only on nodes created by default. Once you create a node yourself, keep the variable instead of searching by name, or set thenameattribute yourself. - Leaving several unconnected nodes behind. They do not change the render, but they make the graph hard to read. Delete them or group them in a frame.
Exercise
A material has two Principled BSDF nodes. You change the Base Color on the first one to bright green, but the rendered object stays red. The viewport shows the green node is filled in. Give two possible causes and say how you would check each.
Show answer
First, the green node may not be linked to the Surface input of the active Material Output, so the renderer never reads it. Follow its BSDF output socket by eye and confirm a link reaches Surface on the active output. Second, the material may have several Material Output nodes, or the Target may be set to the other render engine, so the output you edited is not the one in use. Check which output has the bright header and that its Target is All or matches the current engine.
Exercise
Write a short Python snippet that gets the Material Output node of a material named "GraphDemo" and prints the name of the node connected to its Surface input, or "nothing" if the input is not linked.
Show answer
import bpy mat = bpy.data.materials["GraphDemo"] tree = mat.nodetree output = next(n for n in tree.nodes if n.type == "OUTPUTMATERIAL") surface = output.inputs["Surface"] if surface.islinked: print(surface.links[0].fromnode.name) else: print("nothing")