# for loop - inheriting v attribute

## Recommended Posts

Hello,

I am practicing for loops in houdini and I am in the middle of a pretty wacky but fairly simple example of just moving points around, setting color, setting velocity etc.

I also have some nested if statements in this for loop just for practice.

My issue is that, when I create new points within the loop,  it's not remembering what the velocity attribute is on each iteration of the loop. It only sets it on the last iteration of the loop.

This is much easier to explain by showing a picture/ and scene file.

Hopefully someone can explain to me whats going on here..

Thank you

##### Share on other sites

A wrangle set to run over points will execute the VEX code once for each point in the input geometry. When using the @attribute syntax, you are setting an attribute on the point that is currently being run over. If you want to set an attribute on a different point, you have to use setpointattrib, which I see you already have commented out. The one you have will do exactly the same as setting @v since the point to set the attribute on is @ptnum. If you want to set the @v attribute on the point you just created, you'll have to store the created point in a variable, and then use that variable in place of @ptnum.

```for(int i=0; i<10; i++){

if(@rand>0.8){
v@pos = point(1, "P", 0);
v@v= v@P-v@pos;
i@point = 1;
}

if(i@point !=1){
v@v=set(0,1,0);
v@Cd=set(1,0,0);
}

if(i@point==1){
v@Cd=set(0,1,0);

v@v= v@P-v@pos;
v@P+=v@v*-0.01;

v@v= v@P-v@pos;
setpointattrib(0, "v", new_point, v@v);
}
}```

There are also some inefficient methods in your code, which aren't necessarily a huge issue for what you're doing here but it's good to always optimise things anyway. You're creating variables by setting attributes, which is slower and leaves you with a bunch of attributes you don't need after that wrangle. The first if statement executes once for every loop even though it will have the same outcome each time. Also, certain attributes like P, Cd, and v are recognised by Houdini and don't need to be specified as vectors. Here's an example of how you could remove these inefficiencies:

```if(@rand > 0.8){
vector pos = point(1, "P", 0);
@v = @P - pos;
@Cd = {0, 1, 0};

for(int i=0; i<10; i++){
vector P = @P + @v * -0.01 * i;
setpointattrib(0, "v", new_point, @v);
}
}
else{
@v = {0, 1, 0};
@Cd = {1, 0, 0};
}```

Hope this helps!

##### Share on other sites

@eimk Thank you ! This clears things up for me a lot. appreciate the examples.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×