# Need help with loops and Foreach

## Recommended Posts

I'm trying to understand how loops and Foreach works, but need some help on how to iterate over primitives with different offsets and angles in my cuts.

Here is an image I found online to explain what I want to do...or something similar.

1. Use clip to cut a primitive once

2. Loop over the new primitives and cut again, but at different positions and angles.

##### Share on other sites

I'm sure there's better ways, but here's my attempt. Will explain more if I get time later...

• 3

##### Share on other sites

here's my foolin' around...I put a bias in mine so it can break up the biggus first rather than an even chance for each prim..not sure if I've set up my logic right tho....one thing, tried the compiled block and Sort ain't compilable yet

Edited by Noobini
• 4

##### Share on other sites

Thanks Mestela!   I had a look at your Greeble example as well.

It seems to do exactly what I had in mind...it's just for learning so your file is very welcome. You should put it up on your website.

Thanks again.

Edited by Werner

Too much fun!

• 4

##### Share on other sites

Forgot to mention where I got the idea from. There is a beautiful render on the SideFX gall done by Dimitris Ladopoulos, that made me want to explore loops Foreeach.
https://www.sidefx.com/gallery/algorithm/

Thanks again for the great example Mestela. I'm yet to understand how it all works, but I will take my time.

one more for fun.

• 2

##### Share on other sites

Great renders!

I agree, it'll make a nice mid-length article for the wiki, the way I worked it out with lots of mis-steps and incorrect toggles and whatnot was actually a good way to learn, I have a feeling a slightly meandery tutorial that follows the same path would be good. Anyways, some quick notes:

The outer loop is in 'by count' mode in feedback mode on both the begin and end blocks. This means its purely a 'repeat the inner process x times' loop.

Eg, create a sphere, put down a new foreach loop, begin block method to 'fetch feedback', end block iteration to 'by count', gather method to 'feedback each iteration'. By default nothing happens. Put a transform sop inside the loop, with transformX as 1.0. The end result is the sphere moved 10 units, because the same geo is fed back into the loop, and moved 1 unit on x each time.

The inner loop begin block is in 'fetch piece or point' mode, end block in 'by pieces or points', 'merge each iteration', 'primitives'. This means it goes through each primitive, applies the inner loop to just that primitive, and accumulates all the results.

The tricky stuff is if you use a clip, the clip is defined in worldspace, not relative to the prim being clipped. As such, the wrangle does some bookkeeping to work out the center of the prim being processed, and clips there. That looked too regular, so I added some randomness. Then it looked too much in one direction, so I choose a random x or z direction for each prim, and the switch sop flips between them based on that random x or z.

That'll all be easier to explain with gifs.

• 2

##### Share on other sites

Looking at your file, I got the foreach loop part right(thanks to the Masterclass tutorial). I could not find a way to make the clip SOP to work. That is when I got lazy and posted here.
My mind was heading in the right direction, but only starting to learn VEX now. I can see how much more control I will have once I understand it better.

It would be awesome if you placed it on your wiki. The format and explanation is easy to follow, and would help me to understand the VEX part.
Thanks for the incredible resource and effort. Your wiki page is permanently open on my pc.

Now, the idea is to see if I can ad two more clip SOPS at -45 and 45, so that it randomly  switches between the 4 angles.

Thanks again.

• 1

##### Share on other sites

heh, starting to sketch it out...

• 1
• 1

##### Share on other sites

How high did you push the generations in your file? It looks insane at 15 repeats or more...

Thanks Mestela!

##### Share on other sites

Hey @Werner and @mestela

I was surprised to find this thread with the Algorithm stuff I posted a few days ago.

I'm glad it got you interested and thankful for people like mestela!

I am also relatively new, so my method might not be very efficient or elegant, but here it goes:

I tried a couple of way but ended up using polysplit with edge loops in order to have local space rather than world space.

I ended up with 2 variations. 1st one splits each primitive with a random number of edge loops (eg. 0-4). Blue loop splits horizontally, green loop splits vertically. I could only get equally spaced splits so in the 2nd example I tried to make it split each primitive a random number but at a random distance (0.2-0.8) each time.

Hope this helps.

• 3

##### Share on other sites

Very cool! The patterns you get are way more interesting than the once I was getting.

##### Share on other sites

Excellent @Dimitris , saw your renders the other day and wondered how you did it.

Thanks too @mestela for your file and write up on the for loops. Added in some diagonal cuts too to your file. These cuts are also with a for loop that I modified from a file @henderthing was kind enough to let me dig around in a while back. Still trying to get my head around these loops, like a lot of Houdini stuff, it's a bit of a dark art.

• 2

##### Share on other sites

Thanks @Paul Corfield and @mestela

Nice work! I'll soon post the same idea, this time on solids!

Edited by Dimitris
Updated post with better image

##### Share on other sites

Here is the version for solids.

I used connectivity and clipping this time to achieve this. Green loop takes care of X-axis, red takes care of Y and blue the Z-axis.

• 8

##### Share on other sites

Been playing with this ever since prototyping a basic version for a crystal card in "the Last Mimzy"...

It's also fun to build this way in 3D, filling a cube or volume, and flesh out the wireframe that lives on the resulting edges... Will post a version of that when i get some time...
Here are a couple low detail animations from several months ago:

• 4

##### Share on other sites

Dimitris, great to see you here.

I love the renders you did. Great shading and lighting. I can stare at these patterns all day long.

• 4

##### Share on other sites

Added some rotation with a transform just before the clip node in Matt's file. Also added some noise after the for loop to get some variation in the height from the ground.

• 3

##### Share on other sites

That is super cool. Beautiful render Paul!

##### Share on other sites

@Werner Thanks, it came out ok, not bad for what took about 1 minute in Redshift. I'm like you, I never tire of looking at patterns like these. I'll no doubt mess around with this scene quite a bit more to see what else it can do.

## Create an account

Register a new account