View Full Version : adding item to menu -- clueless

05 May 2003, 01:19 AM
I am (a noob) trying to add an item to the "Edit Polygons" menu and am at a dead end. The code below adds an item but deletes all the original items. I sure it's something simple that I just can't see or don't know. It actually worked a couple of times in a slightly different form but I didn't save and believe this to be equivalent. What's really perplexing is that I can run this repeatedly (changing the item name) and the new items are appended as I expected. I would really appreciate a clue.

menuItem -p "mainEditPolygonsMenu" "TestItem4" ;
menuItem -p "mainEditPolygonsMenu" -ob true ;

05 May 2003, 01:42 AM
Here's a great little tutorial about menus:


05 May 2003, 05:21 AM

I can't get that code to delete all the menu items. It just adds the new item at the bottom.

You don't need to set the parent for each step btw... here's another way to do it that may work better for you:

setParent -menu "mainEditPolygonsMenu";
menuItem "TestItem4";
menuItem -ob true ;

-- Mark

05 May 2003, 12:06 PM
That didn't fix it but a double thanks for the help anyway. I was able to get the internal name of the menu only with the help of your book.

More hacking: Really bizarre is that if I execute this:

setParent -menu "mainPolygonsMenu" ;
menuItem "test5" ;

And then this.

setParent -menu "mainEditPolygonsMenu" ;
menuItem "test5" ;

Polygons menu is truncated but EditPolygons is appended. However if I put the code in a function calls all is right. I'm expecting my upgrade to 5.0 any day so I guess I'll stop pulling out my hair till I install that and see if it goes away.

BTW I assume that there is (internally) a global pointer to the current active parent and that the only difference between

setParent -menu "mainEditPolygonsMenu" ;
menuItem "test5" ;


menuItem -p "mainEditPolygonsMenu" "test5" ;

is that the latter does not change the global pointer?

And thanks again.

05 May 2003, 12:37 PM
And thanks, tropistic. I had been wondering about how to integrate this menu item addition into a plugin and your reference did it for me.

05 May 2003, 02:20 PM
More hacking: The problem returned even with the function call. But it seems to be with loading and running "Blue MEL Studio" I had tried running the code in the Maya Script Editor and had the same problem so I concluded that BMS was OK. But apparently not, for when I do not run and load it there is no problem with my code.

Edit: Arrg! An hour later and it's back. So it's not Blue MEL Studio. I'm almost out of hair to pull on. :cry:

05 May 2003, 05:28 PM
I have tryed to run the MEL code you posted at the beginning of this tread...

If you want to add a new menuItem to an existing (system) menu you can't add it in that way...
You say that sometimes it works and some times it' doesn't..

Try to do this:
Open a new Instance of Maya.. Go to the menu "EditPolygons" and look at the existing menuItems.... Obviously your new item does not exist, does it?
Now try to type your Mel code that adds the item.. now look at the menu... The new menuItem should appear!

Try to do this again:
Open a fresh copy of Maya and whitout opening the menu "EditPolygons" try to add the new menuItem... Now look at the menu. The menu will show only your item... incredible!!

Maya builds a menu the first time you open it. The function that buids the menu checks if the menu is empty or not: if the menu contains already an item, the function skips the building process.
This is the reason why when you add the menuItem to the menu before open it at least once, the menu will show only your menuItem.

To correctly add a new menuItem try this (it's not so easy..):

This is the source code for the script that add/remove a menuItem


global proc string ScriptThatAddYourMenuItem()
// Description:
// A proc to add a new menuItem to an exiting menu.
global int $gYourMenuItemAddTest;
if ($gYourMenuItemAddTest == 0) // test if the new item already exist
menuItem -l "New Item..." -c "yourCommand" yourMenuItemUniqueName;

$gYourMenuItemAddTest = 1;
return "ScriptThatRemoveYourMenuItem()";

global proc addYourNewMenu()
global int $gYourMenuItemAddTest; // this is a global var, so changge the name every time
global string $gStrYourMenuItem = ""; // this is a global var, so changge the name every time
$gYourMenuItemAddTest = 0;
addMenuItemSafe("mainEditPolygonsMenu", "ScriptThatAddYourMenuItem", "gStrYourMenuItem");


global proc removeYourNewMenu()
global string $gStrYourMenuItem = ""; // this is a global var, so changge the name every time
removeMenuItemSafe("mainEditPolygonsMenu", "gStrYourMenuItem");

global proc ScriptThatRemoveYourMenuItem()
global int $gYourMenuItemAddTest;
if ($gYourMenuItemAddTest == 1)
deleteUI -mi yourMenuItemUniqueName;


source this script and than call "addYourNewMenu()" to add the menuItem and "removeYourNewMenu()" to remove it...

If you need more axpaination, ask me!! :thumbsup:


05 May 2003, 06:09 PM
Hmm. This is interesting -- whenever I add my own menu items I always make my own menu, so I don't run into this.

Anyway, it's worth noting (as the very informative previous post didn't quite state, but implied) that addMenuItemSafe and removeMenuItemSafe are functions that are part of the Maya distribution.

-- Mark

05 May 2003, 06:54 PM
Thank you Max, thank you!

I was really getting discouraged and begining to wonder if I was too old to learn another language.


And Mark, another thing to point out is that these functions are not in the *%!^ing fine MEL Commands Help.

05 May 2003, 07:50 PM
Hi Max,

There is one thing (ha ha) I don't understand here. It's


This is not in quotes, it's not a variable, what type of thing is it?


05 May 2003, 07:54 PM
It's just the object name for the new menu you want to create. Quotes are optional for names like this.

-- Mark

05 May 2003, 09:55 PM
Thanks Mark!


Interestingly, the function,


is never called in the code that is in our lexical scope. It appears that Maya internally derives this symbol from


The requirement for proper derivation is that the two names be identical except for Add and Remove.

05 May 2003, 08:14 AM
The requirement for proper derivation is that the two names be identical except for Add and Remove.

... it isen't so..

Look at the end of global proc ScriptThatAddYourMenuItem()..
the first function declared in the script body...
This function return a string like this:

return "ScriptThatRemoveYourMenuItem()";...

You can name this script as you want.. I called they this way for better expain.


PS: yourMenuItemUniqueName is just the name of the UI item.. like the name of an object.

05 May 2003, 12:14 PM
Ayup! I must be getting senile.

CGTalk Moderation
01 January 2006, 02:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.