|On this page|
This node forms a block with a Begin Context Options Block node connected through its third input. This node loops over the nodes wired in between the start node and this node’s third input, cooking them multiple times based on the Iteration method.
(The start node is a context options node because the for-each block uses context option variables to hold the iteration counters).
This has several uses:
Generating a certain number of prims, attributes, edits, or whatever you need, automatically.
Applying a series of changes to a bunch of primitives.
Applying a series of changes to all variant sets on a bunch of primitives, or to all variants.
In the LOP network, press ⇥ Tab and choose For Each.
This puts down a block starting with a Begin Context Options Block node and ending with this node.
Wire the incoming node chain into the
foreach_endnode’s first input.
If you are only adding to the stage (for example, creating new prims), you don’t need to wire anything into the
foreach_begin. However, if you are modifying existing prims, you should also wire the incoming node chain into the
foreach_beginnode’s input, so it’s available in the loop.
Select For Each node at the end of the block (
In the node’s parameters, choose the Iteration method.
In between the
foreach_beginnode and the
foreach_endnode’s third input, wire in the LOP nodes you want to loop over.
The For Each node cooks the block zero or more times (according to the Iteration method). The results are flattened and added as a sublayer over the incoming stage from the first input.
On the nodes inside the block, you can access context options containing the current iteration number (
ITERATION) and the total number of iterations (
Some iteration methods also set an
ITERATIONVALUE variable. The contents of the variable depends on the iteration method. For example, when iterating over primitives, the
ITERATIONVALUE contains the scene graph path of the current prim. See the help for the Iteration method parameter below.
You can rename the looping variables using parameters on the For Each node (see below). If you are nesting for-each loops, you should rename the variables so each loop level has different variable names, so you can tell them apart.
For example, if you had a "for each variant set" loop and inside that loop a "for each variant" loop, you might rename the outer variables to
NUM_VARSETS, and the inner variables to
To get the value of a context option in an expression inside the loop, use
@name, for example:
or use the contextoption expression function:
For example, if you want to create 100 lights inside the
/lights prim, named
/lights/light_99, you would put a Light node in the loop block and set its Primitive path to the following expression:
(Remember in string parameters, you must surround expression code with backticks.)
To get the value of a context option in Python inside the loop, use the hou.contextOption() function.
You may find it useful to use Python’s extensive library string- and path-manipulation function to pull information out of the
ITERATIONVALUE. You can create new context options using hou.setContextOption().
For example, at the start of a "for each primitive" loop, you could insert a Python Script LOP that makes new context option variables containing the parent path and prim name, extracted from the full primitive path.
# Get the path of the current primitive from the context option full_prim_path = hou.contextOption("ITERATIONVALUE") # Find the index of the last slash in the path string last_slash = full_prim_path.rfind("/") # Make a new "PARENTPATH" context option containing everything up # to the last slash hou.setContextOption("PARENTPATH", full_prim_path[:last_slash]) # Make a "PRIMNAME" context option containing everything after the # last slash hou.setContextOption("PRIMNAME", full_prim_path[last_slash + 1:])
Then you could use
@PRIMNAME in expressions on the other nodes in the loop.
Leave the Perform layer break parameter on in the Begin Context Options Block node at the start of the loop block.
Without this option enabled, any nodes above the Begin Context Options Block node would be included in each loop and uselessly combined with every other iteration (uselessly because the data will be the same on each cook).
Because this block uses context options to hold the iteration variables, it works quite differently from For-Each loops in other network types.
To edit every variant in a certain variant set on selected prims:
Make an outer "for each prim" loop over the prims. Set the Primitives parameter to select the prims you want to edit. Change the name of the
ITERATIONVALUEvariable in this loop to
Make an inner "for each variant" loop. Change the name of the
ITERATIONVALUEvariable in this loop to
Now in the inner loop you can supply both the current primitive’s scene graph path as
@PRIMPATHand the current variant’s name as
@VARIANTNAMEin nodes to edit the variant.
Connect the incoming stage to this input. The node flattens the results of looping over the nodes connected to the third input, and then overlays it as a new layer on this stage in the output.
The Iteration method has options to loop over prims/variant sets/variants from this input instead of from the first input. This lets you drive the iteration using a separate stage other than the stage being modified.
The data in this input is never included in this node’s output. It may be used as to drive iteration, depending on the Iteration method setting.
This input is re-cooked for each iteration. The node flattens together the layers generated on each iteration, and composes the flattened result onto the stage from to the first input.
How this node loops/what it loops over.
For Number of Iterations
Loops a set number of times. Note that you can drive the number of iterations dynamically with an expression in the Iterations parameter.
This method sets
ITERATIONVALUE to the same value as
ITERATION (the current loop count, starting from 0).
For Each Primitive in First Input
Loops over each primitive selected by the primitive pattern in Primitives (from the first input).
This method sets
ITERATIONVALUE to the full path of the current prim.
This option is useful for performing edits on a set of primitives.
For Each Variant Set in First Input
Loops over each variant set on a certain prim (or prims). The primitive pattern in Primitives selects the prim(s) holding the variant sets (from the first input).
This method sets
ITERATIONVALUE to the name of the current variant set.
You can use this add variants to or remove variants from variant sets. You could also nest another For-Each loop inside using "For each variant in first input" to loop over each variant in each variant set.
For Each Variant in First Input
Loops over each variant in a named variant set on a certain prim (or prims). The primitive pattern in Primitives selects the prim(s) holding the variant sets (from the first input).
This method sets
ITERATIONVALUE to the name of the current variant.
This method does not change the variant selection.
For Each String in Parameter
Loops over each string provided in the Iterate Over Strings parameter. The number of iterations will be defined by the number of separate strings specified in the parameter.
This method sets
ITERATIONVALUE to the current string from the parameter.
You can also choose to drive iteration using data from the second input instead of the first input. If for some reason the things you want to loop over are in another node chain, you can connect it to the second input and use these options. Note that data from the second input is never included in this node’s output. It is only used to drive the iterations, if you choose one of the "in second input" options.
When Iteration method is "For Number of Iterations", this is the number of loops. (You can use an expression in this parameter to drive the number of loops based on something else.)
When Iteration method is "For each primitive", "For each variant set", or "For each variant", the prims to look at. You can drag primitives from the scene graph tree pane into this textbox to add their paths, or click the select button beside the text box to select the primitives in the viewer. You can also use primitive patterns for advanced matching, including matching all prims in a collection (using
When Iteration method is "For each variant", the name of the variant set to loop over.
Iterate Over Strings
When Iteration method is "For each string in parameter", the list of strings to loop over. Individual strings are separated by white space. Quotes can be used around a string to incorporate white space into that value.
Flatten Iterations Into First Input Layer
When this is off (the default), the edits created in the loop are added to the first input as a new sublayer. When this is on, the edited created in the loop are flattened onto the first input’s active layer (does not create a new layer).
Set Last Modified Primitives from Iteration input
Iteration Option Name
The name of the context option variable this node will create holding the current loop number (starting from 0). You can rename this for convenience or to make nested loops use unique names.
Iteration Count Option Name
The name of the context option variable this node will create holding the total number of loops. You can rename this for convenience or to make nested loops use unique names.
Value Option Name
The name of the context option variable this node will create holding Iteration method-specific information (the current iteration for "Number of iterations", the current prim path for "For each primitive", the variant set name for "For each variant set", or the variant name for "For each variant"). You can rename this for convenience or to make nested loops use unique names.
This lets you optionally restrict the number of iterations the node would normally perform to start and end within a certain range. This can be useful for debugging, to observe the effect of just a "slice" of the overall result.
Run all available iterations (the default, normal operation).
Only run one iteration, specified in the First iteration parameter. The loop numbers start at
Range of Iterations
Only run a range of iterations, from the First iteration until the Last iteration (inclusive). For example, if you set Iteration method to "For number of iterations" and set the number to 5, you would get iterations 0, 1, 2, 3, and 4. If you then set Iteration range to "Range", First iteration to 1, and Last iteration to 3, you would only get iterations 1, 2, and 3.
When Iteration range is "Single", only run this iteration number. When Iteration range is "Range", only run a subset of iterations starting at this number. The first iteration is numbered 0.
When Iteration range is "Range", only run a subset of iterations including but ending at this number. The first iteration is numbered 0.