Houdini 19.5 Geometry

Looping in geometry networks

On this page
Since 15.0

Overview

Typically Houdini processes the nodes in a geometry network from top to bottom, feeding the output of each node into the input of the next node down to the node in the network that has an enabled display or render flag. However, there are times when it’s useful to run the same chain of nodes multiple times in a loop.

There are three main uses for looping:

You can use the For-Each and For-Loop menu items located in the Network Editor ▸ ⇥ Tab menu to set up Block Begin and Block End nodes that will loop any nodes placed between them in their node chain.

Repetition

  1. In the Network Editor, press ⇥ Tab to open the Tab menu, select For-Loop with Feedback from the menu, and then click in the Network Editor to place the new nodes.

    This menu item creates a Block Begin SOP node and a Block End SOP node that are wired together. The Block Begin SOP also has its Method parameter set to Fetch feedback. This means that after the first loop, it will start each loop with the output of the previous loop.

  2. Select the Block End SOP node and set its the Max Iterations parameter to the number of times you want the loop to run.

    Warning

    It is possible to unintentionally create a loop that operates on all geometry and creates new geometry in each loop, exponentially increasing the geometry size and cook time. We recommend that you start with just 1 or 2 iterations at first until you know the loop is working the way you want.

  3. Connect the geometry you want to process to the input of the Block Begin SOP node.

  4. Connect the nodes to process the geometry between the Block Begin SOP and the Block End SOP nodes.

Tip

If Houdini does get stuck in a long cook because of your loop, you can press ⎋ Esc to cancel the cook.

In the example below, the For-Loop extrudes and scales every face of the box for two iterations.

Tip

The point of this type of loop is that the output of one loop becomes the input for the next loop, so any geometry you create in the loop will accumulate. If what you actually want to do is apply the same nodes once to multiple copies, then use the Copy to Points SOP node instead.

Piecewise (For-Each) looping

A piece is usually a series of primitives that have the piece or name attribute. Several Houdini tools (for example, like the Shatter shelf tool) automatically create this type of partition attribute for you.

You can specify an attribute name other than piece with the Piece Attribute parameter on the Block End SOP node. If no piece attribute exists, the block will loop over each point or primitive on the input geometry.

Warning

Merging points and primitives at the end of a For-Each loop operation will collate the points and primitives into contiguous ranges per piece. This may change the original point and primitive order.

  1. In the Network Editor, press ⇥ Tab to open the Tab menu, select For-Each Primitive from the menu, and then click in the Network Editor to place the new nodes.

    This menu item creates a Block Begin SOP node and a Block End SOP node that are wired together. The Block Begin SOP also has its Method parameter set to Extract Piece or Point. This means that it will start each loop with a different piece of the input.

  2. Select the Block End SOP node and then set the Piece Elements parameter to the type of pieces you are iterating over (points or primitives). This will usually be primitives.

Note

If you do not set the Max Iterations parameter on the Block End SOP node, then the For-Each loop will loop over each piece in the input.

In the example below, the For-Each loop loops over the pieces of a shattered torus and applies the Mountain SOP to each individual piece.

Multiple inputs

A loop can have more than one Block Begin SOP node. For example, one branch of a network could get its input piecewise and another branch could be simple repetition accumulating the result of using the pieces.

The classic example of this type of loop is making swiss cheese, where simple repetition is used to accumulate the results of processing one input geometry (the cheese block) by looping over pieces from a second input geometry (the air bubbles).

  1. In the Network Editor, press ⇥ Tab to open the Tab menu, select For-Loop with Feedback from the menu, and then click in the Network Editor to place the new nodes.

    This loop branch will do the work of accumulating the changes to the cheese block.

  2. Create another Block Begin SOP node.

    • Set its Method parameter to Extract Piece or Primitive.

    • Set its Block Path parameter to the Block End of the existing loop.

      You can use the node chooser button next to the parameter, or drag the Block End from the Network Editor onto the parameter’s field in the Parameter Editor.

    This loop branch will do the work of fetching each bubble from the bubble geometry.

In the example below, swiss cheese is created by the two loop branches in the network.

1

The network begins with a box representing the cheese block, and then it is turned into a VDB.

2

Some points are then scattered in the box and spheres are copied onto them to represent the air bubbles.

3

The first Block Begin is a simple repetition that takes the VDB cheese block as input and accumulates the changes inside the loop.

4

The second Block Begin uses the Extract Piece method which takes the air bubbles as input and loops over each primitive sphere, and downstream the spheres are turned into VDBs.

The cheese block VDB is wired into the air bubbles VDB’s second input (see 5) to make it copy the resolution settings from the cheese block.

3

Inside the loop the VDB Combine subtracts the VDB sphere from the cheese block.

How to get information about the current loop

Sometimes you need to use the current loop or the current piece number in an expression on one of the nodes in the same loop. You can get this information with a Block Begin SOP node that uses the Fetch Metadata method.

  1. Set up a looping block using the instructions above.

  2. Select the Block Begin SOP node, and then in the Parameter Editor click Create Meta Import Node.

    This button adds a second Block Begin SOP node to the side of the existing one. This node is set up to generate an empty geometry with some detail attributes.

    The detail attributes are as follows:

    numiterations

    The expected total number of iterations, taking into account the Max Iterations and Single Pass parameters on the Block End SOP node of the loop.

    iteration

    The current iteration number, always starting at 0 and increasing by 1 each loop.

    value

    In piecewise loops, this is the current value of the attribute. For example, the piece integer or name string, or if there is no attribute, the current point or primitive number.

    In simple repetition loops, this is a floating point value starting at the Block End SOP’s Start Value and increasing by the specified Increment each loop.

    ivalue

    In simple repetition, this is an integer version of value. This can be useful if the value is naturally an integer (for example, starts at 1 and increments by 2) and/or if values are over 24 million (where floating point numbers lose precision).

  3. To grab the value of these attributes in a node inside the loop, do the following:

Stop condition

The Iterations and Max Iterations parameters on the Block End SOP node are computed before the loop starts so that they can be used to stop a loop early using results from the loop.

Making the Stop Condition parameter on the Block End SOP node evaluate to 1 before an iteration will stop the loop. This lets you write an expression that tests certain conditions. For example, you could stop looping if the number of polygons in a feedback loop passes a certain threshold.

Notes

  • This condition is evaluated before the loop is processed.

  • This is not yet supported in compiled blocks.

Debugging and testing loops

  • You can step through the iterations of the loop by setting the Max iterations parameter on the Block End SOP node. For example, start it at 1 and increase it to see the results of each loop.

  • In piecewise loops, you can see the output of an individual piece/iteration using the Single Pass parameter on the Block End SOP node.

Tips

  • You cannot specify pieces using groups with the Block Begin SOP node. Since groups are not necessarily disjointed, it is very slow to translate them into pieces. If your geometry uses groups to specify their pieces, then you can create an attribute from the groups using the Name SOP node’s Name from Groups parameter and then split by the attribute you created.

  • The color of the blob surrounding the nodes in a loop comes from the color of the Block Begin SOP and Block End SOP nodes. To change the color of the blob, just change the color of one of these nodes.

  • Currently only the nodes wired between the Block Begin SOP and Block End SOP nodes are included in the blob. Unfortunately, the blob does wrap around all the nodes that are included in the loop because of channel reference dependencies. This is a limitation of the current implementation.

See also

Geometry

Understanding

Modeling

Terrain

Fracturing

Next steps

Guru level