Create Extension not autoloading first go

I have made an extension for Create to automatically load USD levels as layers. The idea is when the extension has a variable set to true, it will go ahead and load in the requested USD’s.

One issue i am having with this is when i set the extension to automatically load, it does nothing on the first launch of create, but it works if i disable it, then re-enable it. The code below. I’m having another issue with the loading of the layers but i will make another topic for that.

if enableExtension == True:

        omni.kit.window.file.open_stage(masterPath)

        stage_ref = Usd.Stage.Open(masterPath)

        root_layer = stage_ref.GetRootLayer()

        new_layer1 = LayerUtils.insert_sublayer(root_layer, -1, dayPath, True)

        def on_stage_event(event):

            if event.type in [int(omni.usd.StageEventType.ASSETS_LOADED)]:

                stage_ref = Usd.Stage.Open(masterPath)

                root_layer = stage_ref.GetRootLayer()

                new_layer1 = LayerUtils.insert_sublayer(root_layer, -1, dayPath, True)

                new_layer2 = LayerUtils.insert_sublayer(root_layer, -1, nightPath, True)

                new_layer3 = LayerUtils.insert_sublayer(root_layer, -1, sdotPath, True)

        event_stream = omni.usd.get_context().get_stage_event_stream()

        self.stage_event_sub = event_stream.create_subscription_to_pop(on_stage_event)

Hi @michaelbaggott! I reached out to the dev team for more help on this. I am hoping to hear back shortly. Appreciate you contacting us!

@michaelbaggott I think you asked the same question here: Correct way to for an extension to load layers. You can refer to the same answer.

Hi Rozhang,

This code works now, but what i was asking in this question was different.

I want to run the extension automatically when Create opens, so i have checked the extension to be turned on here:

But the extension won’t run until i manually switch it off, then back on again. ie. it does not run automatically on the first load of Create

@michaelbaggott Do you mean the extension is loaded on startup, but your logic is not executed until you secondly switched it off and on? Could you show me your complete code about how those sublayers are initialized? I think it’s issue of your code initialization.

Hi Rozhang,

Yes the extension is turned on, but when Create first opens the code is not executed and the extension window doesn’t appear on screen until i switch it off then on again after openingf Create

Here is the complete extension code

enableExtension = True

masterPath = 'omniverse://localhost/Projects/MoretonMaster/Moreton_Master.usd'

dayPath = 'omniverse://localhost/Projects/Moreton/Moreton_Day.usd'

nightPath = 'omniverse://localhost/Projects/MoretonNight/Moreton_Night.usd'

sdotPath = 'omniverse://localhost/Projects/MoretonSDot/SDotLevel.usd'

import threading

import omni.ext

import omni.ui as ui

import omni.usd

import asyncio

import sys

class MyExtension(omni.ext.IExt):

    def on_startup(self, ext_id):

        print("[omni.hello.world] MyExtension startup")

        self._window = ui.Window("My Window", width=300, height=300)

        with self._window.frame:

            with ui.VStack():

                ui.Label("Automatically open file")

                def on_click():

                    print(f"click")

                ui.Button("Click Me", clicked_fn=lambda: on_click())

        if enableExtension == True:

            async def prepare_stage(masterPath):

                (result, error) = await omni.usd.get_context().open_stage_async(masterPath)

                if not result:

                    print(f"Errors: error")

                stage = omni.usd.get_context().get_stage()

                root_layer = stage.GetRootLayer()

                root_layer.subLayerPaths.append(dayPath)

                root_layer.subLayerPaths.append(nightPath)

                root_layer.subLayerPaths.append(sdotPath)

            asyncio.ensure_future(prepare_stage(masterPath))

           

    def on_shutdown(self):

        print("[omni.hello.world] MyExtension shutdown")

Hi, Michael,

Did you see any errors from the console window? Can you paste the config/extension.toml also here? Just to make sure that “omni.ui” and “omni.usd” is in your dependency list of your extension. Or if you can, it’s better to package the minimum extension that could repro it.

Thanks.

Michael, I can repro this with Create. I’ll get back to you asap after I got the answer.

Thanks so much Rozhang, no errors in the console window. Dependencies are not listed in the toml file but i can add them, it seems to work without them though when i manually turn it off and on

@michaelbaggott I am afraid you’d have to live with it for now or you can add a menu item to Window like the following, as Create has window management that will hide those non-builtin ones at startup. You can now enable/disable your window from window menu.

import omni.ext
import omni.ui as ui
import omni.kit

# Functions and vars are available to other extension as usual in python: `example.python_ext.some_public_function(x)`
def some_public_function(x: int):
    print(f"[example.python_ext] some_public_function was called with {x}")
    return x ** x


# Any class derived from `omni.ext.IExt` in top level module (defined in `python.modules` of `extension.toml`) will be
# instantiated when extension gets enabled and `on_startup(ext_id)` will be called. Later when extension gets disabled
# on_shutdown() is called.
class HelloPythonExtension(omni.ext.IExt):
    # ext_id is current extension id. It can be used with extension manager to query additional information, like where
    # this extension is located on filesystem.
    def on_startup(self, ext_id):
        print("[example.python_ext] HelloPythonExtension startup")

        self._window_title = "My Window"
        self._menu_path = "Window/My Window"
        self._window = None

        try:
            self._menu = omni.kit.ui.get_editor_menu().add_item(
                self._menu_path, self.show_window, toggle=True, value=False
            )
        except Exception as e:
            self._menu = None

    def on_shutdown(self):
        print("[example.python_ext] HelloPythonExtension shutdown")
        if self._window:
            self._window.set_visibility_changed_fn(None)

        self._window = None
        self._menu = None

    def build_window(self):
        self._window = ui.Window(self._window_title, width=300, height=300, visible=True)
        self._window.set_visibility_changed_fn(self._visibility_changed_fn)
        with self._window.frame:

            with ui.VStack():

                ui.Label("Automatically open file")

                def on_click():

                    print(f"click")

                ui.Button("Click Me", clicked_fn=lambda: on_click())
    
    def show_window(self, menu, value):
        if not self._window:
            self.build_window()
        elif self._window:
            self._window.visible = value
        
    def _visibility_changed_fn(self, value):
        if self._menu:
            omni.kit.ui.get_editor_menu().set_value(self._menu_path, value)