Correct way to for an extension to load layers

Hello,

I’m working with a custom create extension, i’m trying to get the extension to load layers after loading an intital USD as a layer.

I have it working but i don’t think i’m going the right way about it. I’m using the LayerUtils file from the official Omniverse extension library, and currently i have to load one USD as a layer, then i have to load it again along with any other layers i want. Can anybody advise how to make this work with just loading the inittial layer once before adding the additional layers. The code is below:

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)

Hello @michaelbaggott! I’ll get the development team to help you out! Appreciate you reaching out to us!

@michaelbaggott You can do it like this:

import omni.usd
import asyncio


async def prepare_stage(master_path):
	(result, error) = await omni.usd.get_context().open_stage_async(master_path)
	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(your_master_path))

Or if you want to initialize your stage after stage event like your code, it can be modified as:

 import omni.kit.window.file
import omni.usd

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

def on_stage_event(event):
	if event.type == int(omni.usd.StageEventType.OPENED):
		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)
	elif event.type == int(omni.usd.StageEventType.OPEN_FAILED):
		print(f"Stage open failed")

event_stream = omni.usd.get_context().get_stage_event_stream()
self.stage_event_sub = event_stream.create_subscription_to_pop(on_stage_event)

So one concept need to be clarified: You should not use the following to get the stage you opened. This means it will create a new stage that shares the same masterPath with the stage you opened with omni.kit.window.file.open_stage().

 stage_ref = Usd.Stage.Open(masterPath)

Thank you Wendy!

Thank you, i will give this a try next week. Much appreciated