Usage Examples#

Create a Basic Composition Widget#

import omni.ui as ui
from omni.kit.window.composition import CompositionWidget

# Create a window to display the composition widget
window = ui.Window("USD Composition View", width=800, height=600)

# Add the composition widget to the window
with window.frame:
    # Create a composition widget that automatically displays USD composition information
    composition_widget = CompositionWidget()

Working with CompositionModel and Accessing Data#

import omni.ui as ui
from omni.kit.window.composition import CompositionModel

# Create a window
window = ui.Window("USD Composition Analysis", width=800, height=600)

with window.frame:
    with ui.VStack():
        # Create the composition model that provides data about USD composition
        model = CompositionModel()

        # Add a button to demonstrate how to access model data
        def print_root_items():
            root_items = model.get_item_children(None)  # Get root items
            print(f"Found {len(root_items)} root composition items")

            # Print information about each root item
            for item in root_items:
                # Get the value models for the columns (0=layer, 1=arc_type, 2=path, 3=has_specs)
                layer_model = model.get_item_value_model(item, 0)
                arc_type_model = model.get_item_value_model(item, 1)
                path_model = model.get_item_value_model(item, 2)
                has_specs_model = model.get_item_value_model(item, 3)

                # Access the values
                layer = layer_model.get_value_as_string() if layer_model else "Unknown"
                arc_type = arc_type_model.get_value_as_string() if arc_type_model else "Unknown"
                path = path_model.get_value_as_string() if path_model else "Unknown"
                has_specs = has_specs_model.get_value_as_bool() if has_specs_model else False

                print(f"Layer: {layer}, Arc Type: {arc_type}, Path: {path}, Has Specs: {has_specs}")

                # You can also access children of this item
                children = model.get_item_children(item)
                print(f"  This item has {len(children)} children")

        ui.Button("Print Composition Info", clicked_fn=print_root_items)

        # Create a TreeView using the model to display composition information
        with ui.ScrollingFrame(
            vertical_scrollbar_policy=ui.ScrollBarPolicy.SCROLLBAR_AS_NEEDED,
            horizontal_scrollbar_policy=ui.ScrollBarPolicy.SCROLLBAR_AS_NEEDED,
            style_type_name_override="TreeView",
            height=500
        ):
            tree_view = ui.TreeView(
                model,
                # Define column widths: layer name, arc type, path, has specs
                column_widths=[ui.Pixel(150), ui.Pixel(100), ui.Fraction(1), ui.Pixel(100)],
                root_visible=False,
                header_visible=True,
                columns_resizable=True,
                resizeable_on_columns_resized=True
            )