vcMotionPath OnPhysicalTransition event not firing

I have this script added in a generic conveyor to listen to events when products are added to the conveyor path:

from vcScript import *

comp = getComponent()

def event_func( container, component, arrive_leave ):
  print("Transition fired, component:", component.Name, "| arrive_leave:", arrive_leave)

def OnRun():
  path = comp.findBehaviour("Path")

  if not path:
    print("Path behaviour not found")
    return
  
  print("Path behaviour found", path)
  path.OnPhysicalTransition = event_func

Products are added to the conveyor, but the event never fires - why? Thanks!

Since the product comes in from the To Conveyor Process, you should probably use OnTransition event instead. It’s inherited from class vcContainer.
Based on the OnPhysicalTransition event’s description, it triggers at product’s leading edge, which does not hit the path’s start in this layout.

1 Like

@KustiH thanks, OnTransition works as expected.

It only works though if I add the infinite while True loop into OnRun. Is this the right way to set up event listeners in general?

from vcScript import *

comp = getComponent()

def event_func( component, arrive_leave ):
  print("Transition fired, component:", component.Name, "| arrived:", arrive_leave)

def OnRun():
  path = comp.findBehaviour("Path")

  path.OnTransition = event_func

  while True:
    delay(0.1)

Hello,
That’s related to Python’s “garbage collection”, i.e. deallocating memory for objects that are no longer needed. Now that variable “path” is declared only locally inside OnRun, the object as well as its event handler are cleaned out when the OnRun ends.

Mostly component scripts have the event handler assigned in the global scope (when the script is compiled by user, or component is loaded to 3D world). However, if there’s a need to set the event handler inside some scope, you can do this to “keep the event handler alive”:

path = None

def OnRun():
  global path
  path = comp.findBehaviour("Path")
  path.OnTransition = event_func
2 Likes