How to add usdz model to scene using code

Hi everyone. If anyone has a second, can anyone help me with the code to add a model to my scene? I have done this tutorial on spawning primitives (GitHub - NVIDIA-Omniverse/kit-extension-sample-spawn-prims) but am confused on how to translate this to models. Specifically I downloaded a usdz model from the asset stores and placed it in my omniverse drive. But trying to use omniverse codes generated command does not work. I was able to get it to import using omni.replicator.core.create.from_usd() but this uses replicator which is supposedly for deep learning dataset creation so not sure if this is what I should be doing here.

P.S. Are there more tutorials somewhere besides whats on the contest github page? I am finding the code documentation to be pretty unhelpful for actual programming

Hi @morgan.kohler! Using the Commands Window, you can look at your commands history to learn more about what command are out exist and how to use them. Here’s my history after dragging and dropping a USDZ onto my stage:

Here’s the code for you:

import omni.kit.commands
from pxr import Sdf
import omni.usd

omni.kit.commands.execute('CreateReference',
	path_to=Sdf.Path('/World/where/to/reference/it'),
	asset_path='C:/path/to/my_file.usdz',
	usd_context=omni.usd.get_context())

To learn from more examples, you can also to a look at the source code for any extension. Click on the extension you’re interested in the Extension Manager and open it in VSCode by clicking on the VSCode icon:

I will show an example at our Developer Office Hours tomorrow at 11AM (PDT). You can join on Discord for a live follow-up or catch the recording on YouTube.

Hi Mati, thanks for responding. This solution worked for a newly started scene in code but when I load a usd file in code and then try to use this, I get “ValueError: Not a valid path”. Do I need to first set the scene somehow after loading it in code?

Hi @morgan.kohler. No, I think the same code example should just work. Could you share a sample scene and code that’s causing the error?

Not sure why this was happening but seems to be resolved! Thanks

Here’s the timestamped video link to the Developer Office Hour where I covered this question: