MEL optionMenu. How to use Current value?

MEL optionMenu. How to use Current value?

peter_baehr97
Contributor Contributor
914 Views
2 Replies
Message 1 of 3

MEL optionMenu. How to use Current value?

peter_baehr97
Contributor
Contributor

I am currently trying to use the output of a menuOption dropdown menu to determine what Object i want to create when i press a button. The window, the menu and the button seem to be working in general, but the procedure of the button is not correct yet. It seems i don't correctly query what i want to or i have problems in my variable definition/declaration. Here is the script

(I use polyobjects instead of NURBS objects for testing. Don't get confused by that)

 

//Window deletion, followed up by window creation
if ( `window -exists MyRiggingScript` )
{
deleteUI MyRigging;
}
window -wh 300 500 MyRiggingScript;

//Layout
columnLayout;

//Variable Declaration
global string $NURBSoption = "Shape";


// Menu creation
optionMenu -label "NURBS-Shape" $NURBSoption;
menuItem -label "Circle";
menuItem -label "Square";
menuItem -label "Triangle";


//Button and Procedure:
button -w 110 -h 35 -label "Create Shape" -command "createShape()" MyButton1;

proc createShape()
{
global string $NURBSoption;
string $NURBSshape = `optionMenu -q -value $NURBSoption` ;
if ( `$NURBSshape = "Circle"` ) polySphere;
if ( `$NURBSshape = "Square"` ) polyCube;
if ( `$NURBSshape = "Triangle"` ) polyCone;
}

// Show final window
showWindow MyRiggingScript;

 

 

 

When I perform the script i get Syntax errors for the if-lines and also one Error for the line :

optionMenu -label "NURBS-Shape" $NURBSoption;

 

I can't figure out whats the problem here. I am appreciating any kind of help!

0 Likes
Accepted solutions (1)
915 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

Your logic is correct, your Syntax isn't. A Syntax error basically means that your program isn't written in the conventions of the language you are using.
In general "condition statements" in MEL need to be enclosed in curly brackets {}, this goes for If-statements, loops, procedures and classes. Generally everything that goes "When I give you this" -> "do that".

 

Also you declared your If statements like you would declare variables. The backticks `` in MEL basically mean "Store this value", so they need to be used to tell Maya what goes into a variable, but they are not used when comparing the variable to something else in an if statement.

The last thing is that a single equal sign = means "I declare the left side to be the value of the right side", so it is used in variable declaration, while a double equal sign == means "I want to compare the left side to the right side". This one is a bit confusing for newby coders but it works the same in pretty much every popular language so it's a good one to know.

 

I know this explanation was a bit wordy, but I thought it was important to explain those basics here, because this will make the changes I did to your code make sense. Also because I think that understanding the different signs as giving verbal commands to the program can help to avoid/find syntax errors.

 

Here is how your if statements look when done with correct Syntax:

string $NURBSshape = `optionMenu -q -value $NURBSoption` ;
if ( $NURBSshape == "Circle" ) {
polySphere;   
}
if ( $NURBSshape == "Square" ){
polyCube;
} 
if ( $NURBSshape == "Triangle" ){
polyCone;
}
}

 

I hope this helps!

Message 3 of 3

peter_baehr97
Contributor
Contributor

Wow! Thanks for the detailed explanation of the syntax! Helped alot! Appreciate you. Script is finally working now.

0 Likes