You've written a menu for the Maya UI in Python.
Now if you want to add a Python menu item to a Shelf, you'll find that the Ctrl+Shift combination adds it as a MEL command instead, and your item won't run from the Shelf.
In this post I'll show you how to modify the MEL procedure: menuItemToShelf to accommodate Python commands.
Open the script menuItemToShelf.mel in your favorite text editor, and save a copy in your user/maya/scripts directory.
Look at the last few lines, where the shelfButton command is used to build your new Shelf button.
// create the shelfButton
//
shelfButton
-command $command
-doubleClickCommand $doubleClickCommand
-image1 $image
-imageOverlayLabel $imageOverlayLabel
-label `menuItem -q -label $itemName`
-style `shelfLayout -q -style $currentShelf`
-width `shelfLayout -q -cellWidth $currentShelf`
-height `shelfLayout -q -cellHeight $currentShelf`
-ann `menuItem -q -ann $itemName`;
// -style "iconOnly";
The Docs page for shelfButton mentions a flag for specifying whether the script should be considered MEL or Python: -sourceType that takes one of two values: "mel" or "python".
The Docs go on to say that "mel" is enabled by default, that is, when the flag is not explicitly set.
Well, the workaround is straightforward enough: since the script assumes that my menu item is a MEL command, we'll interrupt the script before the shelf button is created to ask the user: is this a MEL or a Python command ?
Putting a simple confirmDialog before the shelfButton command does the trick:
-ma "left" -message "Save menu item to shelf as type:"
-button "MEL" -button "Python" -defaultButton "MEL"
-cancelButton "MEL" -dismissString "MEL" `;
And use the value of $sourceType to set the flag -sourceType inside the shelfButton command.
You could follow that by checking the value of $sourceType to determine whether to use the appropriate Python shelf icon:
$image = "pythonFamily.xpm";
// create the shelfButton
//
shelfButton
...
-sourceType $sourceType;
You can get the full script here.
Save it in your scripts directory, for example: C:/Users/burgeso/Documents/maya/2010-x64/scripts
Launch Maya and away you go....
Cheers,
Owen
Comments
You can follow this conversation by subscribing to the comment feed for this post.