Is there any way to use MEL to edit existing menuItems in an optionMenu in a custom gui? I can not find any reference material on how to do this.
Thanks.
Is there any way to use MEL to edit existing menuItems in an optionMenu in a custom gui? I can not find any reference material on how to do this.
Thanks.
an entry in an optionMenu is a menuItem UI control. so its just a normal UI item, and you edit it just as you would any other UI control.
Uh, right...
Let's say you have a simple optionMenu that you are calling:
string $window = window;
columnLayout;
optionMenu -label "Colors";
menuItem -label "Yellow";
menuItem -label "Purple";
menuItem -label "Orange";
showWindow $window;
And you want to programatically change the label for "Orange" to some other word. How would you do it? I can not find any sytactical references to how one calls a menuItem label under an optionMenu to edit it. Any light shed on this would be appreciated. Thanks.
I think your problem is that you aren't naming any of your controls, so you can't (easily) refer back to them. If you do something like this:
menuItem -label "Orange" orangeItem;
(which is naming this control "orangeItem") you can then do this later on:
menuItem -edit -label "Purple" orangeItem;
and your menuItem label should now read "Purple". You might have to name ALL your controls that parent this menuItem and refer to it in it's "pathed" position but I think the easy example above will probably work for you.
If you want to be really clever, you could do it this way:
string $orangeItem = menuItem -label "Orange";
then refer to it like this:
menuItem -edit -label "Purple" $orangeItem;
Unfortunately, I don't have Maya to hand to check this and it's been a while..
but the above should be what you want to know.
:nathaN
Yah - just as I was discovering the same on my own your post came through. Here's my solution:
string $window = window;
columnLayout;
string $colors = optionMenuGrp -label "Colors";
menuItem -label "Yellow";
menuItem -label "Purple";
menuItem -label "Orange";
showWindow $window;
menuItem -parent ($colors + "|OptionMenu") -e -label "Blue" "Orange";
I really like your appending the menu name on the end - that should be just the thing - that's the bit that seems to bite me on the ass when I'm having trouble - that and v5's "wonderous" documentation.
Thanks!
For posterity's sake: don't forget the closing inverted comma on the string eval:
string $orangeItem = menuItem -label "Orange";
mistake? what mistake??
heh, nice spot, thanks..
Is there a way to delete a menuitem from a group as well? I have a optionMenuGrp with items that change based on what is in the scene.
Thanks
ahh...
deleteUI does the trick.
Another question - is there a simple way to clear out all the entries in an optionMenu besides keeping an array of all the menu items and deleting them?
So, for example, let's say I have an optionMenuGrp that lists all the textures. I can't have the system call my functions to add a new menu item each time a texture is created, so let's say I have a "refresh" button on my UI that calls a function that clears out the whole menu and then adds on all the textures.
Is there any way to do this with keeping a global array of all the items added?
A function like "optionMenuGrp -e -clear texture_menu" would be great, but doesn't exist. If there was some way to do something like listing all the children of the texture_menu and then deleting them, that'd be great, too. But I can't seem to find a way to do it.
I'd like to add a function like this to my toolbox for future use:
clear_menu("texture_menu")
there must be some generic way to do this.
you could always just do something like:
CODE
deleteUI optionMenuGrp -q -ill texture\_menu;
it's a bit brutish, but it works.
Thanks! Figured it out, came here to post my solution, and saw yours. Works like a charm.
Ok, here I go again... my UI has several option menus, some of which need to be updated.
Something like meesto suggested:
menuItem -parent ($colors + "|OptionMenu") -e -label "Blue" "Orange";
Doesn't work for me:
$tmenu = optionMenuGrp -label "Texture:" textureMenu;
//
// later, after much code and other menus have been built
//
// clear out the menu...
// deleteUI optionMenuGrp -q -ill $tmenu; - doesn't work for some reason.
//
string $items[] = optionMenuGrp -q -ill $tmenu;
string $scan;
for ($scan in $items) deleteUI $scan;
// rebuild with new textures:
$items = ls -textures;
$tmenu = ($tmenu + "|optionMenu");
for ($scan in $items) menuItem -l $scan -parent $tmenu;
This simply hasn't worked for me... it keeps complaining that "...|textureMenu|optionMenu" (the ellipses for abbreviation only, it contains the full path) doesn't exist...
Well, yes, it exists - but I think the problem is that it's not a menu, which is what menuItem is expecting as a parameter for "-parent". This is another case of MEL and Maya being object based, NOT object oriented. If it were truly object oriented, optionMenu would be a sub-class of of Menu, and through polymorphism would be an acceptable object where a menu is required.
Ok... ultimately, my brute force solution is as follows:
formLayout textureForm
optionMenuGrp -label "Texture:" textureMenu;
setParent ..
//
// later on...
//
deleteUI textureMenu;
optionMenuGrp -label "Texture:" -parent textureForm textureMenu;
$items = ls -textures;
$tmenu = ($tmenu + "|optionMenu");
for ($scan in $items) menuItem -l $scan -parent $tmenu;
By keeping it in it's own little form, I don't have a placement problem. Hey, I could make it worse... I could delete the entire UI and just rebuild it every time some action occurs. There's got to be a better way!