Connecting custom python scripts as Nodes in Action Graphs to call functions

Hello,

I am using USD Composer and I have added custom functionalities in a python script.

I want to call these functions or scripts only when triggering them, not when Play is started by default.

Is there a way I can call these custom scripts or functions in an Action Graph and trigger them only on click? Is there any other way to call functions only when I need to?

Yes for sure. That is why ActionGraph is so powerful. You do not want all of your scripts starting at runtime, just because you hit play. The main idea of Actiongraph is you can assign each action script an “on click” node to start it. So for example “S” to start an action. Check out this great OMNIGRAPH overview page for scripting with nodes. In here you will see examples of how to assign keys. OmniGraph — Omniverse Extensions

And here is one specifically on keyboard shortcuts On Keyboard Input — Omniverse Extensions

Sorry I think the query was misunderstood, I have functions created in Python code, I want to call those scripts in the python script via Action graph on key press.

Well then, there is a “script” node in ActionGraph, ready to go. So you would just connect your “keyboard input” node in actiongraph to connect and trigger your python script node.

Here is a point-by-point explanation:

  1. I have created a Python script with multiple methods.
  2. I want to call each function from that python script from the action graph.
  3. In action graph, I pasted the Python script as Path.
  4. I want to know how to access the methods after that.

Hi there.

You can put your Python script in any registered folder for Python. To find what folders are added to Python’s sys path you can run this snippet in the script editor:

import sys
for sys_path in sys.path:
    print(sys_path)

If you would rather build up a library of your own scripts elsewhere, you can add a system path by running:

import sys
sys.path.append("C:/MyCode/Kit/scripts")

Once you have the Python script in place within a folder that is part of Python’s sys path, you can import it and use it. Example C:/MyCode/Kit/scripts/my_script.py which contains a function:

import sys

def print_sys_paths():
    for sys_path in sys.path:
        print(sys_path)

In your script node you can then import and use the function.

# This script is executed the first time the script node computes, or the next time
# it computes after this script is modified or the 'Reset' button is pressed.
#
# The following callback functions may be defined in this script:
#     setup(db): Called immediately after this script is executed
#     compute(db): Called every time the node computes (should always be defined)
#     cleanup(db): Called when the node is deleted or the reset button is pressed
# Available variables:
#    db: og.Database The node interface - attributes are exposed in a namespace like db.inputs.foo and db.outputs.bar.
#                    Use db.log_error, db.log_warning to report problems in the compute function.
#    og: The omni.graph.core module
import sys
my_path = "C:/Users/cakesson/Documents/Kit/shared/scripts"
if not my_path in sys.path:
	sys.path.append(my_path)
import my_script

def setup(db: og.Database):
    pass


def cleanup(db: og.Database):
    pass


def compute(db: og.Database):
    my_script.print_sys_paths()
    return True

Hopefully this is helpful.

Thanks for the detailed instructions.

I followed the mentioned steps and was able to see the sys paths printing from my python script which was added to the appended folder path.

But when I try to change the code (For instance, Add a API code), The Old code runs no matter how many times I stop and Play / Reload script.

Is there something I am missing or could this be a Bug?

Please let me know, Thanks

Hi there.
Put these lines in your compute to force reload your script as you iterate.

def compute(db: og.Database):
    from importlib import reload
    reload(my_script)
    my_script.print_sys_paths()
    return True