Python Usage Examples
Creating Custom Stage Column Delegates
A custom StageColumnDelegate
can be created by subclassing from AbstractStageColumnDelegate
and implementing the
abstract methods. Here’s an example from the built-in VisibilityColumnDelegate
. For more details, please refer to
omni.kit.widget.stage.VisibilityColumnDelegate
.
class VisibilityColumnDelegate(AbstractStageColumnDelegate):
"""The column delegate that represents the visibility column"""
def __init__(self):
super().__init__()
self.__visibility_layout = None
self.__items_sort_policy = VisibilityColumnSortPolicy.DEFAULT
self.__stage_model: StageModel = None
def destroy(self):
if self.__visibility_layout:
self.__visibility_layout.set_mouse_pressed_fn(None)
self.__visibility_layout = None
self.__stage_model = None
@property
def initial_width(self):
"""The width of the column"""
return ui.Pixel(24)
def build_header(self, **kwargs):
"""Build the header"""
stage_model = kwargs.get("stage_model", None)
self.__stage_model = stage_model
self.__initialize_policy_from_model()
if stage_model:
self.__visibility_layout = ui.HStack(style=_get_header_styles())
with self.__visibility_layout:
ui.Spacer()
with ui.VStack(width=0):
ui.Spacer()
ui.Image(width=22, height=14, name="visibility_header", style_type_name_override="TreeView.Header")
ui.Spacer()
ui.Spacer()
self.__visibility_layout.set_mouse_pressed_fn(self.__on_visiblity_clicked)
else:
with ui.HStack(style=_get_header_styles()):
ui.Spacer()
with ui.VStack(width=0):
ui.Spacer()
ui.Image(width=22, height=14, name="visibility_header", style_type_name_override="TreeView.Header")
ui.Spacer()
ui.Spacer()
async def build_widget(self, _, **kwargs):
"""Build the eye widget"""
item = kwargs.get("stage_item", None)
if not item or not item.prim or not item.prim.IsA(UsdGeom.Imageable):
return
with ui.ZStack(height=20, style=_get_widget_styles()):
# Min size
ui.Spacer(width=22)
# TODO the way to make this widget grayed out
ui.ToolButton(item.visibility_model, enabled=not item.instance_proxy and item.active, name="visibility")
@property
def order(self):
# Ensure it's always to the leftmost column except the name column.
return -101
@property
def sortable(self):
return True
def on_stage_items_destroyed(self, items: List[StageItem]):
pass
@property
def minimum_width(self):
return ui.Pixel(20)
@property
def resizable(self):
return False
Registering Stage Column Delegates
StageColumnDelegateRegistry.register_column_delegate
can be used to register a custom StageColumnDelegate
, as shown
below. It’s important to keep the returned subscription object alive, otherwise the registration will be immediately
deregistered.
# Register column delegates
self._name_column_sub = StageColumnDelegateRegistry().register_column_delegate("Name", NameColumnDelegate)
self._type_column_sub = StageColumnDelegateRegistry().register_column_delegate("Type", TypeColumnDelegate)
self._visibility_column_sub = StageColumnDelegateRegistry().register_column_delegate("Visibility", VisibilityColumnDelegate)
Deregistering Stage Column Delegates
Unlike registering, to deregister a StageColumnDelegate
, there’s no explicit API calls required. The delegate will
automatically be deregistered when the subscription object is destroyed. For example:
self._visibility_column_sub = None
self._type_column_sub = None
self._stage_open_sub = None