Getting started with NVIDIA Omniverse KIT — handling selection

Working with CustomData on USD Prims

I’d like to note that a more sustainable way of putting your domain data into USD is by defining schemas, see USD Schema Documentation here:
https://graphics.pixar.com/usd/release/tut_generating_new_schema.html

setting custom data:
prim.SetCustomDataByKey(“my_data_key”, my_data)
getting custom data:
my_data = prim.GetCustomDataByKey("my_data_key")
iterating over all custom data:
for key in prim.GetCustomData():
value = prim.GetCustomDataByKey(key)

Subscribing to selection events:

We subscribe for all stage events in the startup callback of the extension:

def on_startup(self, ext_id):  
self._usd_context = omni.usd.get_context()
self._selection = self._usd_context.get_selection()
self._events = self._usd_context.get_stage_event_stream()
self._stage_event_sub = self._events.create_subscription_to_pop(
self._on_stage_event,
name='my stage update'
)
def on_shutdown(self): # cleanup 
self._window = None
self._stage_event_sub = None
def _on_stage_event(self, event):
if event.type == int(omni.usd.StageEventType.SELECTION_CHANGED):
self._on_selection_changed()
def _on_selection_changed(self):
selection = self._selection.get_selected_prim_paths()
stage = self._usd_context.get_stage()
print(f'== selection changed with {len(selection)} items')
if selection and stage:
for selected_path in selection:
print(f' item {selected_path}:')
prim = stage.GetPrimAtPath(selected_path)
for key in prim.GetCustomData():
print(f' — {key} = {prim.GetCustomDataByKey(key)}')

Creating a GUI

Now we want to create a GUI which displays the selected prim path and lists all custom data attributes in a simple key value list.

GUI displaying custom data attributes

selected prim path:

selected prim path display
#-- create simplestringmodel with initial value "-":self._selected_primpath_model = ui.SimpleStringModel(“-”) [...]#-- create a readonly stringfield for display using this model: self._selectedPrimName = ui.StringField( 
model=self._selected_primpath_model,
read_only=True)

customdata attributes:

custom data property display
class CustomDataAttributesModel(ui.AbstractItemModel):
[...]
def set_prim(self, usd_prim ):
# we reset the model with the new custom data
self._children = []
for key in usd_prim.GetCustomData():
item = NameValueItem(key, usd_prim.GetCustomDataByKey(key))
self._children.append(item)
# emit data changed
self._item_changed(None)
class NameValueItem(ui.AbstractItem):
def __init__(self, text, value):
super().__init__()
self.name_model = ui.SimpleStringModel(text)
self.value_model = ui.SimpleStringModel(value)
tree_view = ui.TreeView(
self._customdata_model,
root_visible=False,
header_visible=False,
columns_resizable=True,
column_widths=[ui.Fraction(0.4), ui.Fraction(0.6)],
style={'TreeView.Item': {'margin': 4}})

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michael T. Wagner

Michael T. Wagner

35 Followers

CTO and Co-Founder @ipolog.ai, creating clever solutions for smart factory