Skip to content

API Reference

MenuSortKey = Callable[
    [Menu | Action], "SupportsRichComparison"
]

Mode.iter sort key function.

Must be a function that accepts a Menu or Action argument that is used to extract a comparison key from each element.

Runtime action definition.

Parameters:

Name Type Description Default
id str

Action identifier.

Identifier must abide by these rules:

  • Must be unique in its Model
  • The first and last characters must be ascii lowercase [a-z]
  • Other characters must be ascii lowercase or dash [a-z-]
required
cb Callable[[], Any]

Callback to execute when action is requested.

<function passthrough at 0x7f9dcfcf47d0>
menu tuple[str, ...]

Menu labels hierarchy.

The root menu is represented by an empty tuple ().

()
label str | None

Display name.

None
group str | None

A group under menu.

Items under the same menu can be grouped together. Groups are represented by separators.

None
icon Traversable | None

Path to an icon.

Icons are displayed alongside the label, if supported by the menu builder.

None
desc str | None

Short description.

Displayed as a menu tooltip.

None
deserialize(config: dict[str, Any]) -> Self

Deserialize config into a new instance.

Action wrapper, returned by Model.iter.

Parameters:

Name Type Description Default
inner ForwardRef
None
menu: tuple[str, ...]
path: tuple[str, ...]

Group wrapper, returned by Model.iter.

Parameters:

Name Type Description Default
inner ForwardRef
None
menu ForwardRef
None
path: tuple[str, ...]

Menu wrapper, returned by Model.iter.

Parameters:

Name Type Description Default
inner ForwardRef
None
menu: tuple[str, ...]
path: tuple[str, ...]

Menu node definition.

Parameters:

Name Type Description Default
label str

Display name.

required
menu tuple[str, ...]

Menu labels hierarchy.

The root menu is represented by an empty tuple ().

()
group str | None

A group under menu.

Items under the same menu can be grouped together. Groups are represented by menu separators.

None
icon Traversable | None

Path to an icon.

Icons are displayed alongside the label, if supported by the menu builder.

None
desc str | None

Short description.

Displayed as a menu tooltip.

None
deserialize(config: dict[str, Any]) -> Self

Deserialize config into a new instance.

is_configured() -> bool

Whether this menu has any configuration beside menu and label.

Model()

The main Action and Menu storage abstraction.

add_action(action: Action) -> None

Add action to model.

add_menu(menu: Menu) -> None

Add menu to model.

get_action(id: str) -> Action

Return Action with given id in model.

Raises:

Type Description
KeyError

id not found.

iter(
    menu: tuple[str, ...] = (),
    *,
    sort_key: MenuSortKey | None = None,
    recursive: bool = False
) -> Iterator[ItemGroup | ItemMenu | ItemAction]

Iter menu items.

By default, menu items are sorted as follow:

  1. Sort groups, alphabetically
  2. In the current group, place menus above actions
  3. Sort elements alphabetically

Parameters:

Name Type Description Default
menu tuple[str, ...]

The start menu. An empty tuple () (the default), starts at the top.

()
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
recursive bool

Iter sub-menus in depth-first search.

False
deserialize(
    config: dict[str, Any],
    model: Model,
    root_keys: tuple[str, ...] | None = None,
) -> None

Deserialize config and add its content to the model.

Parameters:

Name Type Description Default
config dict[str, Any]

A dict containing a configuration of menus and actions.

required
model Model

Target model.

required
root_keys tuple[str, ...] | None

The sequence of keys that lead to the root configuration structure. For example, a [tool.myapp.mymenu] table (("tool", "myapp", "mymenu")) in pyproject.toml.

None
load(
    fp: SupportsRead[bytes],
    model: Model,
    root_keys: tuple[str, ...] | None = None,
) -> None

Load fp and deserialize its content to model.

Parameters:

Name Type Description Default
fp SupportsRead[bytes]

a .read()-supporting file-like object containing a TOML document.

required
model Model

Target model.

required
root_keys tuple[str, ...] | None

The sequence of keys that lead to the root configuration structure. For example, a [tool.myapp.mymenu] table (("tool", "myapp", "mymenu")) in pyproject.toml.

None
loads(
    s: str,
    model: Model,
    root_keys: tuple[str, ...] | None = None,
) -> None

Load s and deserialize its content to model.

Parameters:

Name Type Description Default
s str

a str containing a TOML document.

required
model Model

Target model.

required
root_keys tuple[str, ...] | None

The sequence of keys that lead to the root configuration structure. For example, a [tool.myapp.mymenu] table (("tool", "myapp", "mymenu")) in pyproject.toml.

None

TextMenuBuilder render options.

ASCII = _Render(
    child="|-- ",
    last_child="`-- ",
    menu="|   ",
    empty="    ",
    group_suffix=" ---",
)
UTF8 = _Render(
    child="├── ",
    last_child="└── ",
    menu="│   ",
    empty="    ",
    group_suffix=" ───",
)
TextMenuBuilder(
    model: Model,
    *,
    root_menu: str | None = None,
    sort_key: MenuSortKey | None = None,
    render: Render = Render.ASCII
)

Text menu builder.

Parameters:

Name Type Description Default
model Model

Model to build.

required
root_menu str | None

Specify a root menu name. If not, the model items whose parent is an empty tuple () are printed at the root.

None
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
render Render

Text render option.

Render.ASCII
build() -> str

Build menu.

QMenuBuilder(
    model: Model,
    *,
    root_menu: QtWidgets.QMenu | str,
    sort_key: MenuSortKey | None = None
)

Qt Menu Builder.

Parameters:

Name Type Description Default
model Model

Model to build.

required
root_menu QtWidgets.QMenu | str

Menu to populate. All existing actions in the menu are removed during build. If a str is provided, a new QMenu will be created.

required
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
build() -> QtWidgets.QMenu

Build menu.

Added in 1.1.0

BlenderMenuBuilder(
    model: Model,
    *,
    root_menu: str,
    sort_key: MenuSortKey | None = None
)

Blender Menu Builder.

Parameters:

Name Type Description Default
model Model

Model to build.

required
root_menu str

Root menu name.

required
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
build() -> type[bpy.types.Menu]

Build menu.

find_menu(
    path: tuple[str, ...],
) -> type[bpy.types.Menu] | None

Find Blender menu created by the build method.

find_operator(id: str) -> type[bpy.types.Operator] | None

Find Blender operator created by the build method.

unregister() -> None

Unregister all operators and menus registered by the last build call.

Added in 1.2.0

MayaMenuBuilder(
    model: Model,
    *,
    root_menu: str,
    parent: str = "MayaWindow",
    sort_key: MenuSortKey | None = None
)

Maya Menu Builder.

Parameters:

Name Type Description Default
model Model

Model to build.

required
root_menu str

Root menu name.

required
parent str

Specify the window or menu that the menu will appear in. Default to Maya main menubar.

'MayaWindow'
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
build() -> None

Build menu.

delete() -> None

Delete menu if it exist.

Added in 1.3.0

UnrealMenuBuilder(
    model: Model,
    *,
    parent: unreal.ToolMenu,
    root_name: str,
    root_label: str | None = None,
    to_string_command: Callable[[str], str],
    sort_key: MenuSortKey | None = None
)

Unreal Menu Builder.

Parameters:

Name Type Description Default
model Model

Model to build.

required
parent unreal.ToolMenu

Parent menu.

required
root_name str

Root menu name. Serve as the 'owner' name of all sub-menu and entries created by the builder and used as an identifier during build.

required
root_label str | None

Root menu display name. Default to root_name.

None
to_string_command Callable[[str], str]

Callable that accept an Action.id and return an executable string. The string script should execute the Action.cb. Unreal menu entry expect a string command.

required
sort_key MenuSortKey | None

Customize the sort order of menu items.

None
build() -> unreal.ToolMenu

Build menu.

unregister() -> None

Unregister menu, if it exist.

model_reference_to_string_command(
    reference: str,
) -> Callable[[str], str]

Generate the to_string_command argument from a Model reference.

Parameters:

Name Type Description Default
reference str

Points to a Callable returning a Model in the form importable.module:function.

required
Example
>>> func = model_reference_to_string_command('menuet.demo:demo_model')
>>> print(func('anim-bake'))
import menuet
from menuet.utils import load_entry_point
model = load_entry_point('menuet.demo:demo_model')
if not isinstance(model, menuet.Model):
    raise TypeError(f"Expected 'Model', found '{type(model)}'")
model.get_action('anim-bake').cb()

Added in 1.1.0

demo_model() -> Model

An example model to demonstrate what menuet can do.

Example
>>> from menuet.demo import demo_model
>>> from menuet.builders.text import Render, TextMenuBuilder
>>> model = demo_model()
>>> builder = TextMenuBuilder(model, root_menu="Demo", render=Render.UTF8)
>>> print(builder.build())
Demo
├── Animation
   ├── FBX
      ├── FBX Animation Exporter
      └── FBX Animation Importer
   ├── Bake Animation
   ├── Edit ───
   ├── Adjustment Blending
   └── Tween Machine
├── Development
   └── Start Debugger
├── Modeling
   ├── Mesh Cleaner
   ├── Mesh Randomizer
   └── Mirror Geometry
├── Rigging
   ├── Joint Tools
   ├── Skinning Tools
   ├── Controller ───
   ├── Controller Creator
   └── Controller Editor
└── Open Documentation