PDA

View Full Version : MEL, undeclared variable error


Xinos
06-27-2012, 09:36 AM
I am hacking in a new menu in buildShaderMenus.mel so I can delete shaders without going into hypershade, but when I try to use a variable in menuItem's -command flag, I get a "$material is an undeclared variable" error, even though $material is used with -label.

$menu = `menuItem -label $material
-annotation $deleteMaterialAnnotation
-command "deleteMaterial($material)"`;

What's going on?

jaydru
06-27-2012, 10:00 AM
$menu = `menuItem -label $material
-annotation $deleteMaterialAnnotation
-command ("deleteMaterial(\"" + $material + "\")")`;

try that :)

NaughtyNathan
06-27-2012, 11:06 AM
what's going on is scope.

http://forums.cgsociety.org/showpost.php?p=6538895&postcount=17

Xinos
06-27-2012, 12:33 PM
Thanks jaydru, that worked!

what's going on is scope.

http://forums.cgsociety.org/showpost.php?p=6538895&postcount=17
It's not very obvious why the -command flag can't access non-global variables even if they are "within" scope. Seems like a pretty dumb design decision to me, but I guess there's a reason for it.

rgkovach123
06-27-2012, 03:53 PM
When you specify a command to run when the menuitem is activated, you must specify a string that includes the command to run and the arguments (if any).

You need to join together the contents of the variable with the command to run together as a single string and then pass that the menu's command argument.

Xinos
06-27-2012, 04:38 PM
Nobody agrees with me that it seems dumb?
-command "deleteMaterial($material)"`;

The whole command is one string.. the function call deleteMaterial works fine, but $material can't be apart of that string, we have to break out of that string, make $material into a new string, and concatenate the two strings into one string again. wtf.

rgkovach123
06-27-2012, 04:59 PM
Whether or not it's dumb doesn't matter...that is the way MEL is.

If you come to MEL with pre-concieved notions from other programming languages, prepare to be disappointed. MEL is not a programming language...just a simple scripting system.

You can try Python or pymel, they are both more robust. Though if you are modifying some native part of Maya, you'll be stuck with MEL.

NaughtyNathan
06-27-2012, 07:00 PM
it's not in the slightest bit dumb. Not only is it completely logical when you understand anything about how programming languages, callbacks, literal strings and scope works, but it's also extremely important that it does work like this, not just in MEL but in most languages that use variables and strings.

zoharl
06-27-2012, 08:43 PM
it's not in the slightest bit dumb. Not only is it completely logical when you understand anything about how programming languages, callbacks, literal strings and scope works, but it's also extremely important that it does work like this, not just in MEL but in most languages that use variables and strings.

I disagree, and think it's dumb as well. This syntax (except for the stupid back ticks - any reason for that btw?) is the same as in Perl, except that in Perl the variable is interpolated.

@Xinos, after discovering this, I think that it is dumber that you use mel:

http://forums.cgsociety.org/showthread.php?t=1044538

CGTalk Moderation
06-27-2012, 08:43 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.