ContextMenuExtension#

class omni.kit.context_menu.scripts.ContextMenuExtension#

Bases: IExt

Context menu core functionality.

Example using viewport mouse event to trigger:

class ContextMenu:
    def on_startup(self):
        # get window event stream
        import omni.kit.viewport_legacy
        viewport_win = get_viewport_interface().get_viewport_window()
        # on_mouse_event called when event dispatched
        self._stage_event_sub = viewport_win.get_mouse_event_stream().create_subscription_to_pop(self.on_mouse_event)

    def on_shutdown(self):
        # remove event
        self._stage_event_sub = None

    def on_mouse_event(self, event):
        import omni.kit.menu.core

        # check its expected event
        if event.type != int(omni.kit.menu.core.MenuEventType.ACTIVATE):
            return

        # get context menu core functionality & check its enabled
        context_menu = omni.kit.context_menu.get_instance()
        if context_menu is None:
            carb.log_error("context_menu is disabled!")
            return

        # get parameters passed by event
        objects = {}
        objects["test_path"] = event.payload["test_path"]
        # setup objects, this is passed to all functions
        objects["test"] = "this is a test"

        # setup menu
        menu_list = [
            # "name" is name shown on menu. (if name is "" then a menu ui.Separator is added. Can be combined with show_fn)
            # "glyph" is icon shown on menu, can use full paths to extensions
            # "name_fn" function to get menu item name
            # "show_fn" function or list of functions used to decide if menu item is shown. All functions must return True to show
            # "enabled_fn" function or list of functions used to decide if menu item is enabled. All functions must return True to be enabled
            # "onclick_fn" function to be called when user clicks menu item
            # "onclick_action" action to be called when user clicks menu item
            # "checked_fn" function returns True/False and shows solid/grey tick
            # "header" as be used with name of "" to use named ui.Separator
            # "populate_fn" a function to be called to populate the menu. Can be combined with show_fn
            # "appear_after" a identifier of menu name. Used by custom menus and will allow custom menu to change order
            # "show_fn_async" this is async function to set items visible flag. These behave differently to show functions as the item will be created regardless and have its
            #   visibility set to False and its upto show_fn_async callback to set the visible flag to True if required

            {"name": "Test Menu", "glyph": "question.svg", "show_fn": [ContextMenu.has_reason_to_show, ContextMenu.has_another_reason_to_show],
                     "onclick_fn": ContextMenu.clear_default_prim },
            {"name": "", "show_fn": ContextMenu.has_another_reason_to_show},
            {"populate_fn": context_menu.show_create_menu},
            {"name": ""},
            {"name": "Copy URL Link", "glyph": "menu_link.svg", "onclick_fn": ContextMenu.copy_prim_url},
        ]

        # add custom menus
        menu_list += omni.kit.context_menu.get_menu_dict("MENU", "")
        menu_list += omni.kit.context_menu.get_menu_dict("MENU", "stagewindow")
        omni.kit.context_menu.reorder_menu_dict(menu_dict)

        # show menu
        context_menu.show_context_menu("stagewindow", objects, menu_list)

    # show_fn functions
    def has_reason_to_show(objects: dict):
        if not "test_path" in objects:
            return False
        return True

    def has_another_reason_to_show(objects: dict):
        if not "test_path" in objects:
            return False
        return True

    def copy_prim_url(objects: dict):
        try:
            import omni.kit.clipboard

            omni.kit.clipboard.copy("My hovercraft is full of eels")
        except ImportError:
            carb.log_warn("Could not import omni.kit.clipboard.")

Methods

__init__()

ContextMenuExtension init function.

bind_material_to_prims_dialog(**kwargs)

[deprecated] Shows bind_material_to_prims_dialog.

build_add_menu(objects, prim_list[, ...])

Builds "Add" context sub-menu items

build_create_menu(objects, prim_list[, ...])

Builds "Create" context sub-menu items

can_assign_material_async(objects, menu_item)

async show function.

can_be_copied(objects)

Checks if prims can be copied :param objects: context_menu data :type objects: dict

can_convert_references_or_payloads(objects)

Checks if references can be converted into payloads or vice versa.

can_delete(objects)

Checks if prims can be deleted :param objects: context_menu data :type objects: dict

can_show_find_in_browser(objects)

Checks if "find" can be shown in browser.

can_use_find_in_browser(objects)

Checks if "find" can be used in browser.

close_menu()

[omni.kit.widget.context_menu bridge function] Close currently open context menu.

convert_payload_to_reference(objects)

Converts selected prims from payload(s) to reference(s).

convert_reference_to_payload(objects)

Converts selected prims from reference(s) to payload(s).

copy_prim_path(objects)

Copy prims path to clipboard

copy_prim_url(objects)

Copies URL of Prim in USDA references format.

create_mesh_prim(objects, prim_type)

Create mesh prims

create_prim(objects, prim_type, attributes)

Create prims

delete_prim(objects[, destructive])

Delete prims

duplicate_prim(objects)

Duplicate prims

find_in_browser(objects)

Select prim in content_browser

get_context_menu()

[omni.kit.widget.context_menu bridge function] Gets current context_menu.

get_prim_group(prim)

If the prim is or has a parent prim that is a group Kind, returns that prim otherwise None

group_selected_prims(objects)

Group prims

has_payload(objects)

Checks if prims have payload.

has_payload_or_reference(objects)

Checks if prims have payloads or references :param objects: context_menu data :type objects: dict

has_reference(objects)

Checks if prims have references.

is_material(objects)

Checks if prims are UsdShade.Material

is_material_bindable(objects)

Checks if prims support material binding.

is_one_prim_selected(objects)

Checks if one prim is selected.

is_prim_in_group(objects)

Checks if any prims are in group(s)

is_prim_selected(objects)

Checks if any prims are selected

menu(*args, **kwargs)

[omni.kit.widget.context_menu bridge function] Creates a menu.

menu_item(*args, **kwargs)

[omni.kit.widget.context_menu bridge function] Creates a menu item.

on_shutdown()

ContextMenuExtension shutdown function.

on_startup(ext_id)

ContextMenuExtension startup function.

prim_is_type(objects, type)

Checks if prims are given class/schema

refresh_payload_or_reference(objects)

Find layers containing prims and triggers reload.

refresh_reference_payload_name(objects)

Checks if prims have references/payload and returns name

select_prims_using_material(objects)

Select stage prims using material

separator([name])

[omni.kit.widget.context_menu bridge function] Creates a ui.Separator named `name`.

show_context_menu(menu_name, objects, menu_list)

[omni.kit.widget.context_menu bridge function] build context menu from menu_list

show_create_menu(objects)

Populate function that builds create menu

show_selected_prims_names(objects[, delegate])

Populate function that builds menu items with selected prim info

ungroup_selected_prims(objects)

Ungroup prims

Attributes

menu_item_count

[omni.kit.widget.context_menu bridge function] Number of items in context menu.

name

[omni.kit.widget.context_menu bridge function] Name of current context menu.

__init__()#

ContextMenuExtension init function.

bind_material_to_prims_dialog(**kwargs)#

[deprecated] Shows bind_material_to_prims_dialog.

Replaced by omni.kit.material.library.bind_material_to_prims_dialog()

build_add_menu(
objects: dict,
prim_list: list,
custom_menu: list | None = None,
delegate=None,
)#

Builds “Add” context sub-menu items

build_create_menu(
objects: dict,
prim_list: list,
custom_menu: dict = [],
delegate=None,
)#

Builds “Create” context sub-menu items

async can_assign_material_async(
objects: dict,
menu_item: Widget,
)#

async show function. The `menu_item` is created but not visible, if this item is shown then `menu_item.visible = True` This scans all the prims in the stage looking for a material, if one is found then it can “assign material” and `menu_item.visible = True`

Parameters:
  • objects (dict) – context_menu data.

  • menu_item – (uiMenuItem): menu item.

can_be_copied(objects: dict)#

Checks if prims can be copied :param objects: context_menu data :type objects: dict

Returns:

True if prims can be copied else False.

Return type:

(bool)

can_convert_references_or_payloads(objects)#

Checks if references can be converted into payloads or vice versa. :param objects: context_menu data :type objects: dict

Returns:

True if prims can be converted from to payload/reference otherwise False.

Return type:

(bool)

can_delete(objects: dict)#

Checks if prims can be deleted :param objects: context_menu data :type objects: dict

Returns:

True if prim can be deleted otherwise False.

Return type:

(bool)

can_show_find_in_browser(objects: dict)#

Checks if “find” can be shown in browser. IE one prim is selected and is authored. :param objects: context_menu data :type objects: dict

Returns:

True if authored and has URL that is saved otherwise False.

Return type:

(bool)

can_use_find_in_browser(objects: dict)#

Checks if “find” can be used in browser.

Parameters:

objects (dict) – context_menu data

Returns:

True if prims are authored else False.

Return type:

(bool)

close_menu()#

[omni.kit.widget.context_menu bridge function] Close currently open context menu. Used by tests not to leave context menu in bad state.

convert_payload_to_reference(objects: dict)#

Converts selected prims from payload(s) to reference(s).

convert_reference_to_payload(objects: dict)#

Converts selected prims from reference(s) to payload(s).

copy_prim_path(objects: dict) None#

Copy prims path to clipboard

Parameters:

objects – context_menu data

copy_prim_url(objects: dict)#

Copies URL of Prim in USDA references format. @planet.usda@</Planet>

static create_mesh_prim(
objects: dict,
prim_type: str,
) None#

Create mesh prims

Parameters:
  • objects – context_menu data

  • prim_type – created prim’s type

static create_prim(
objects: dict,
prim_type: str,
attributes: dict,
create_group_xform: bool = False,
) None#

Create prims

Parameters:
  • objects – context_menu data

  • prim_type – created prim’s type

  • attributes – created prim’s custom attributes

  • create_group_xform – passed to CreatePrimWithDefaultXformCommand

delete_prim(objects: dict, destructive=False)#

Delete prims

Parameters:
  • objects – context_menu data

  • destructive – If it’s true, it will remove all corresponding prims in all layers. Otherwise, it will deactivate the prim in current edit target if its def is not in the current edit target. By default, it will be non-destructive.

duplicate_prim(objects: dict)#

Duplicate prims

Parameters:

objects – context_menu data

find_in_browser(objects: dict) None#

Select prim in content_browser

Parameters:

objects – context_menu data

get_context_menu()#

[omni.kit.widget.context_menu bridge function] Gets current context_menu.

Returns:

Current context_menu.

Return type:

(str)

get_prim_group(prim)#

If the prim is or has a parent prim that is a group Kind, returns that prim otherwise None

Parameters:

prim (Usd.Prim) – prim to get group from.

Returns:

(Usd.Prim) Group prim or None.

group_selected_prims(objects: dict)#

Group prims

Parameters:

prims – list of prims

has_payload(objects: dict)#

Checks if prims have payload.

Parameters:

objects (dict) – context_menu data

Returns:

True if prims has payload otherwise False.

Return type:

(bool)

has_payload_or_reference(objects: dict)#

Checks if prims have payloads or references :param objects: context_menu data :type objects: dict

Returns:

True if prims have payload or references otherwise False.

Return type:

(bool)

has_reference(objects: dict)#

Checks if prims have references. :param objects: context_menu data :type objects: dict

Returns:

True if prims have reference otherwise False.

Return type:

(bool)

is_material(objects: dict)#

Checks if prims are UsdShade.Material

Parameters:

objects (dict) – context_menu data

Returns:

True if prim is UsdShade.Material else False.

Return type:

(bool)

is_material_bindable(objects: dict)#

Checks if prims support material binding.

Parameters:

objects (dict) – context_menu data

Returns:

True if prims can have bound materials otherwise False.

Return type:

(bool)

is_one_prim_selected(objects: dict)#

Checks if one prim is selected.

Parameters:

objects (dict) – context_menu data

Returns:

True if one prim is selected otherwise False.

Return type:

(bool)

is_prim_in_group(objects: dict)#

Checks if any prims are in group(s)

Parameters:

objects (dict) – context_menu data

Returns:

True if prims are part of group otherwise False.

Return type:

(bool)

is_prim_selected(objects: dict)#

Checks if any prims are selected

Parameters:

objects (dict) – context_menu data

Returns:

True if one or more prim is selected otherwise False.

Return type:

(bool)

menu(*args, **kwargs)#

[omni.kit.widget.context_menu bridge function] Creates a menu.

Parameters:
  • name (str) – Name of the menu.

  • delegate (ui.MenuDelegate) – Specify the delegate to create a custom menu. Optional.

  • glyph (str) – Path of the glyph image to show before the menu name. Optional.

  • submenu (bool) – Enables the submenu marker. Optional.

  • tearable (bool) – The ability to tear the window off. Optional.

Returns:

Menu item created.

Return type:

(uiMenu)

menu_item(*args, **kwargs)#

[omni.kit.widget.context_menu bridge function] Creates a menu item.

Parameters:
  • name (str) – Name of the menu item.

  • triggered_fn (Callable) – Function to call when menu item is clicked. Optional.

  • enabled (bool) – Enable the menu item. Optional.

  • checkable (bool) – This property holds whether this menu item is checkable. A checkable item is one which has an on/off state. Optional.

  • checked (bool) – This property holds a flag that specifies the widget has to use eChecked state of the style. It’s on the Widget level because the button can have sub-widgets that are also should be checked. Optional.

  • is_async_func (bool) – Optional.

  • delegate (ui.MenuDelegate) – Specify the delegate to create a custom menu. Optional.

  • additional_kwargs (dict) – Additional keyword arguments to pass to ui.MenuItem. Optional.

  • glyph (str) – Path of the glyph image to show before the menu name. Optional.

Returns:

Menu item created.

Return type:

(uiMenuItem)

on_shutdown()#

ContextMenuExtension shutdown function.

on_startup(ext_id)#

ContextMenuExtension startup function.

Parameters:

ext_id (str) – Extension identifier.

prim_is_type(
objects: dict,
type: Type,
) bool#

Checks if prims are given class/schema

Parameters:
  • objects (dict) – context_menu data.

  • type (Tf.Type) – prim type to check.

Returns:

True if prims are of type `type` otherwise False.

Return type:

(bool)

refresh_payload_or_reference(objects: dict)#

Find layers containing prims and triggers reload.

refresh_reference_payload_name(objects: dict)#

Checks if prims have references/payload and returns name

Returns:

Name of payload/reference or None

Return type:

(str)

select_prims_using_material(objects: dict)#

Select stage prims using material

Parameters:

objects – context_menu data

separator(name: str = '') bool#

[omni.kit.widget.context_menu bridge function] Creates a ui.Separator named `name`.

Parameters:

name (str) – Name of the menu separator. Optional.

show_context_menu(
menu_name: str,
objects: dict,
menu_list: List[dict],
min_menu_entries: int = 1,
**kwargs,
) None#

[omni.kit.widget.context_menu bridge function] build context menu from menu_list

Parameters:
  • menu_name – menu name

  • objects – context_menu data

  • menu_list – list of dictionaries containing context menu values

  • min_menu_entries – minimal number of menu needed for menu to be visible

show_create_menu(objects: dict)#

Populate function that builds create menu

Parameters:

objects – context_menu data

show_selected_prims_names(
objects: dict,
delegate=None,
) None#

Populate function that builds menu items with selected prim info

Parameters:

objects – context_menu data

ungroup_selected_prims(objects: dict)#

Ungroup prims

Parameters:

prims – list of prims

property menu_item_count: int#

[omni.kit.widget.context_menu bridge function] Number of items in context menu.

Returns:

number of menu items.

Return type:

(int)

property name: str#

[omni.kit.widget.context_menu bridge function] Name of current context menu.

Returns:

Name of current context menu.

Return type:

(str)