Updated 24/3/2003

Tutorial: Creating Custom Maya Menus

Maya has an incredibly open architecture, which allows users to customise and expand it in many ways. One of these ways is to allow the addition of new menus or menu items under Maya's own main menu. This is no trivial matter -- new tools that you write or download can be fully integrated into Maya's interface, making them easily available for everyone on your project or in your company.

Creating Menus

The command for creating a new menu is (funnily enough) menu, but before you call it you need to tell Maya what the parent of the menu should be, i.e. where it should be inserted. For example:

  global string $gMainWindow;
  setParent $gMainWindow;
  menu -label "My Menu" myNewMainMenu;

If you copy and paste that into the script editor in Maya and execute it, you will now see a new (but very empty) menu in Maya's main menu bar. The global string $gMainWindow tells us the name of the Maya window, and we set that to be our parent before creating the new menu. You can insert a new menu into any existing Maya window if you know the correct path to it. Note I have given the new menu a nice long and hopefully unique name, so we can easily refer to it later when adding menu items to it, or if we need to delete it.

Adding Menu Items

You add new menu items with the menuItem MEL command. This command can add individual menu items as well as new sub-menus with further menu items underneath. We'll now add a few menu items and a sub-menu to execute some common Maya functions:

  setParent -menu myNewMainMenu;
  menuItem -label "List Attributes" -command "print(`listAttr`)";
  menuItem -subMenu true -tearOff true -label "Make Me A..." myMakeMeAMenu;
      menuItem -label "Sphere"  -command sphere;
      menuItem -label "Locator" -command CreateLocator;
  setParent -menu ..;
  menuItem -divider true;
  menuItem -label "Show Help for 'menu' MEL Command"     -command "help -doc menu";
  menuItem -label "Show Help for 'menuItem' MEL Command" -command "help -doc menuItem";

Analysing the code above, we first use the setParent command to tell Maya where we want to start inserting things. Then we use menuItem calls specifying the label and command to run. We have also made a submenu which we have said can be torn-off and floated over the Maya window for easy access (this would be handy in this case if you wanted to quickly make a lot of spheres and locators in a hurry... you never know!

When we're finished making the submenu, we use a setParent ..; command to go 'back' one parent, otherwise the last items we add will still be under the submenu instead of the new main menu. I've also slipped in a divider to separate the last two help items from the rest. Note the help -doc command used by the last two menu items - this is a very helpful MEL command to know!

Restricting Menus to an Interface Mode

By default, when you add a new menu to Maya it is visible all the time. However, most of Maya's menus change when you move between the different interface modes (Animation, Modelling, Rendering, etc), and you can restrict your new menu to only be shown in a particular mode.

To do this, you simply need to add the name of your menu (the actual name, not the menu label) to an array that Maya uses for this purpose. For example, to make our new menu only show up on Animation mode, run the following code:

  global string $gAnimationMenus[];
  $gAnimationMenus[size($gAnimationMenus)] = "myNewMainMenu";
  updateMenuModeUI;

We're simply adding the name of our new menu to the end of a string array that Maya looks for when organising its menus. After doing this Maya will hide the new menu except when you are in the Animation interface mode. For the other interface modes there are corresponding global arrays (e.g. $gRenderingMenus or $gModelingMenus).

Making a New Menu Permanent

Running the commands above will create the menu for the current Maya session, but it will disappear when you close Maya. In order to make the changes permanent, you must put the commands into your userSetup.mel script file, which is usually kept under your home account (or Windows equivalent). If you have one already you can find where it is from within Maya by running whatIs userSetup. Consult the Maya docs for more help on the userSetup.mel file.

Adding Menus for Plugins

If you're wanting to add a menu for a new plugin you've written, then you should write a small script to create the menu, and one to remove it again. Then, in your initializePlugin() function you can specify the name of the script to run when the plugin is loaded (to create the menu) and the script to run when the plugin is unloaded (to remove the menu), e.g.:

  MStatus initializePlugin(MObject obj)
  {
    MStatus status;
    MFnPlugin plugin(obj, "MyPlugin", "1.01", "Any");
    status = plugin.registerUI("makeMyCustomUI", "removeMyCustomUI");
    return status;
  }

Using this mechanism, your interface will always be in sync with your plugin being loaded.

Tips

Here are some general tips for using custom menus successfully:


That should get you started, if you have any further queries feel free to email me.



Return to the Tutorials page