PDA

View Full Version : optionMenu help


mccollom73
09-24-2010, 04:34 AM
Hey,

Yes, I am a noob. I have searched the forums, but it hasn't helped me much.

I have 5 commands I want to run, and I know I need an optionMenu. So, when you select one of the 5 commands from the drop down box, then press a button, it runs that.

I have found this, but not much help.

// Create a window with two option menu groups.
string $window = `window -title "Riggit"`;
columnLayout;
// Create a couple of option menu groups.
string $fingers = `optionMenuGrp -label "Fingers"`;
menuItem -label "1";
menuItem -label "2";
menuItem -label "3";
menuItem -label "4";
menuItem -label "5";
optionMenuGrp -l "Toes";
menuItem -label "1";
menuItem -label "2";
menuItem -label "3";
menuItem -label "4";
menuItem -label "5";
showWindow $window;



Like I said, I need a button still, but when you change the numbers and hit the button, I want it to run that specific command.

I can explain things further if need be. Any tutorial or something may help.

Josh

rahulsg2901
09-24-2010, 06:28 AM
I m hoping that u r taking abt UI button not a keyboard button here a new code


// Create a window with two option menu groups.
string $window = `window -title "Riggit"`;
columnLayout;
// Create a couple of option menu groups.
string $fingers = `optionMenuGrp -label "Fingers" fing`;
menuItem -label "1" ;
menuItem -label "2" ;
menuItem -label "3" ;
menuItem -label "4" ;
menuItem -label "5" ;
optionMenuGrp -l "Toes";
menuItem -label "1";
menuItem -label "2";
menuItem -label "3";
menuItem -label "4";
menuItem -label "5";

//Button to run command
button -c buttonCmd;

showWindow $window;


global proc buttonCmd ()
{
//Get current value
string $currentSel = `optionMenuGrp -q -v fing`;

//Define stages for each value
switch ($currentSel) {
case "1":

print "it works! at 1\n";

break;

case "2":

print "it works! at 2\n";

break;

case "3":

print "it works! at 3\n";

break;

case "4":

print "it works! at 4\n";

break;

case "5":

print "it works! at 5\n";

break;

}

};

NaughtyNathan
09-24-2010, 09:52 AM
Rahul has solved it adequately already, but he didn't actually explain how and it may not be immediately obvious to a MEL noob looking at his code. Basically the trick is to give your controls a name and then use this name to query their values later on.

If you give your optionMenuGrp control a fixed name, "fing" in Rahul's example, you can query it at any point in the future and determine it's current label or index, or any other property of it. Although your original code `captured` the control name into the variable $fingers this is not so much use later on in different scopes. Ideally you should hard-code fixed names for any controls that you want to query at some point in a different proc (or inside Maya once the UI is built)

:nathaN

mccollom73
09-24-2010, 05:57 PM
Awesome, thank you to both of you. I'm understanding it a little better.

If I wanted to run a command, would i do it like this?



// Create a window with two option menu groups.
string $window = `window -title "Riggit"`;
columnLayout;
// Create a couple of option menu groups.
string $fingers = `optionMenuGrp -label "Fingers" fing`;
menuItem -label "1" ;
menuItem -label "2" ;
menuItem -label "3" ;
menuItem -label "4" ;
menuItem -label "5" ;

//Button to run command
button -c buttonCmd;

showWindow $window;


global proc buttonCmd ()
{
//Get current value
string $currentSel = `optionMenuGrp -q -v fing`;

//Define stages for each value
switch ($currentSel) {
case "1":

-command "runCmd\n";

break;

case "2":

-command "runCmd\n";


break;

case "3":

print "it works! at 3\n";

break;

case "4":

print "it works! at 4\n";

break;

case "5":

print "it works! at 5\n";

break;

}

};





Or what tag would I use? Thanks.

Josh

rahulsg2901
09-24-2010, 08:06 PM
hey nathan u r right I have to explain something let me try in my way

first of all to assign any command you have determine user choice or query current choice for that you have get active value in optionMenuGrp & UI object name.

you define string $fingers it just a local name if you enter in any other procedure that proc doesn't understand what is $fingers. to define name that can access from any proc you have to give internal name (that is define at the end of the line like in e.g. fing)

now you know the name, find the command that give value in this case this is

string $currentSel = `optionMenuGrp -q -v fing`;

it return what is label of current menu Item. now for each menu Item you have to define a stage or some commands. for that we use switch-case condition (to know more about condition check out Maya help) & define each case like

print "it works! at 1\n";

print "it works! at 2\n";

print "it works! at 3\n"; & so on

now to run your command replace it with this lines

Hope that helps

mccollom73
09-24-2010, 08:14 PM
Thank you all so much. I finally figured it out, and it works great! I'm still a nooby, but I'm learning.

Josh

CGTalk Moderation
09-24-2010, 08:14 PM
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.