Jump to content
willrmiller

Switches and loops

Recommended Posts

Hello,

I have a situation where I need to apply the same operations to several discrete pieces of geometry which will later be exported as separate files.  To save myself some network duplication, I thought I might be able to collect all of these pieces into a switch and iterate over them in a for-loop.  I can't figure out how exactly to make this work.  Is there a better way?  (image illustrates what I'm going for)

Thanks for the help!

image.png.fe27170056d1cd3d2a88c43728c6f7f0.png

Share this post


Link to post
Share on other sites

the way i did it in the past was to give each piece of geometry a discrete attribute (name, number, etc), collect them in a merge SOP and then run my loop with that attribute.
if you have a lot of inputs, you might want to use python to automate that primwrangle creation

Share this post


Link to post
Share on other sites

if you want to process them separately and save as different files anyway, you can just use Wedge SOP and control switch value with $WEDGENUM variable, then have any sops after switch to process your geo and ROP Geometry Output (referenced in wedge driver) that will save your geo with path varying using $WEDGE variable  

https://www.sidefx.com/docs/houdini/nodes/out/wedge.html

Share this post


Link to post
Share on other sites

Thank you so much for the help. I tried both of these methods (merge + named primitives and using a wedge) and they both work.  My specific situation called for a different solution, because I needed to force a heightfield erode recalculation and export the resulting maps in each iteration.  To do this, I wrote a little Python in the session module:

def renderTerrain():
    for i in range(0, 13):
        hou.node("obj/Terrain/Controller").parm("shape_index").set(i)
        hou.node("obj/Terrain/heightfield_erode1").parm("resimulate").pressButton()
        hou.node("obj/Terrain/heightfield_output1").parm("execute").pressButton()

Here, the Controller node's shape_index parameter is referenced in the switch.  I could improve this further by replacing the hard-coded loop range with a count fetched from the switch.

EDIT: @Noobini your solution works well too.  Very simple!

Edited by willrmiller

Share this post


Link to post
Share on other sites

@Noobini in your example you can also use opninputs("../switch1") for Iterations in foreach_end1 to iterate over all switch inputs automatically.

Share this post


Link to post
Share on other sites

Depending on what you are doing in the loop.
if the logic is going to be "compilable",

You can also look into the "Invoke" SOP ;)

that is a way of reusing a part of network, with a different input (generating a different output)
but it only works together with a compiled block as a reference.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×