Scripting OpenUSD data interchange: Worked Example — Data Exchange (NVIDIA-Certified Professional: OpenUSD Development)
Scripting OpenUSD Data Interchange: A Worked Example In the context of the NVIDIA-Certified Professional: OpenUSD Development certification...
Scripting OpenUSD Data Interchange: A Worked Example
In the context of the NVIDIA-Certified Professional: OpenUSD Development certification, scripting OpenUSD data interchange is a critical skill. This involves automating the import and export of USD (Universal Scene Description) data to enable seamless data exchange between 3D content creation tools and pipelines.
Below is a detailed, step-by-step worked example demonstrating how to script a simple OpenUSD data interchange task using Python and the USD Python API. The scenario involves exporting a custom 3D scene with basic geometry and then importing it back to verify the data integrity.
Scenario
You are tasked with creating a script that:
- Creates a USD stage with a simple geometric primitive (a cube).
- Sets some custom attributes on the cube.
- Exports the stage to a USD file.
- Imports the USD file back and reads the attributes to verify the data.
Step 1: Setting Up the Environment
Ensure you have the USD Python bindings installed and accessible in your environment. Import the necessary modules:
from pxr import Usd, UsdGeom, Sdf
Step 2: Creating a USD Stage and Adding Geometry
Create a new USD stage in memory and define a cube primitive:
stage = Usd.Stage.CreateNew("example_scene.usda") cube = UsdGeom.Cube.Define(stage, "/World/Cube")
Set the size of the cube:
cube.CreateSizeAttr(2.0)
Step 3: Adding Custom Attributes
Add a custom string attribute to the cube to store metadata, for example, the material name:
cube.CreateCustomDataAttr().Set({"material": "plastic_blue"})
Alternatively, you can use the CreateAttribute method:
cube.CreateAttribute("custom:material", Sdf.ValueTypeNames.String).Set("plastic_blue")
Step 4: Saving the USD Stage
Save the stage to disk:
stage.GetRootLayer().Save()
Step 5: Importing the USD File and Reading Data
Open the saved USD file and access the cube primitive:
imported_stage = Usd.Stage.Open("example_scene.usda") imported_cube = UsdGeom.Cube(imported_stage.GetPrimAtPath("/World/Cube"))
Read the size attribute:
size = imported_cube.GetSizeAttr().Get()Read the custom material attribute:
material = imported_cube.GetPrim().GetAttribute("custom:material").Get()Step 6: Verification and Output
Print the values to verify correct data interchange:
print(f"Cube size: {size}") print(f"Cube material: {material}")Worked Example Summary
Problem: Automate the creation, export, and import of a USD scene containing a cube with custom attributes.
Solution Steps:
- Import USD Python API modules.
- Create a new USD stage and define a cube primitive.
- Set the cube size and add a custom attribute for material.
- Save the USD stage to a file.
- Open the USD file and retrieve the cube primitive.
- Read and print the cube's size and custom material attribute to verify.
This scripting approach enables developers to build custom importers/exporters and automate data exchange workflows efficiently, a key competency for the NVIDIA-Certified Professional: OpenUSD Development exam.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →