MEL How-To #77

Back · Previous · Next Maya

How do I create a popup or Marking Menu that activates via mouse button but only when a hotkey is pressed?

The key for generating a popup menu is in defining its parent control. It is the parent control which defines when the popup menu is active and can be made visible. Looking at Maya's own scripts reveals its use of the ‘viewPanes’ layout as the parent for its Marking Menus. This is the paneLayout Maya uses for its model views.

setParent viewPanes;
// Result: MayaWindow|mayaMainWindowForm|formLayout3|viewPanes //

If you are using your popup menu as an interactive modeling tool then this is a good choice.

Note: Controls can be assigned more than one popup menu, each bound to a different mouse button and key qualifier.

When assigning a hotkey to a popup menu you must assign two actions: one for Press and one for Release. The Press action is typically the name of the MEL procedure that builds the menu:
buildMyMM;
The Release action is typically:
if ( `popupMenu -exists tempMM` ) { deleteUI tempMM; }

Note: It's a good idea to use the same name for all popup menus so only one is visible at a time. (Maya uses "tempMM".)

Example Marking Menu

Example Marking Menu

global proc doMyMM()
{
  // Precaution in case tempMM was not destroyed
  // since its last invokation.
  popupMenu -e -deleteAllItems tempMM;

  // Since this is a Marking Menu the -radialPosition
  // flag specifies menuItem's position on the menu.
  menuItem -radialPosition "N" -l "Delete History"
    -c "delete -ch";

  menuItem -rp "E" -l "Freeze Transformations"
    -c "makeIdentity -apply true";

  menuItem -rp "S" -l "Delete All History"
    -c "delete -all -ch";

  menuItem -rp "W" -l "Center Pivot"
    -c "xform -cp";

  // "Pop" the menu parent up one level.
  setParent -m ..;
}

global proc buildMyMM()
// Creates a marking menu that allows the user to control
// History and pivot options.
{
  // Reuse the name tempMM for the name of the menu, to
  // ensure that there's only one of these at any one time.
  if( `popupMenu -exists tempMM` )
  {
      deleteUI tempMM;
  }

  // Create the popupMenu.
  // Specify '-markingMenu true' to indicate a Marking Menu.
  // Use 'viewPanes' as the parent layout so it is active within the model views.
  popupMenu -alt 0 -markingMenu true -b 1 -aob 1 -p viewPanes -pmc "doMyMM" tempMM;
}

Press: buildMyMM;

Release: if ( `popupMenu -exists tempMM` ) { deleteUI tempMM; }

Example Popup Menu

Example popup menu

global proc doMyPopupMenu()
{
  // Precaution in case tempMM was not destroyed
  // since its last invokation.
  popupMenu -e -deleteAllItems tempMM;

  menuItem -l "Delete History"
    -c "delete -ch";

  menuItem -l "Freeze Transformations"
    -c "makeIdentity -apply true";

  menuItem -l "Delete All History"
    -c "delete -all -ch";

  menuItem -l "Center Pivot"
    -c "xform -cp";

  // "Pop" the menu parent up one level.
  setParent -m ..;
}

global proc buildMyPopupMenu()
{
  // Reuse the name tempMM for the name of the menu, to
  // ensure that there's only one of these at any one time.
  if( `popupMenu -exists tempMM` )
  {
      deleteUI tempMM;
  }

  // Create the popupMenu.
  // Specify '-markingMenu false'.
  // Use 'viewPanes' as the parent layout so it is active within the model views.
  popupMenu -alt 0 -markingMenu false -b 1 -aob 1 -p viewPanes -pmc "doMyPopupMenu" tempMM;
}

Press: buildMyPopupMenu;

Release: if ( `popupMenu -exists tempMM` ) { deleteUI tempMM; }

Wednesday, October 24, 2001