Adding Property to Default Path Statements

Is there a better way to add a simple property (such as boolean, string, or integer) to a path statement?
I tried using a Python Script for a robot behavior and my boolean checkbox appears. However, I have to recompile my code after every new path statement. I just wanted to see if I could execute this code on the creation of every new path statement automatically? I tried creating a command, but that didn’t seem to work either. I know my code isn’t the cleanest. I’m just starting out in Python, so any suggestions would be welcome.



from vcScript import *
from vcApplication import *


app = getApplication()
comp = getComponent() 
executor = comp.findBehaviour("Executor") 
routine = executor.Program.MainRoutine
statement = routine.Statements[-1]
currentLen =  len(routine.Statements)-1

if statement.Type == VC_STATEMENT_PTPMOTION: 
  if currentLen != len(routine.Statements):
    statement.createProperty(VC_BOOLEAN, "checkbox")

Hi @clagos

If you want this behaviour to be active all the time, then I recommend implementing it as a command.
The .NET API might have more sophisticated tools for such. Since we are now talking about python, you could try using the vcScope.OnStatementAdded event. When a new statement is added to your robot program, this event will trigger and you could then add a new property to that statement in the event handler.

Example could be something like this:

def my_event_handler(statement):
  if statement.Type == .....
    ....


executor = comp.findBehaviour("Executor") 
routine = executor.Program.MainRoutine
routine.OnStatementAdded = my_event_handler

Thank you Este. I tried adding a “command” under my commands for adding a property. It seems like the python script is not being read when I open up the program editor. I can add the script under the robot as a behavior, but not as a command. I’m not sure how to get the command to execute.

Here is the code I used…

app = getApplication()   

def my_event_handler(statement):
  if statement.Type == VC_STATEMENT_PTPMOTION: 
      statement.createProperty(VC_BOOLEAN, "checkbox")

if app.TeachContext != None and \
            app.TeachContext.ActiveRobot != None:
            robotdata = getSelectedRobotsData()
            executor = robotdata[0]
            routine = executor.Program.MainRoutine
            routine.OnStatementAdded = my_event_handler

With Python Behavior

Without Python Behavior