Python script only works the first time

Hello,

I’m trying to develope a python script for rhino that removes lines based on if they move backwards by tacking all controlpoints for a poly line the distance to the 5 closest points to see if it increases.

This works well the first time I run the script but after that it seams like it gets very confused, not sure why. I thought it might have something to do with the code variables not reseting or that the polyline somehow still had the the old points stored somehow. I’m not sure since I don’t have a good way to test it, but I can see that the point index changes everytime.

If that was not weird enough, my code also indicates a different point from the point that it removes.

Basically running
for M in range(0,NrOfPoints):
RemoveCurveKnot(curve_id,points_to_remove[M])

does not remove the points that I have marked with
AddPoint(control_points[P_index])
points_to_remove.append(P_index)
.

The fix that I have found is to run convert on the polyline, basically redefining a new polyline and runing the script again, but I need to loop the code so this is not a viable option. I have tried running ConvertCurveToPolyline in the python script but it didn’t help.

Any thoughts on this? is it known issue?

Script-404.py (3.2 KB)

/Regards
Robin