UsdGeom meshes and cameras: Worked Example — Visualization (NVIDIA-Certified Professional: OpenUSD Development)
Introduction to UsdGeom Meshes and Cameras in OpenUSD In the context of the NVIDIA-Certified Professional: OpenUSD Development certification...
Introduction to UsdGeom Meshes and Cameras in OpenUSD
In the context of the NVIDIA-Certified Professional: OpenUSD Development certification, understanding how to work with UsdGeom meshes and cameras is essential for building and optimizing 3D content creation pipelines. This worked example demonstrates a practical approach to creating and manipulating a simple 3D scene using these components.
Scenario
You are tasked with creating a USD stage that contains a geometric mesh representing a simple cube and a camera positioned to view the cube. The goal is to define the mesh geometry, assign it to the stage, and set up a camera with appropriate parameters to capture the scene.
Step 1: Setting Up the USD Stage
Begin by creating a new USD stage, which acts as the container for all scene elements.
- Use Usd.Stage.CreateNew("example.usda") to create a new stage file.
Step 2: Defining the UsdGeom Mesh
The UsdGeomMesh schema is used to define polygonal geometry. For a cube, specify the vertices, face vertex counts, and face vertex indices.
- Define the points (vertices): A cube has 8 vertices. These are specified as a list of 3D points.
- Specify face vertex counts: Each face of the cube is a quad, so each face has 4 vertices.
- Specify face vertex indices: These indices reference the points list to define each face.
Code Snippet
from pxr import Usd, UsdGeom, Gf
stage = Usd.Stage.CreateNew("example.usda")
Define the mesh prim
mesh = UsdGeom.Mesh.Define(stage, "/Cube")
Define cube vertices
points = [ Gf.Vec3f(-1, -1, -1), Gf.Vec3f(1, -1, -1), Gf.Vec3f(1, 1, -1), Gf.Vec3f(-1, 1, -1), Gf.Vec3f(-1, -1, 1), Gf.Vec3f(1, -1, 1), Gf.Vec3f(1, 1, 1), Gf.Vec3f(-1, 1, 1) ]
mesh.CreatePointsAttr(points)
Each face has 4 vertices
faceVertexCounts = [4, 4, 4, 4, 4, 4] mesh.CreateFaceVertexCountsAttr(faceVertexCounts)
Indices for each face
faceVertexIndices = [ 0, 1, 2, 3, # Bottom face 4, 5, 6, 7, # Top face 0, 4, 7, 3, # Left face 1, 5, 6, 2, # Right face 3, 2, 6, 7, # Front face 0, 1, 5, 4 # Back face ] mesh.CreateFaceVertexIndicesAttr(faceVertexIndices)
Step 3: Defining the UsdGeom Camera
The UsdGeomCamera schema allows you to define camera parameters such as position, orientation, focal length, and clipping planes.
- Create the camera prim: Define a camera under the root of the stage.
- Set transform: Position the camera at (0, 0, 5) looking towards the origin.
- Set camera parameters: Define focal length, horizontal aperture, and clipping planes.
Code Snippet
from pxr import UsdGeom, Gf
camera = UsdGeom.Camera.Define(stage, "/Camera")
Set camera transform (translate 5 units on Z axis)
camera.AddTranslateOp().Set(Gf.Vec3d(0, 0, 5))
Set focal length in mm
camera.CreateFocalLengthAttr(50.0)
Set horizontal aperture in mm
camera.CreateHorizontalApertureAttr(20.955)
Set clipping planes
camera.CreateClippingRangeAttr((0.1, 1000.0))
Step 4: Saving the Stage
Once the mesh and camera are defined, save the USD stage to persist the scene.
- Call stage.GetRootLayer().Save() to write changes to the file.
Summary
This example demonstrated how to create a simple USD scene with a UsdGeomMesh representing a cube and a UsdGeomCamera positioned to view the mesh. Understanding these steps is critical for visualization tasks in OpenUSD pipelines, enabling developers to build and optimize 3D content efficiently.
For further study, explore how to integrate usdShade materials and usdLux lights to enhance scene realism.
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 →