Outcome

One hotkey can open a specialized menu in Edit Mesh mode and a different menu in Object mode, while retaining a safe fallback for everything else.

The add-on includes a context-sensitive-menu example, but an explicit Command is the best starting point when the routing has only a few branches:

open_menu("Toolset: Edit") if C.mode == "EDIT_MESH" else open_menu("Toolset: Object")

Recipe

  1. Create the target menus first: for example, Toolset: Edit, Toolset: Mesh, and Toolset: Any Object.
  2. Decide the precedence. Mode should generally beat object type: EDIT_MESH is more specific than MESH.
  3. Add the routing Command to the hotkey or parent menu.
  4. End with a fallback that is valid in the remaining contexts.
open_menu("Toolset: Edit") if C.mode == "EDIT_MESH" else \
open_menu("Toolset: Mesh") if C.object and C.object.type == "MESH" else \
open_menu("Toolset: Any Object")

For a larger family of menus, use PME’s bundled context-sensitive-menu pattern. It tries candidate names in order: a selection-specific name, then mode, then object type, then Any Object; no-object use is handled separately by None Object.

Pitfalls

  • Do not route solely by C.object.mode. C.mode distinguishes Blender contexts such as Edit Mesh and Edit Armature more precisely.
  • A missing fallback turns an ordinary unsupported context into an error path. Always decide what no selection and no matching menu should do.
  • Keep the naming convention in one place. If names are free-form, a small explicit conditional is safer than a large name-driven router.

PME 2.1’s bundled command_context_sensitive_menu.py example uses the ordered selection, mode, type, and fallback pattern above.

Sources