Smart Material Studio › Python scripting
Guide

Building a layer stack with the Substance Painter Python API

Substance 3D Painter ships a Python API that can build layers programmatically. This guide covers the layerstack calls that matter for generating a material - groups, fills, channels, masks and generators - and the gotchas worth knowing.

Why script the layer stack

Since the 2024 release (Painter 10.0), substance_painter.layerstack can insert and edit layers programmatically. That turns a documented material design into a one-click build: instead of hand-placing a dozen fills and masks, a script rebuilds the whole stack on the active texture set. It is the mechanism behind Smart Material Studio's Painter export.

Scripting builds the editable layer stack - it does not, and cannot, write a .spsm file. The output is a live material in your open project, which you can then save as a smart material by hand.

The core calls

Get the active stack, then insert into it:

import substance_painter.layerstack as ls
import substance_painter.textureset as ts

stack = ts.get_active_stack()
top = ls.InsertPosition.from_textureset_stack(stack)

group = ls.insert_group(top)
fill  = ls.insert_fill(ls.InsertPosition.inside_node(group, ls.NodeStack.Content))

Set channels on a fill layer with a channel type and a colour:

import substance_painter.colormanagement as cm
from substance_painter.textureset import ChannelType

fill.set_source(ChannelType.BaseColor, cm.Color(0.40, 0.18, 0.09))
fill.active_channels = { ChannelType.BaseColor, ChannelType.Roughness, ChannelType.Metallic }

Masks and generators

Add a black mask to a layer, then insert a generator effect into the mask stack:

fill.add_mask(ls.MaskBackground.Black)
import substance_painter.resource as rsc
res = rsc.search("Metal Edge Wear")[0]
ls.insert_generator_effect(
    ls.InsertPosition.inside_node(fill, ls.NodeStack.Mask),
    res.identifier())

Wrap edits in a ScopedModification so they land as a single undo step, and guard resource lookups - a shelf that lacks a named generator returns an empty list, so fall back to a plain mask rather than crashing.

Common traps

Let the tool write it

Smart Material Studio generates this script for you from a visual design, including the alias resolver, the add-channel fallback and the graceful mask degradation. Design the material, tune the channels, and export the .py - then run it in Painter.

Design a material now - free

Pick a theme, tune every channel, preview it in 3D, then export the Painter script.

Open the app

FAQ

Which Painter version has the layerstack API?

Substance 3D Painter 10.0 (the 2024 release) introduced the substance_painter.layerstack module. Target 10.1+ for the most stable behaviour.

Can a script export a .spsm smart material file?

No. Scripting builds an editable layer stack inside an open project. To get a .spsm you save that stack as a smart material by hand in Painter; the format itself is not writable externally.

What happens if a generator resource is missing?

A resource search returns an empty list, so a robust script checks for that and falls back to a plain mask. Smart Material Studio does this automatically so the build never fails outright.