Events in python add ons

Hey all I’m stumped

I was trying to write an add on with some functionality that requires subscribing to the

OnPostExecuteStatement

of a robots executor.
I’m subscribing like so

executor.OnPostExecuteStatement = robot_statement_finished

executor being the variable where I stored the robots executor.
I defined the robot_statement_finished function with a simple print statement.
basically anytime the robot finishes a statement it should print something.

however it doesn’t do anything.

the exact same code work flawlessly when I put it in a python script behaviour.
it works as it should as soon as the executor finishes a statement it prints out a message.

but when I put the same code into the add on’s python file code it doesn’t do anything.

I don’t get any errors, and if I print(executor) it does appear to be holding the right executor.

Any help would be appreciated.

Carlos Plazas

Doing this will need a magic trick.
You need to create a global executors list first.
In the function, add executor to the list.
Then declare event for the last element of the list.
It would looks like this.

from vcCommand import *

global executors
executors = []

def printSomeThing(statement, immediate):
  print statement.Type
  
def addExecutors(comp, executor):
  executors = comp.findBehavioursByType(VC_ROBOTEXECUTOR)
  if (len(executors) > 0):
    executor = executors[0]
    executors.append(executor)    
    executors[-1].OnPostExecuteStatement = printSomeThing

Hey Chungmin

thanks for the reply. I was trying to implement your code but got confused at one part.
Im not sure what to pass the addExecutors() functions second parameter.

“executor”

it looks like you are grabbing the executor inside the function from the component. so what are you passing to its executor parameter. it doesn’t look like you are even using the argument you would pass
as the executor gets over written by = executors[0]

Ah, that’s a mistake.

def addExecutors(comp, executor):

should be

def addExecutors(comp):
1 Like

You also seem to be putting the executors list in
“executors” variable
then grabbing the first element.
and adding it back to the same list.
then grabbing the last element again.
I don’t understand why not just use the first element the first time you grab it?

oh ok ok thanks man yeah was a bit confused about that.

Hey Chungmin thank you
very much I used a modified version of your code basically using a global list to store the robot executors. and then subscribing to their events. It seemed to work great.
I didn’t grab the last element on the list as you showed I removed those lines and just used
executors[0].OnPostExecuteStatement = printSomeThing

and it worked fine so I left it like that for now.

I’m just wondering what the reason was that we have to do it this way I’m glad it’s working now but would like to get a deeper understanding of what the issue was, like why wasn’t working the other way and how come by putting it in a global list it works now.

If anyone has any insight would be greatly appreciated.