Houdini: Creating a counter while iterating over groups in a foreach SOP

Sometimes, while iterating over groups you’ll need a counter of some sort inside the foreach SOP. To do this you can set the foreach SOP to Each Number, then set the numberrange2 value to

argc(primgroupmask(opinputpath(".", 0), "group_mask*"))

…inside the foreach SOP, add a delete node and in the groupmask use

arg(primgroupmask(opinputpath(".",0), "group_mask*"), stamp("..", "FORVALUE", 0))

…and set operation to Delete non selected.

  • primgroupmask will return a space separated list of groups that meet the group mask specified.
  • opinputpath just gives the path to the previous noed (usually “each1” in a foreach).
  • stamp returns the current index, which, combined with the primgroupmask list is fed to…
  • arg which looks up the correct mask for this iteration.

This will then act the same as setting the foreach SOP to iterate over groups called “group_mask*”.

To access the current value of the iterator, use

stamp("..", "FORVALUE",0))

(obviously, this is for if you’re using primitive groups).