Skip to content

Commit 5aefbd9

Browse files
authored
Add more stage examples (Open, CreateInMemory, RootLayer). Fixes #14. (#42)
* Add more stage examples (Open, CreateInMemory, RootLayer). Fixes #14. Signed-off-by: PovelC <povel.croona@gmail.com> * Review updates Signed-off-by: PovelC <povel.croona@gmail.com> --------- Signed-off-by: PovelC <povel.croona@gmail.com>
1 parent 941372a commit 5aefbd9

1 file changed

Lines changed: 104 additions & 5 deletions

File tree

docs/stage-setting/stage.md

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ kernelspec:
3131
Welcome to this lesson on OpenUSD {term}`stages <Stage>`, a core element in 3D scene description. Understanding OpenUSD stages enables collaboration across various applications and datasets by allowing us to aggregate our data in one place.
3232

3333
In this lesson, we will:
34-
* Define the role of stages in 3D scene description.
34+
35+
- Define the role of stages in 3D scene description.
3536

3637
## What Is a Stage?
3738

3839
At its core, an OpenUSD stage presents the scenegraph, which dictates what is in our scene. It is the hierarchy of objects, called {term}`prims <Prim>`. These prims can be anything from geometry, to materials, to lights and other organizational elements. This scene is commonly stored in a data structure of connected nodes, which is why we refer to it as the scenegraph.
3940

4041
```{kaltura} 1_cm4ehcvo
42+
4143
```
4244

4345
### How Does It Work?
@@ -65,10 +67,10 @@ Creating a USD stage is the first step to generating a new USD scenegraph. In Py
6567
```python
6668
# Create a new, empty USD stage where 3D scenes are assembled
6769
Usd.Stage.CreateNew()
68-
70+
6971
# Open an existing USD file as a stage
7072
Usd.Stage.Open()
71-
73+
7274
# Saves all layers in a USD stage
7375
Usd.Stage.Save()
7476
```
@@ -80,10 +82,11 @@ Usd.Stage.Save()
8082
At its core, an OpenUSD [stage](https://openusd.org/release/glossary.html#usdglossary-stage) refers to a top-level USD file that serves as a container for organizing a hierarchy of elements called prims. Stages aren't files, but a unified scenegraph populated from multiple data sources called [layers](https://openusd.org/release/glossary.html#usdglossary-layer).
8183

8284
Some of the functions we will use to access the stage will be the following:
85+
8386
- [`Usd.Stage.CreateNew()`](https://openusd.org/release/api/class_usd_stage.html#a50c3f0a412aee9decb010787e5ca2e3e): Creates a new empty USD Stage where 3D scenes are assembled.
8487
- [`Usd.Stage.Open()`](https://openusd.org/release/api/class_usd_stage.html#ad3e185c150ee38ae13fb76115863d108): Opens an existing USD file as a stage.
8588
- [`Usd.Stage.Save()`](https://openusd.org/release/api/class_usd_stage.html#adefa2f7ebfc4d8c09f0cd54419aa36c4): Saves the current stage of a USD stage back to a file. If there are multiple layers in the stage, all edited layers that contribute to the stage are being saved. In our case, all edits are being done in a single layer.
86-
89+
8790
```{code-cell}
8891
# Import the `Usd` module from the `pxr` package:
8992
from pxr import Usd
@@ -97,8 +100,104 @@ print(stage.ExportToString(addSourceFileComment=False))
97100

98101
Here we created a `usda` file using Python, loaded it as a stage, and printed out the stage's contents. Since nothing is in our stage we do not get much from the output.
99102

100-
`.usda` are human-readable UTF-8 text. The [Crate file](https://openusd.org/release/glossary.html#crate-file-format) format is USD's own binary file format whose file extension is `.usdc` and is bi-directionally convertible to the `.usda` text format. `.usd` can refer to either Crate or text files.
103+
```{seealso}
104+
`.usda` is a human-readable text format for OpenUSD.
105+
Read more about the native file formats in the
106+
[USD File Formats lesson](https://docs.nvidia.com/learn-openusd/latest/stage-setting/usd-file-formats.html).
107+
```
108+
109+
### Example 2: Open and Save USD Stages
110+
111+
A common task when working with OpenUSD is opening an existing file, making changes to the stage, and then saving the result back to disk. The `Usd.Stage.Open()` function loads a USD file as a stage, and `stage.Save()` writes any edits you make to the stage's root layer.
112+
113+
In this example, we open an existing USDA file, add a prim so the modification is visible, and then save the updated stage.
114+
115+
```{code-cell}
116+
from pxr import Usd
117+
118+
# Open an existing USD stage from disk:
119+
stage: Usd.Stage = Usd.Stage.Open("_assets/first_stage.usda")
120+
121+
# Add a simple prim so we can see a change in the saved file:
122+
stage.DefinePrim("/World", "Xform")
123+
124+
# Save the stage back to disk:
125+
stage.Save()
126+
127+
# Print the stage as text so we can inspect the result:
128+
print(stage.ExportToString(addSourceFileComment=False))
129+
```
130+
131+
Here we opened an existing stage, modified its scenegraph by adding a prim, and saved the result back into the same root layer file. Any edits made to the stage are written to the root layer unless additional layers are introduced.
132+
133+
### Example 3: Create a Stage in Memory
101134

135+
Sometimes you may want to create a stage without immediately writing it to disk. This is useful when generating temporary data, running tests, or building a stage that you only want to save after validating its contents.
136+
137+
The `Usd.Stage.CreateInMemory()` function creates a stage whose root layer exists only in
138+
memory until you explicitly export it.
139+
140+
```{code-cell}
141+
from pxr import Usd
142+
143+
# Create a new stage stored only in memory:
144+
stage: Usd.Stage = Usd.Stage.CreateInMemory()
145+
146+
# Add a prim so the stage contains some data:
147+
stage.DefinePrim("/World", "Xform")
148+
149+
# Print the stage's contents:
150+
print("In-memory stage:")
151+
print(stage.ExportToString(addSourceFileComment=False))
152+
153+
# Export the stage to disk if needed:
154+
stage.Export("_assets/in_memory_stage.usda")
155+
```
156+
157+
In this example, the stage begins entirely in memory and is not written to disk until Export() is called. This makes CreateInMemory() useful for temporary stages, procedural generation, and workflows where you want to avoid unnecessary file writes.
158+
159+
### Example 4: Working With the Root Layer
160+
161+
Every stage has a root layer, which is the first layer opened by the stage.
162+
Although it acts as the anchor for the layer stack, the majority of authored data may reside in other layers depending on the composition. When you create a stage with CreateNew(), the file you pass becomes its root layer.
163+
164+
In this example, we access the root layer directly, inspect its metadata, and add a sublayer to demonstrate how the root layer organizes a stage’s data.
165+
166+
```{code-cell}
167+
from pxr import Usd, Sdf
168+
169+
# Create a new stage:
170+
stage: Usd.Stage = Usd.Stage.CreateNew("_assets/root_layer_example.usda")
171+
172+
# Get the root layer object:
173+
root_layer: Sdf.Layer = stage.GetRootLayer()
174+
print("Root layer identifier:", root_layer.identifier)
175+
176+
# Add a simple prim so the stage is not empty:
177+
stage.DefinePrim("/World", "Xform")
178+
179+
# Create an additional layer (in a different format) and add it as a sublayer:
180+
extra_layer: Sdf.Layer = Sdf.Layer.CreateNew("_assets/extra_layer.usdc")
181+
root_layer.subLayerPaths.append(extra_layer.identifier)
182+
183+
# Save both layers:
184+
stage.Save()
185+
extra_layer.Save()
186+
187+
# Print the contents of the root layer:
188+
print("Root layer contents:")
189+
print(root_layer.ExportToString())
190+
```
191+
192+
In this example, the file passed to `CreateNew()` becomes the stage’s root layer.
193+
We access the root layer to inspect it, attach an additional {term}`sublayer <Sublayer>`,
194+
and save both files. This illustrates how the root layer participates in the layer stack,
195+
and that sublayers can use different USD file formats.
196+
197+
```{seealso}
198+
Sublayers are covered in depth in the
199+
[Sublayers lesson](https://docs.nvidia.com/learn-openusd/latest/creating-composition-arcs/sublayers/index.html).
200+
```
102201

103202
## Key Takeaways
104203

0 commit comments

Comments
 (0)