ComfyUI Node Runs on cloud

Layer Order

Reshuffle your stack without rebuilding it

By WASasquatch·Created 3 years ago·Updated 4 days ago· 1,844
Layer Order
  • layers
  • layers
  • index
index0
layer_name
move
target0

Z-order is half of compositing, and most of the time you don't discover you got it wrong until the render is done. Layer Order is the fix-without-rebuilding node: it moves one layer through the stack - front, back, one step either way, or to an exact slot - or sorts the whole stack by name or by the area each layer covers. Hand it the LAYERS document, and it hands back the same document renumbered from 0 at the bottom.

How to drive it

You target the layer with index (0 = bottom of the stack, -1 = front, negative counts down from the top) or layer_name. The move dropdown does the actual work:

  • to front / to back - one layer over or under everything.
  • up one / down one - swap with its neighbour, which is the polite way to nudge a layer past another without guessing positions.
  • to index - drop it at target, counting the same way.
  • The sort moves - by name A→Z or Z→A, or by area small→large or large→small - reorder every layer, not just the one you picked.

A nice detail: index and layer_name are ignored by the sort moves, so you don't need to clear one to use the other. Name matching is case-insensitive, and the layer lookup is fuzzy on the back end - ask for 'sky' and it finds 'Sky Backdrop' if nothing is called exactly Sky.

Outputs you'll actually use

The layers output is the reordered stack, numbered from 0 at the back with no gaps, ready for Create Layered Image or another edit. The index output tells you where the moved layer landed - useful when you're building logic that needs to remember "the plate is now layer 2" for a later edit. After a sort that had nothing to follow, it reports -1, which is its way of saying the move didn't apply.

One genuinely useful pattern: combine a sort by area, largest first, with Layer Select or Layer Remove. Sort the stack so the big background layers sit at the bottom, then drop everything above a threshold - you've just turned "clean up this document" into one deterministic pass instead of a pile of index arithmetic.

Installing it

Layer Order is one node in WAS Node Suite, so install the pack once:

cd ComfyUI/custom_nodes
git clone https://github.com/WASasquatch/was-node-suite-comfyui.git

or search "WAS Node Suite v3" in ComfyUI Manager and restart. It needs ComfyUI 0.14.0+ and Python 3.10+. The pack installs no pip packages in v3 - if an old guide tells you to pip its requirements.txt, it's talking about the v2 era and you can skip it.

Where people trip

The off-by-one trap is the recurring one: this pack numbers 0 from the bottom, so "the last layer" is -1, not a high positive number. If you move to an index past the end, the node treats it as a miss rather than clamping silently - you'll see a clear error naming what the stack holds, which is better than a mysteriously wrong render. And remember the sort moves renumber everything, so any later node addressing a layer by index needs to re-read where things landed (that's what the index output is for). If you address by name instead, reordering never breaks your wiring.

CategoryWAS Suite/Image/Layers

Inputs (5)

NameTypeDefaultDescription
layersLAYERSThe stack to restack. Wire in Add Layer, Layer Edit or anything else answering a LAYERS document.
indexINT0-16384–16384Which layer moves, counting 0 from the back of the stack. -1 is the front layer. Ignored while layer_name names one, and by the sort moves.
layer_nameSTRINGName of the layer to move instead of index. Blank uses index. 'sky' finds a layer called Sky, and finds Sky Backdrop where nothing is called exactly Sky.
moveCOMBO'to front' draws the layer over everything, 'to back' under everything, 'up one' and 'down one' swap it with its neighbour, 'to index' drops it at target, and a sort reorders the whole stack.
targetINT0-16384–16384Where 'to index' puts the layer, counting 0 from the back. -1 is the front. Read by no other move.

Outputs (2)

NameTypeDescription
layersLAYERSThe restacked document, numbered from 0 at the back, for Create Layered Image or another edit.
indexINTWhere that layer landed, counting 0 from the back. -1 after a sort that had no layer to follow.