comfyui_pcg docs

Tutorial 4: for-each variations#

Runs the same small modelling chain six times with a different random seed each pass and gathers the results: six different stones from one graph. The loop's iteration number is wired into the Seed inputs, which is how the larger example workflows build their rock libraries.

Six stones produced by one For-Each loop

Load it#

Drag example_workflows/tutorial_04_foreach_variations.json onto the ComfyUI canvas and press Queue. No model files or other custom nodes are needed.

Step by step#

  1. Sphere (PCG) makes a coarse sphere (10 columns, 7 rows). It is the input of the loop.
  2. For-Each Begin (PCG) ("Each stone (count x 6)") is set to Iteration Method By Count with 6 iterations and Method Fetch Input, so every pass receives the whole, uncut sphere on its geo output. Its iteration output counts 0, 1, ... 5.
  3. The loop body:
    • Point Jitter (PCG) (scale 0.5) moves the points randomly. Its rand_seed input is connected to iteration, so the jitter differs per pass.
    • Subdivide (PCG) (depth 3) smooths the jittered sphere; Normal (PCG) (cusp angle 180) gives it smooth point normals.
    • Attribute Noise (PCG) ("Pits along the normal") displaces the points along N with a two-octave Worley F2-F1 noise (amplitude 0.12). Its rand_seed is also wired to iteration.
    • Wrangle (PCG) ("Place and colour") receives iteration on its Seed input too. Inside the code that value is the variable seed:
      @P.x += (seed - 2.5) * 2.3
      @Cd = lerp(set(0.42, 0.40, 0.37), set(0.66, 0.60, 0.52), rand(seed + 0.5))

      Each stone is moved to its own slot along X and gets a colour between two greys.

  4. For-Each End (PCG) (Gather merge) is linked to the Begin node by the loop wire and merges the six results.
  5. Geometry Preview (PCG) (1600 x 800, Studio look) and Viewport (PCG) show the row of stones.

Nodes between Begin and End (everything that depends on the Begin outputs) form the loop body. Nodes outside it, such as the Sphere, run once.

Worth tweaking#

NodeParameterEffect
For-Each BeginiterationsNumber of stones. With more than 6, change the 2.5 in the Wrangle to (iterations - 1) / 2 to keep the row centred.
For-Each Beginuse_single_pass, single_passRun only one iteration, to inspect the body on a single stone.
Point JitterscaleHow irregular the base shape is.
Pits along the normalamplitude, element_size_*, noise_typeSize and depth of the surface pits.
Place and colourcodeAnything that should vary per stone: scale (@P *= 0.7 + 0.6 * rand(seed + 3)), colour, placement.

To scatter the variations, give every stone an integer piece attribute (for example i@class = seed in the Wrangle) and use Copy to Points with Piece Attribute class, as the rock garden does.

See also#