ghPython stop update function

Hi all!

I have a surface and multiple points that goes down by a move function.
I want to stop them whenever the subtraction of the last two points reaches a value of 0.005.
How to stop the move and update each of the points in the list individually?

Code here
import Rhino.Geometry as rg
import ghpythonlib.treehelpers as th
import random

# CREATING A CLASS CALLED PARTICLE
class Particle:

    def __init__(self, point, surface):

        self.pt = point
        self.srf = surface

        self.vec = rg.Vector3d.Unset
        self.plane = rg.Plane.Unset

        # A LIST CALLED HISTORY (starts with point self.pt)
        self.history = [self.pt]

    # A METHOD CALLED UPDATE (updates the position of the particle)
    def update(self):
        
        self.move()
        self.history.append(self.pt)

    # A NEW METHOD CALLED MOVE
    def move(self):

        #unpacking the returning values from the function
        success, U, V = self.srf.ClosestPoint(self.pt)

        if success:
            self.pt = self.srf.PointAt(U, V)
            self.plane = self.srf.FrameAt(U, V)[1]

            worldZ = rg.Vector3d.ZAxis
            localZ = self.plane.ZAxis

            vec1 = rg.Vector3d.CrossProduct( worldZ, localZ)
            vec2 = rg.Vector3d.CrossProduct( vec1, localZ)

            self.vec = vec2
            self.vec.Unitize()

            self.vec *= step

     self.pt += self.vec

allParticles = []
for pt in pts:

    p = Particle(pt, srf)
    allParticles.append(p)

allHistory = []
for i in range(count):
    for p in allParticles:
        p.update()
        allHistory.append(p.history)

a = th.list_to_tree(allHistory)

lastpointsZ = []
for p in allParticles:
    lastpointsZ.append(p.history[-1].Z)

secondlastpointsZ = []
for p in allParticles:
    secondlastpointsZ.append(p.history[-2].Z)

# SUBTRACT THE SECOND TO LAST POINT FROM THE LAST POINT
resultingZ = []
for i in range(len(lastpointsZ)):
    resultingZ.append(abs(secondlastpointsZ[i]) - abs(lastpointsZ[i]))
    
print lastpointsZ
print secondlastpointsZ

print resultingZ

Thanks!

extend your class a little

class Particle:
    @property
    def prev(self):
        if len(self.history) == 1:
            return None
        return self.history[-2]

then you can replace your main body with:

for p in allParticles:
    while p.prev is none or abs(p.pt.Z - p.prev.Z) > 0.005:
        p.update()
        allHistory.append(p.history)

lastpointsZ = [p.pt.Z for p in allParticles]
secondlastpointsZ = [p.prev.Z for p in allParticles]
resultingZ = [abs(prev) - abs(last) for prev, last in zip(secondlastpointsZ, lastpointsZ)]

@James_Parrott Thanks for your reply. But the while loop is freezing GH. Maybe is causing an infinite loop?

Ah sorry. While loops are always susceptible to that, even when they do converge.

Add a simple counter variable and an iterations limit condition, or make it a for loop instead and use the negation of the while loop condition to break it.