Accumulating a Value in a ForEach Loop

   598   9   2
User Avatar
Member
36 posts
Joined: June 2021
Offline
I want to layout a set of geos in a line on (say) the z-axis. The geos have arbitrary length in their z-axis. There may be multiple copies or each geo (think floorboards here).

In the ForEach loop I need to keep track of length of the previously placed goes is so I know to where to translate the current geo.

I have tried many, many options but seems attributes keep getting reset each time through the loop and I cannot get the sum.

Things like this are very common when programming so I'm likely missing something simple.

Thanks!

Greg
User Avatar
Member
385 posts
Joined: April 2017
Offline
It's easier to diagnose a problem if you post a hip file, but it sounds like your for/each block is just misconfigured? Make sure the Block Begin is set to Fetch Feedback, and the Block End is set to Feedback Each Iteration.
MOPs (Motion Operators for Houdini): http://www.motionoperators.com [www.motionoperators.com]
User Avatar
Member
36 posts
Joined: June 2021
Offline
toadstorm
It's easier to diagnose a problem if you post a hip file,

Here's my messed up test scene.. lol

Image Not Found
Edited by GregBollella - Nov. 8, 2024 15:26:48

Attachments:
HP028-ForEachTesting-Nov24.hiplc (203.5 KB)

User Avatar
Member
385 posts
Joined: April 2017
Offline
There's a few things in here that need refactoring. First, when designing a for/each loop like this, you want to make sure that anything that's being controlled in the loop, such as your Switch SOP, is inside the loop. You can use Fetch Input nodes as part of your block to make this happen. Second, you can't have parameter expressions referencing downstream nodes, even if you're in a for/each loop... that'll cause an infinite recursion. You can only lookup nodes that are not a direct dependent of your current node.

You don't really need to bind attributes for this to work, anyways... you just need to know the bounding box maximum of the current geometry in the loop, and the new geometry you want to add for each iteration. I'm attaching an example showing you what I mean.

Attachments:
HP028-ForEachTesting-Nov24_toadstorm.hiplc (176.0 KB)

MOPs (Motion Operators for Houdini): http://www.motionoperators.com [www.motionoperators.com]
User Avatar
Member
72 posts
Joined: Oct. 2018
Offline
Hi !

I think the "match size" node could be usefull here...
Edited by GOgraphR - Nov. 8, 2024 17:00:42

Attachments:
forumtopic98661-1.jpg (116.6 KB)

User Avatar
Member
36 posts
Joined: June 2021
Offline
toadstorm
There's a few things in here that need refactoring. First, when designing a for/each loop like this, you want to make sure that anything that's being controlled in the loop, such as your Switch SOP, is inside the loop. You can use Fetch Input nodes as part of your block to make this happen. Second, you can't have parameter expressions referencing downstream nodes, even if you're in a for/each loop... that'll cause an infinite recursion. You can only lookup nodes that are not a direct dependent of your current node.

You don't really need to bind attributes for this to work, anyways... you just need to know the bounding box maximum of the current geometry in the loop, and the new geometry you want to add for each iteration. I'm attaching an example showing you what I mean.

Thanks. I understand your solution. Would not have thought about just moving the points of the current geo to new x-coord. Seems that I see a lot of this kind of thinking in Houdini. For large, complex geos, though, this might be computationally heavy? Accumulating a simple float offset each iteration seems might be more efficient, no?

Greg
User Avatar
Member
36 posts
Joined: June 2021
Offline
GOgraphR
Hi !

I think the "match size" node could be usefull here...


Thanks. Seems like an elegant solution! I'll try to get it to work with an arbitrary number of input geos.

Greg
User Avatar
Member
385 posts
Joined: April 2017
Offline
GregBollella
Thanks. I understand your solution. Would not have thought about just moving the points of the current geo to new x-coord. Seems that I see a lot of this kind of thinking in Houdini. For large, complex geos, though, this might be computationally heavy? Accumulating a simple float offset each iteration seems might be more efficient, no?
Greg

I don't think you'll likely see a huge computational difference but you could totally benchmark it if you want. A Transform in SOPs context is multiplying every point by the matrix you define in that interface anyways... I wouldn't expect adding a fixed value to P every frame to change that too much.

If you're trying to do this in the most straightforward way possible, I wouldn't even bother with a For/Each loop. Just use the Chain SOP and provide a straight line as the curve input, it'll handle it for you. Just figured this example with VEX and loops would be a good way to show you how Houdini thinks and better answer your original question.
MOPs (Motion Operators for Houdini): http://www.motionoperators.com [www.motionoperators.com]
User Avatar
Member
36 posts
Joined: June 2021
Offline
toadstorm
I don't think you'll likely see a huge computational difference but you could totally benchmark it if you want. A Transform in SOPs context is multiplying every point by the matrix you define in that interface anyways... I wouldn't expect adding a fixed value to P every frame to change that too much.

If you're trying to do this in the most straightforward way possible, I wouldn't even bother with a For/Each loop. Just use the Chain SOP and provide a straight line as the curve input, it'll handle it for you. Just figured this example with VEX and loops would be a good way to show you how Houdini thinks and better answer your original question.

Ah, good point about the Transform SOP moving every point anyway.

Thanks, I will give the Chain SOP a look!

Greg
User Avatar
Member
36 posts
Joined: June 2021
Offline
toadstorm
Just use the Chain SOP and provide a straight line as the curve input, it'll handle it for you.

Wow! The Chain SOP is amazing !

Greg
  • Quick Links