Skip to main content
Ctrl+K
NVIDIA Omniverse Launcher will be deprecated on October 1, 2025. Visit here for more details.
Omniverse Developer Guide - Home Omniverse Developer Guide - Home

Omniverse Developer Guide

  • twitter
  • youtube
  • instagram
  • www
  • linkedin
  • twitch
Omniverse Developer Guide - Home Omniverse Developer Guide - Home

Omniverse Developer Guide

  • twitter
  • youtube
  • instagram
  • www
  • linkedin
  • twitch

Table of Contents

Developer Guide

  • Developer Guide Overview
  • Create a Project
  • Develop a Project
  • Build a Project
  • Test a Build
  • Debug a Build
  • Package a Build
  • Publish a Package

Omniverse Kit

  • Kit Manual
  • Kit SDK Extensions
  • Kit Release Notes
    • Kit 107.2 Release Highlights
    • Kit 107.2 Release Notes
    • Kit 107.0 Release Highlights
    • Kit 107.0 Release Notes
    • Kit 106.5 Release Highlights
    • Kit 106.5.5 Release Notes
    • Kit 106.5.4 Release Notes
    • Kit 106.5.3 Release Notes
    • Kit 106.5.2 Release Notes
    • Kit 106.5.1 Release Notes
    • Kit 106.5.0 Release Notes
    • Kit 106.4 Release Highlights
    • Kit 106.4.0 Release Notes
    • Kit 106.3 Release Highlights
    • Kit 106.3 Release Notes
    • Kit 106.2 Release Highlights
    • Kit 106.2 Release Notes
    • Kit 106.1 Release Highlights
    • Kit 106.1 Release Notes
    • Kit 106.0 Release Highlights
    • Kit 106.0.3 Release Notes
    • Kit 106.0.2 Release Notes
    • Kit 106.0.1 Release Notes
    • Kit 106.0 Beta Release Notes
    • Kit 105.0 Release Highlights
    • Kit 105.0 Release Notes

Developer Reference

  • Technical Requirements
  • Tutorials and Examples
  • Code Samples
    • Audio
    • Events
    • Extensions
      • Startup and Shutdown functions
      • Enable a Kit Extension
      • Get All the Registered Extensions
      • Get the Config for an Extension
      • Get the File Path to an Extension
      • Extension Intercommunication
      • Testing Extensions with Python
    • Input Devices
      • Keyboard
      • Mouse
      • Gamepad
    • Physics
    • Python
      • Install a New Python Package from PyPI
      • Write to Log
    • Services
    • Settings
      • Get the Value of a Setting
      • Set the Value of a Persistent Setting
      • Set the Value of a Setting
      • Subscribe to Setting Changes
    • User Interface
      • Interactive Widgets
        • Window
        • Label
        • StringField
        • Button
        • Checkbox
        • HStack and VStack
        • Spacer
        • Image
        • ProgressBar
        • IntField
        • IntSlider
        • FloatField
        • FloatSlider
        • Interactive Widgets (with DataModels)
        • UI Widget Arguments (kwargs)
      • DataModels
    • USD
      • Create an Orthographic Camera
      • Create a Perspective Camera
      • Convert Between VtArray and Numpy Array
      • Find All the Prims of a Given Attribute
      • Find a Prim by Name
      • Find All the Prims of a Given Type
      • Get the Child of a Prim
      • Add an Inherit
      • Add a SubLayer
      • Assign a Material to a Mesh
      • Create an MDL Material
      • Create a UsdPreviewSurface Material
      • Concatenate a Prim Path
      • Concatenate a Property Name with a Prim Path
      • Get the Parent Path for a Prim Path
      • Check if a Prim Exists
      • Get the Currently Selected Prims
      • Get a Prim
      • Print or Export a Prim
      • Select a Prim by Prim Path
      • Add a Relationship Target
      • Check if a Property Exists
      • Create an Attribute
      • Create a Relationship
      • Get the Value of an Attribute
      • Get Custom Metadata
      • Get the Targets of a Relationship
      • Set the Value of an Attribute
      • Set Custom Metadata
      • Add a Payload
      • Add a Reference
      • Create a Payload
      • Create a Reference
      • Add a Specialize
      • Create a New Stage
      • Get the Current Stage
      • Set the Default Prim on a Stage
      • Set the Stage Linear Units
      • Set the Stage Up Axis
      • Compute the Bounding Box for a Prim
      • Get the Local Space Transforms for a Prim
      • Get the World Space Transforms for a Prim
      • Author Data for a Particular Variant
      • Create a Variant Set
      • Select a Variant for a Variant Set
      • Show or Hide a Prim
    • Viewport
      • Viewport Camera
      • Focus, Zoom or Frame a Prim
      • Get the Active Viewport Window
  • OpenUSD Exchange SDK
  • Developing with OpenUSD
  • Additional Omni USD Docs
  • Carbonite SDK
  • Repo Tools
  • Visual Scripting

Omniverse Common

  • Formats
  • Technical Requirements
  • Omniverse Glossary of Terms
  • Feedback and Forums
  • Omniverse Licenses
    • Omniverse License
    • Licensing Disclaimer
    • Product Licenses
      • omniverse-launcher
        • 1.9.8
      • usd-explorer
        • 2023.2.0
    • Other Licenses
    • Redistributable Omniverse Software
  • Data Collection & Usage
  • Code Samples
  • Universal Scene Description (OpenUSD) Code Samples
  • Print or Export a Prim

Print or Export a Prim#

Utility functions that allow you to print a string representation of a single prim, or export a single prim into a layer by itself.

Particularly useful for debugging large scenes, as USD provides Export() and ExportToString() at the stage level, but has no equivalent utilities for outputting information about only a single prim.

Attempts to ensure the prim is the same as in the source stage, by including changes introduced by the session layer, if include_session_layer=True.

from typing import Optional
from pxr import Sdf, Usd


def export_prim_to_layer(prim: Usd.Prim, flatten=True,
                         include_session_layer=True) -> Optional[Sdf.Layer]:
    """
    Creates a temporary layer with only the given prim in it

    Parameters
    ----------
    prim: Usd.Prim
        The usd object we wish to export by itself into its own layer
    flatten: bool
        If True, then the returned layer will have all composition arcs
        flattened. If False, then the returned layer will contain a reference
        to the original prim.
    include_session_layer: bool
        If True, then include changes from the session layer. If this is
        False, and the prim is ONLY defined in the session layer, then None
        will be returned.

    Note that no parents are included, and xforms are not "flattened" (even if
    `flatten` is True - `flatten` refers to USD composition arcs, not parent
    hierarchy or xforms).
    """
    orig_stage = prim.GetStage()
    orig_root_layer = orig_stage.GetRootLayer()
    orig_session_layer = orig_stage.GetSessionLayer()
    if orig_session_layer and not include_session_layer:
        # Make sure that the prim still exists if we exclude the session layer
        stage_no_session = Usd.Stage.Open(orig_root_layer, sessionLayer=None)
        if not stage_no_session.GetPrimAtPath(prim.GetPrimPath()):
            return None

        orig_session_layer = None

    # If there is a session layer, to get an EXACT copy including possible
    # modifications by the session layer, we need to create a new "copy" layer,
    # with a layer stack composed of the orig_stage's session layer
    # and root layer
    if not orig_session_layer:
        copy_layer = orig_root_layer
    else:
        copy_layer = Sdf.Layer.CreateAnonymous()
        copy_layer.subLayerPaths.append(orig_session_layer.identifier)
        copy_layer.subLayerPaths.append(orig_root_layer.identifier)

    # Now create a "solo" stage, with only our prim (from the copy layer)
    # referenced in
    solo_stage = Usd.Stage.CreateInMemory()
    solo_prim_path = Sdf.Path(f"/{prim.GetName()}")
    solo_prim = solo_stage.DefinePrim(solo_prim_path)
    solo_prim.GetReferences().AddReference(copy_layer.identifier,
                                           prim.GetPrimPath())
    solo_stage.SetDefaultPrim(solo_prim)

    if flatten:
        return solo_stage.Flatten()
    else:
        return solo_stage.GetRootLayer()


def export_prim_to_string(prim: Usd.Prim, flatten=True,
                          include_session_layer=True) -> Optional[str]:
    """
    Return a string representation of the given prim

    Parameters
    ----------
    prim: Usd.Prim
        The usd object we wish to convert to a string
    flatten: bool
        If True, then return a representation with all USD composition arcs
        flattened. If False, then return the prim definition from the
        strongest composition arc that contributes opinions to this prim.
    include_session_layer: bool
        If True, then include changes from the session layer. If this is
        False, and the prim is ONLY defined in the session layer, then None
        will be returned.

    Note that no parents are included, and xforms are not "flattened" (even if
    `flatten` is True - `flatten` refers to USD composition arcs, not parent
    hierarchy or xforms).
    """
    if not flatten:
        prim_stack = prim.GetPrimStack()
        if not include_session_layer:
            non_session_layers = \
                prim.GetStage().GetLayerStack(includeSessionLayers=False)
            prim_stack = \
                [x for x in prim_stack if x.layer in non_session_layers]
            if not prim_stack:
                return None
            return prim.GetPrimStack()[0].GetAsText()

    solo_layer = export_prim_to_layer(
        prim, flatten=True, include_session_layer=include_session_layer)
    if solo_layer is None:
        # If include_session_layer was False, it's possible that the prim
        # doesn't exist any more...
        return None
    solo_primspec = solo_layer.GetPrimAtPath(solo_layer.defaultPrim)
    return solo_primspec.GetAsText()


def print_prim(prim: Usd.Prim, flatten=True, include_session_layer=True):
    """
    Print a string representation of the given prim.

    Parameters
    ----------
    prim: Usd.Prim
        The usd object we wish to convert to a string
    flatten: bool
        If True, then print a representation with all USD composition arcs
        flattened. If False, then print the prim definition from the
        strongest composition arc that contributes opinions to this prim.
    include_session_layer: bool
        If True, then include changes from the session layer. If this is
        False, and the prim is ONLY defined in the session layer, then 'None'
        will be printed.

    Note that no parents are included, and xforms are not "flattened" (even if
    `flatten` is True - `flatten` refers to USD composition arcs, not parent
    hierarchy or xforms).
    """
    print(export_prim_to_string(prim, flatten=flatten,
                                include_session_layer=include_session_layer))

previous

Get a Prim

next

Select a Prim by Prim Path

NVIDIA NVIDIA
Privacy Policy | Manage My Privacy | Do Not Sell or Share My Data | Terms of Service | Accessibility | Corporate Policies | Product Security | Contact

Copyright © 2023-2025, NVIDIA Corporation.

Last updated on May 17, 2025.