Creating new USD Stage via Python Console and Composer

Greetings, everyone!

I am presently a beginner learning USD and Python together, and am currently following an online book as my onramp, titled, USD in One Weekend: 3. Creating an OpenUSD scene - OpenUSD in One Weekend

I find that my trouble at the moment lies in creating, populating, and saving a usd stage via python scripting. For personal experimentation purposes, I have been attempting to do so via the python console in the NVIDIA Omniverse USD Composer, VS Code, Blender Script Editor, and Miniconda through the terminal.

However, I find that I am running into syntax errors through each of these methods. I understand trouble in establishing a scripting directory and saving a .usda stage in python is truly a beginner struggle.

In an effort to to match my ambitions with the possibilities of USD, I ask for any assistance, as it would be very much appreciated.

Thank you!

That looks like it should work. Could you paste the full error message here?

Hi, Mati.

Sure thing. Here it is:

Error in ‘pxrInternal_v0_25_2__pxrReserved__::SdfLayer::_CreateNew’ at line 600 in file C:\g\207190794\USD\pxr\usd\sdf\layer.cpp : ‘A layer already exists with identifier ‘C:/OMUSD/kit-app-template/_build/windows-x86_64/release/sphere.usda’’

At:
c:/users/super/appdata/local/temp/xlmo.0/script_1766022377.py(4):

Thanks!

Ah, yes. So there is already a “sphere.usda” created at that location. This is can happen in learning situations like this where you’re experimenting and running the same script multiple times. One option is to just delete the file and run the script again. Another option is to use Usd.Stage.CreateInMemory() so you have more control over when the file is written. Lastly, you could try something like what we do here in Learn OpenUSD to open the file instead of creating it if it already exists: LearnOpenUSD/src/utils/helperfunctions.py at main · NVIDIA-Omniverse/LearnOpenUSD · GitHub

Oh, I see. Thank you for your understanding, Mati.

Interestingly enough, my scene never populated with the sphere prim. Or, is it that I’m pulling redundant data from the USD Composer prefabs? Please don’t hesitate to correct me if my idea here is mistaken.

Usd.Stage.CreateInMemory(), this seems to be an incredibly useful approach to the matter…

And also, thank you for linking me to the LearnOpenUSD helper functions! I’m afraid that I’m receiving some rookie syntax errors here. Would you mind pointing towards a little bit of correction?

Thanks again.

For Kit-based apps, you’re not necessarily authoring to the stage you’re seeing in the viewport when you use Usd.Stage.CreateNew() or Usd.Stage.Open(). If you want to continue using Kit while you learn, I’d recommend using this to get the current loaded stage:

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

Then you can continue with the rest of the code from the examples using that stage. Doing it this way, you can also use File->New and File->Save menu items in the main window to clear the stage or save your work. Additionally, working this way you won’t need to worry about the initial error you hit or using that helper function.

For any syntax errors you’re encountering, this is primarily a challenge with learning Python. I’d recommend using an LLM as a learning companion to help you fix the errors and explain the errors to you as you go along.

My apologies for the delayed response. I had spent some time experimenting and attempting.

But I see. This makes more sense now. The code is also seems very helpful as well. Thank you, Mati.

For one reason or another, I haven’t been able to successfully ‘author’ The example scenes, I ask your pardon!

And yes, very good point. In this day and age, might as well take advantage!