Getting Previous point in line in a for loop

   726   1   0
User Avatar
Member
1 posts
Joined: Oct. 2020
Offline
Hi,

I am trying to get a line that is only allowed to go up or down.
It might be the ForLoop i wrote that is handled different from what i am expecting:

int numPoints = @numpt; 

for (int i = 0;i < numPoints;i++)
    {
    
    vector currentPos=point(0,"P",i); //get the prev point position
    vector lastPos=point(0,"P",i-1); //get the second to last point position
    
    if (currentPos.y >= lastPos.y)
        {   
    
        vector newPos = (currentPos.x,lastPos.y,currentPos.z);
        
        setpointattrib(0,"P",npoints(0)-1,newPos); //write the newPos
        
        }
    }

i am expecting every point to be under or above the Y value of its previous point, but for some reason nothing happens, only the last point in the line is altered. When i transform the Line i am using to have Y values above 0 only, some y values are set but not in a manner that makes sense.

User Avatar
Member
2041 posts
Joined: Sept. 2015
Offline
Well for one, when you use the setpointattrib you are only ever assigning the last point number (npoints(0)-1) a value.

Second, I'm not sure what you are trying to accomplish, because by 'default' any point will have a Y value that is either above or below the previous point; except in cases where they have the same Y value. But your code doesn't seem to be attempting to address the cases in which the Y value is the same.

Are you simply wanting all points to have the same x and z values, ie. ensure a vertial 'y' line?

Perhaps you could re-state what you are trying to accomplish with a hip file and someone might offer a solution.
Remember too that in reworking your code if your wrangle is running in points mode and your setting point positions, you are going to get odd results since that code will be run for every point concurrently with a number of other points having the same code run on them too.(parallel processing is done with points/primitive modes) - Detail mode might be better depending on what your final attempt is.
Edited by BabaJ - June 8, 2023 20:48:05
  • Quick Links