Extension: omni.kit.menu.utils-1.7.5

Documentation Generated: Nov 18, 2024

Overview

omni.kit.menu.utils allows users to add/remove/update menus for the top bar of the window.

Adding a menu for your extension

from omni.kit.menu.utils import MenuItemDescription

import carb.input

def on_startup(self, ext_id):
    self._file_menu_list = [
        MenuItemDescription(
            name="Menu Example",
            glyph="file.svg",
            onclick_action=("omni.kit.menuext.extension", "menu_example"),
            hotkey=(carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, carb.input.KeyboardInput.T),
        )]

    omni.kit.menu.utils.add_menu_items(self._file_menu_list, "File")

def on_shutdown(self):
    omni.kit.menu.utils.remove_menu_items(self._file_menu_list, "File")

What does this do?

  • Adds a menu item “Menu Example” in “File” menu using icon file.svg and when clicked executes action “omni.kit.menuext.extension” / “menu_example”.

NOTES:

  • You need to keep a copy of your file menu list to prevent python from garbage collecting it.

  • Although kit uses fastexit and on_shutdown is never called, a user can still disable your extension and on_shutdown will be called, so you need to cleanup your menus otherwise you will get leaks and menu will still be shown.

Delegates

You can have a delegate associated with your menu

Currently supported functions are:

  • build_item(item: ui.Menu)

    • created ui for menu items, be careful overriding this function as IconMenuDelegate has a custom build_item

  • get_elided_length(menu_name: str)

    • returns max string length & any menu text over this length will be elided

  • get_menu_alignment() returns MenuAlignment.LEFT or MenuAlignment.RIGHT

    • used by “Live” and “Cache” button on top right. These are right aligned menu items with custom build_item function

  • update_menu_item(menu_item: Union[ui.Menu, ui.MenuItem], menu_refresh: bool)

    • allows modification of ui.Menu/ui.MenuItem. menu_refresh indicates its updating state during ui triggered_fn

Example

from omni.kit.menu.utils import MenuItemDescription, IconMenuDelegate

class FileMenuDelegate(IconMenuDelegate):
    def build_item(item: ui.Menu):
        super().build_item(item)
        ui.Label("<<>>")

    def get_menu_alignment(self):
        return MenuAlignment.RIGHT

    def update_menu_item(self, menu_item: Union[ui.Menu, ui.MenuItem], menu_refresh: bool):
        if isinstance(menu_item, ui.MenuItem):
            menu_item.visible = False

    def get_elided_length(self, menu_name):
        if menu_name == "Open Recent":
            return 160
        return 0


self._file_delegate = FileMenuDelegate()
self._file_menu_list = [
    MenuItemDescription(
        name="Menu Example",
        glyph="file.svg",
        onclick_action=("omni.kit.menuext.extension", "menu_example"),
        hotkey=(carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, carb.input.KeyboardInput.T),
    )]

omni.kit.menu.utils.add_menu_items(self._file_menu_list, "File", -10, delegate=self._file_delegate)