Strange FOR loop results in Attribute Wrangle

   2029   2   1
User Avatar
Member
12 posts
Joined: May 2017
Offline
I've been using the forum for some time but this is my first post - so hello to everybody.

I'm stuck with FOR loop in Attribute Wrangle.
I cannot understand what's happening that causes it to produce unwanted results.

Intro:
I got points and I want to rearrange their numbers to fit certain pattern so I can create a curve which shape will match the pattern.

Current state:
Based on pattern definition I created additional point attributes that should be the base for reordering point numbers.

What was supposed to be quick debug check made me stuck for two days now.
1. I want the code to go through all points in the geometry and check value of a specific attribute (“LeftSide”).
2. If the value of the attribute matches certain value new point should be created.

Code:

int ParamValue = 0;
int pointCount = @numpt;

for (int i=0; i<pointCount ;i++)
{
    ParamValue = pointattrib(geoself(),"LeftSide",i,1);
    printf(itoa(ParamValue));
    
    if(ParamValue == 2)
    {
        addpoint(0,{0,0,5});
        printf("added new point");
    }
}

What's weird to me:
Debug results printed to the console seems to be ok. However it looks like the new points were created for all the points - I start with 29 points and finish with 58.
No errors, no warnings and I'm bit lost. I attached screen of the geometry after the code run.
I'd appreciate any advice on finding out what I am doing wrong and/or misunderstood.


Staszek

Attachments:
FOR_loop.jpg (469.9 KB)

User Avatar
Member
2034 posts
Joined: Sept. 2015
Offline
What's happening is that your wrangle is set to run over points,

so your loop is getting executed for every pre-existing point.

And since your condition is set to ParamValue == 2 and you have 29 points to begin with;

Your adding 29 new points.

To get what you want without this unwanted duplication set your wrangle to ‘detail’ instead of running over points.

I know the printf function is not printing out this 29 extra times, but it's just one of those behaviors of the wrangle compiling that ‘defaults’ to only printing once(because it's going to be the same value for the same point 29 times).

This print behavior can be convenient in other situations in which you might just want to see ‘one same result’ instead of potentially hundreds/thousands of ‘prints’ when running over points.
Edited by BabaJ - March 8, 2019 16:45:45
User Avatar
Member
12 posts
Joined: May 2017
Offline
Thanks! That's was it.
Staszek
  • Quick Links