how to use the new foreach with partition...

   6779   4   2
User Avatar
Member
2 posts
Joined: Feb. 2016
Offline
hi,
I am having trouble following tutorial by Peter Quint Basics: ForEach III (https://vimeo.com/7361320 [vimeo.com] ) with the new foreach in houdini 15.

In the tutorial he explains how to randomly select primitive after copying boxes on a grid and then using connectivity with partition sop. However, in Houdini 15 the new foreach doesn't understand the groups created by Partition (at least I don't understand how to). In H15 Masterclass | Loops (https://vimeo.com/142534639) [vimeo.com] towards the end of video its been explained how to get iteration from detail attribute…. But I cannot figure out how to use this as stamp variable here. I am attaching file. Please let me know how to go about it.

Thanks.

Attachments:
snap.jpg (70.7 KB)
forEach.hiplc (63.8 KB)

User Avatar
Member
1737 posts
Joined: May 2006
Offline
Here's one way. I avoid hscript expressions whenever I can, so took a few goes at this until it was attribute+vex based, which makes me happy.

Short answer: use this in a prim wrangle before the extrude, and set the extrude to use the group ‘extrudeme’:

int randface = floor(rand(@class)*@numprim);

if (@primnum == randface) {
@group_extrudeme = 1;
}

Long answer:

First get rid of the partition node and loop metadata, you don't need them.

Next, what we want inside the loop is for each cube, randomly choose 1 of the 6 faces on the cube, group it, and extrude that group.

Its simple enough to make groups within a wrangle, you just assign the geometry the virtual attribute @group_mygroupname, and make it 1 if you want it in the group, 0 if you don't.

To get random number, use rand( something). This returns a results between 0 and 1, so to get it between 0 and 6, just multiply it by 6. Or better, use @numprim, which will be the total number of primitives in the shape.

rand ( something ) * @numprim


But what to use for ‘something’? If we make it, say the primitive id, that's no good, as it'll be equally random for each cube (ie, rand(3) if we give it the 3rd prim on cube 1, will give the same results as rand(3) on the 20th cube).

Instead, we need something that is different for each cube, but the same within the cube. We already have that with the @class attribute. So the random prim chooser now looks like

rand ( @class ) * @numprim


To use this to make a group, we'll compare the current primitive to this random number. If they match, add it to the group. If not, ignore it. Some extra stuff required here because @primnum is an integer, and the random number is a float, so we have to round it down to an integer too:

int randface = floor(rand(@class)*@numprim);

if (@primnum == randface) {
@group_extrudeme = 1;
}

So with that, you can now just set the extrude node to use ‘extrudeme’ as its group, and we're done.

Hip attached…

Attachments:
forEach_rand.hipnc (74.1 KB)

http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
53 posts
Joined: Aug. 2009
Offline
This is a very old tutorial. The effect as mestela has pointed out is more easily achieved in other ways, but I used it back then as a way of illustrating the for each node. It is worth understanding looping, as sometimes there is no other way to achieve the effect.

There is an excellent tutorial on looping from Sidefx:

https://vimeo.com/142494487 [vimeo.com]

The direct answer is that your loop must be using the fetch piece method. Then use the attribute your partition node created, usually ‘class’ as the attribute. Each box is then passed through the loop in sequence.
Peter Quint
User Avatar
Member
2 posts
Joined: Feb. 2016
Offline
thanks mestela, your solution is working. got a lot of points cleared in the process of understanding what you achieved. thanks.

also thanks peter for replying. However, what you told is difficult for me to comprehend at this stage perhaps. Tried attribcreate as suggested in the video you mentioned, and then used the attribute at Block-end. But how to randomly select primitive inside for-each. I tried $PR==floor(rand($PRIMNUM)*$NPR)). It doesn't work with limited knowledge of mine. Attaching file… plz post any solution.

Attachments:
forEach01.hiplc (67.5 KB)

User Avatar
Member
22 posts
Joined: Sept. 2015
Offline
Thanks mestela, you're explanations of vex on your cgwiki and the forums are great!
  • Quick Links