Referencing behavior scripts

Hello everyone,

I am currently working on a project in NVIDIA Omniverse and have encountered an issue with Python Behavior Scripts. I would like to know how to reference Python Behavior Scripts in Omniverse, specifically: How can I access the object of one script from another Python script?

Does anyone have experience with this or can provide guidance on how to best implement this? Any help would be greatly appreciated! Thank you in advance!

1 Like

Hi niclas,

I had the same problem and after a lot of research I got to the conclusion that getting the reference or instance of the Behavior Script attached to a prim, if it’s possible, it is not the best way to communicate between a Behavior Script and a different one. Check this video for further explanations: Omniverse Developer Q&A - 1/6/2023

The way it is solved is by means of event streams. This way you can subscribe to a specific type of event, which you can create, and tell the Behavior Script that whenever this type of event is pushed, call a specific function. Check this doc to learn more about events: Event streams

Let’s follow a simple guide right now. First, go to the Behavior Script and write, for example, on the on_init() method the following code:

import omni.kit.app
import carb.events
        
TEST_EVENT = carb.events.type_from_string("omni.my.extension.TEST")    
bus = omni.kit.app.get_app().get_message_bus_event_stream()
self.sub1 = bus.create_subscription_to_push_by_type(TEST_EVENT, self.test)

Then add the specified function:

def test(self, e):
        print("It works!")

Finally, go to the other python script and, again, follow this steps. First, get the exact same event and the bust event stream:

import omni.kit.app
import carb.events

TEST_EVENT = carb.events.type_from_string("omni.my.extension.TEST")    
bus = omni.kit.app.get_app().get_message_bus_event_stream()

To finish, push the event through the bus (you can add some information using the payload argument):

bus.push(TEST_EVENT, payload={"info": "this is a text"})

In order this to work properly, you need to subscribe first and then push the event.

I hope this solves your problem :)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.