Hi!
TL;DR:
I have troubles finding a solution for a Python related problem.
I want to call a function in a Python script from another Python script while the simulation is not running.
Let’s say that I have a component ‘talker’:
This component has a button and a Python script that is linked to the button. When I click this button, I can launch the function talker() which in turn calls a function that makes the talker say ‘Hello’. This works perfectly fine as a way to manually launch a piece of Python code.
# talker::say_hello
from vcScript import *
app = getApplication()
comp = getComponent()
def talker(arg):
say_hello()
def say_hello():
print('Hello!')
button = comp.getProperty('talker')
button.OnChanged = talker
Now, let’s say that I have another component ‘guest’. This guest expects that the talker kindly says hello, and therefore I have a very similar button-script setup to manually launch this request:
# guest::say_hello_to_me
from vcScript import *
app = getApplication()
comp = getComponent()
def say_hello_to_me(arg):
# Here I want to call say_hello() from talker.py
button = comp.getProperty('say_hello_to_me')
button.OnChanged = say_hello_to_me
However, I am not able to call say_hello() that resides in talker::say_hello, now from guest::say_hello_to_me. Calling this function in the first script itself was - not surprisingly - possible, but I actually want to call it from outside.
Some options I tried, but FAILED:
- finding the VcScript object and importing it:
from app.findComponent('talker').findBehaviour('say_hello') import say_hello
say_hello()
- trying to call the function with a prefix:
talker = app.findComponent('talker').findBehaviour('say_hello')
talker.say_hello()
I found a workaround by adding a Signal property to the ‘talker’ component. I put my functionality in the OnSignal() event handler, and I am able to successfully launch the functionality from the other Python script:
app.findComponent('talker').findBehaviour('my_signal').signal('')
However, this is only possible when the simulation is running. Now, my implementation becomes very slow compared to when I launch my functionality manually by using the buttons.
I am aware that Python can be used in different ways, maybe I should not implement my functionality in component behaviours? Although for launching everything manually my approach worked very well.
So my question boils down to: How can I call a Python function (def) from another Python script while the simulation is not running?