Sub topic 2
Putting an image on a material with the Image Texture node
This chapter shows how to connect an image file to a material so that it colors a mesh. It assumes you can navigate the 3D viewport, switch between Object Mode and Edit Mode, select vertices, edges and faces, and that you have used a material in Blender before. UV unwrapping is covered in later chapters. Here you use whatever UV map the mesh already has, such as the default cube's.
The working model
A material in Blender is a node graph. The Principled BSDF node is a shader that describes how a surface reflects light. Its inputs, such as Base Color, Roughness and Normal, each accept either a fixed value or a link from another node.
The Image Texture node reads an image and outputs one color for each point on the surface. To pick that color, it needs a UV coordinate, a 2D position inside the image, for every point being shaded. Without a link in its Vector input, the node uses the mesh's active UV map automatically. The steps are:
- The renderer shades a point on a face.
- It looks up the UV coordinate that the mesh stores for that point.
- The Image Texture node reads the image color at that coordinate.
- The color goes to the Principled BSDF input it is linked to.
Setting it up in the interface
- Select the object and open the Shading workspace. It has a viewport, a Shader Editor and an Image Editor.
- If the object has no material, click New in the Shader Editor header. Blender creates a material with a Principled BSDF node connected to a Material Output node.
- With the mouse over the Shader Editor, press
Shift+A, choose Texture, then Image Texture, and click to place the node. - In the node, click Open and choose an image file.
- Drag from the node's Color output socket to the Base Color input of the Principled BSDF.
- Switch the viewport to Material Preview shading to see the result.
A shortcut: select the Principled BSDF node and press Ctrl+Shift+T. This opens a file browser and builds a set of Image Texture nodes already connected. It is a convenient way to start, but it relies on the node add-on being enabled and on file names that match its naming rules. Building the connection by hand once makes it clear what the shortcut does.
If the mesh looks wrong, such as a single smeared color or a texture that is tiny, check the UV map before checking the node. The Image Texture node can only use the coordinates the mesh provides.
The Color and Alpha outputs
The Image Texture node has two outputs.
- Color carries the red, green and blue values of the pixel. Use it for Base Color.
- Alpha carries the transparency of the pixel, if the image has an alpha channel. Linking it to the Principled BSDF's Alpha input makes parts of the surface see-through, provided the material's blend settings allow transparency.
If an image has an alpha channel, the node's Alpha setting controls how that channel is treated. The common options are Straight, Premultiplied, Channel Packed and None. Use None to ignore alpha. For a photo or painted color map with no transparency, the default is fine.
The node's main settings
The node shows four settings under the image name. Each is covered in more detail in later chapters, but you should recognize them now.
| Setting | What it controls | Typical value for color |
|---|---|---|
| Interpolation | How colors are blended between pixels when the image is magnified | Linear |
| Projection | How the image is mapped onto the object | Flat |
| Extension | What happens for coordinates outside the 0 to 1 range | Repeat |
| Color Space | How pixel values are interpreted | sRGB |
The color space setting matters from the first render. An image that holds visible color, such as a brick photo, should be sRGB. An image that holds data, such as a roughness or normal map, should be Non-Color. Setting the wrong one changes the numbers the shader receives. Chapter nine returns to this in depth.
Doing it with Python
The same setup can be scripted. This script creates a material, adds an Image Texture node, loads an image relative to the .blend file, and links it to Base Color.
import bpy
obj = bpy.context.active_object
mat = bpy.data.materials.new("TexturedMaterial")
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
bsdf = nodes["Principled BSDF"]
tex = nodes.new("ShaderNodeTexImage")
tex.image = bpy.data.images.load("//textures/brick_color.png")
tex.image.colorspace_settings.name = "sRGB"
links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
obj.data.materials.append(mat)Points to notice:
- The prefix
//means "relative to the folder of the saved .blend file". The script fails with an error if the file has not been saved, or if the image does not exist at that path. bpy.data.images.loadcreates an image data-block that points to the file. Loading the same path twice reuses the existing block only if you passcheck_existing=True. Otherwise you get a second copy, named likebrick_color.png.001.obj.data.materials.append(mat)adds a new material slot. If the object already has a slot, you may prefer to assignobj.data.materials[0] = matinstead of stacking slots.- The Principled BSDF node is found by its name. In a non-English interface the node's displayed label can differ, but its internal name stays
"Principled BSDF".
Common errors
- The image shows as pink or magenta in the viewport. The file is missing. Blender cannot find it at the stored path. Use File > External Data > Find Missing Files, or reopen the image from its new location.
- The object is a single flat color. The mesh has no UV map, or all its UV coordinates collapse to one point. Check the Data properties tab, under UV Maps. Add one if the list is empty.
- The texture looks correct in Material Preview but wrong in a render. Check that the same image and node are used by the active render engine's material, and that Color Space is set the way you intended.
- The colors look washed out or too dark. The color space is probably wrong. A color image set to Non-Color looks flat. A data image set to sRGB gets its values bent by the transfer curve.
- The image disappears when you reopen the file. An image loaded from disk is linked by path. If you moved the file, it goes missing. An image you created inside Blender, with no file, is lost on close unless you save or pack it. The chapter on saving and packing covers both cases.
- Stretched or tiled patterns appear. The Image Texture node is working correctly. The cause is the UV layout. The next chapters cover unwrapping and checking for stretch.
Checking your setup
Use this short list whenever a texture does not appear as you expect.
- Is the correct material assigned to the correct slot of the object?
- Does the Image Texture node show an image, and does the image load without a pink warning?
- Is the Color output linked to Base Color, not to another input?
- Does the mesh have an active UV map?
- Is the viewport in Material Preview or Rendered mode?
- Is the color space correct for what the image contains?
Exercise
You link an image's Color output to Base Color, and the viewport shows a single flat color across the whole object. The image itself opens fine in the Image Editor. Name two things you would check, and say which check comes first.
Show answer
Check first that the mesh has an active UV map (Data properties tab, UV Maps list), because the Image Texture node reads its coordinates from there. If a map exists, check that its coordinates are not collapsed to one point, for example by viewing the faces in the UV Editor. Also confirm the viewport is in Material Preview mode and that the Color output is linked to Base Color. The UV map is the first check because the image loads correctly, so the node's input is the more likely cause.
Exercise
Why does the script in this chapter set the color space to "sRGB", and what would you set for a roughness map?
Show answer
The image holds visible color data, which is stored with the sRGB transfer curve, so the node must decode it as sRGB. A roughness map holds numeric values that are used directly by the shader, so its color space should be "Non-Color".