Look ahead / change motion on part detect

I have a pick and place application that picks parts from a moving conveyor and orients it into a pattern when placed back on the same conveyor. The part I need help with is after they are placed, the robot needs to start heading back to the idle position after the retreat but once another part is detected it needs to change direction and head for that part.

      robot.traceOn (1)
      robot.ActiveTool = 17
      approachLD =  boxh * 1.5
      robot.jointMoveToMtx_ExternalBase (part,Tz = approachLD, Ry = 180)
      robot.linearMoveToMtx_ExternalBase (part,Tz = boxh, Ry = 180)
      
      
      #follow part 
      robot.followNode(part, .01)
      
      #grab part 
      robot.graspComponent(part)
     
      #calc  time/speed
      buffer = comp.getProperty('Y_Buffer')
      offsetY= pos
      offsetX = -abs((offsetY  / max_speed) * cnvspd.Value + buffer.Value)
      retreatx = (approachLD / max_speed) * cnvspd.Value*-1
      
     
      #release part
      part = robot.GraspContainer.Components[0]
      robot.linearMoveRel(Tx=offsetX,Ty=offsetY,Rz=rot)
      robot.releaseComponent(comp) 
      
      #follow on retreat
      retreat = boxh * -1
      
      #robot.followNode(part, .01)
      robot.linearMoveRel(Tz=float(retreat), Tx=retreatx, Rz=-rot)
      picked.append(part)
      
      robot.traceOff (1)
      picked = filter(lambda x: x in comp.ChildComponents, picked)
      parts = filter(lambda x: x not in picked, parts)
      
      #robot.callSubRoutine ("idlePos")