Jump to content
Sign in to follow this  
logix1390

foreach- index vs iteration of all points

Recommended Posts

Hello,

I have a few questions about the foreach in vex that is probably better explained with an actual example I'm working on. 

First Example:
i[]@pts = neighbours(0,3); //get array/list of point numbers
foreach (int pt; @pts ){
    v@pos=point(0, "P", pt); //gets the last index point position.  Point 4 in this case
}

Second Example:

i[]@pts = neighbours(0,3); //get array/list of point numbers
foreach (int pt; @pts ){

    v@y= set(0,1,0); //value
   
setpointattrib(0, "P", pt, @y, "add"); //Setting the @y value on each point inside of the array. 
                                                           //It will add the @y value every iteration . In this case, the iterations are the number of points on the grid (25 points) ??
                                                          //Therefore we are adding the @y value on each point in the array 25 times ??

    
    }

 

In the First example , I use the point function within the foreach loop and it returns the point position of the last index in the array. That makes sense. 

In the Second example, I am trying to set a value on the point positions inside the array using a setpointattrib function but i can see that it is actually iterating over all my points in the grid (25 points) which means it will be adding (0,1,0) 25 times to each point number in the array. 

It seems like the foreach is working differently in both examples which is confusing. I am aware that the wrangle sop iterates over all the points. I get it. 

So why is it that in the first example the foreach is iterating over the points in the array ( which is what I expected) and the second example it is iterating over ALL the points and adding that value 25 times instead of just 4. 

Hopefully i'm explaining this correctly. I will attach my scene file for anyone that wants to take a look

Thank you

 

foreach_iteration_question.hipnc

Share this post


Link to post
Share on other sites

In a attribute wrangle running over points you iterate over all points. What means one iteration for every point. So for every point you get the neighbours of point number 3 and add 1 to the y position. In your first example, you iterate over all 25 points, get the number of connected points of point id 3 and set a variable namend "pos" to the position of the last point id. This is done 25 times as well because it runs over points. If you want to execute it only once you could set the mode of the attribute wrangle to "Detail" so it is executed only once.

Share this post


Link to post
Share on other sites

@haggi I understand that its iterating over every point. I guess I am a bit confused on the fact that the setpointattrib function is actually returning a value based on the accumulation of ALL the points it was iterating over rather than just the amount of points in the array that I specified. 

The point function seems to be doing what I was expecting and giving me the  last point in the array that I specified. Based on the logic of it running over every point, wouldn't the point function return the last point on the grid? The setpointattrib function doesn't seem to care about the index in the array and accumulates the value based on all the points but the point function seems to respect the index in the array. 

I realize I probably sound a bit confusing . I apologize

Thanks for the reply

 

Share this post


Link to post
Share on other sites

@haggi Your right about setting it to detail. That works. I'm still a bit confused about this when its not set to detail and set to points. 

 

Thanks

Share this post


Link to post
Share on other sites

Maybe it helps if you try to follow the instructions step by step to understand the process.

  1. There is an invisible outer loop which loops over every point is the mode is set to points. That means the next instructions are called for every point in your case 25 times.
  2. Get the indices of the connected points here you have 3 indices.
  3. For every index, add 1 to y.
  4. Go to the next point, what means go to step 2. and 3 and so on until the last point is done.

Since you have three indices here [2,4,8], you add 25 times 1 to the y position of these points what results in a y value of 25 for these points.

Share this post


Link to post
Share on other sites
12 hours ago, logix1390 said:

@haggi I understand that its iterating over every point. I guess I am a bit confused on the fact that the setpointattrib function is actually returning a value based on the accumulation of ALL the points it was iterating over rather than just the amount of points in the array that I specified. 

The point function seems to be doing what I was expecting and giving me the  last point in the array that I specified. Based on the logic of it running over every point, wouldn't the point function return the last point on the grid? The setpointattrib function doesn't seem to care about the index in the array and accumulates the value based on all the points but the point function seems to respect the index in the array. 

I realize I probably sound a bit confusing . I apologize

Thanks for the reply

 

It seems you are mixing some principles. First of all the wrangle node iterates over all elements. The setpointattib() function does exactly what you tell it to do. It adds 1 for every point in your array, but it does it 25 times because the wrangle node is exectued 25 times. The point() function gives you exactly what you ask it to give you. Since you loop over your three connected points, and write the result in an attribute, is always contains the last position of the array. This is executed 25 times. And you always ask for the same point in the point function v@pos=point(0, "P", pt);

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
Sign in to follow this  

×