PDA

View Full Version : Need Help - evaluate attribute


Yann1ck
01-11-2012, 11:25 AM
Hi,
I have created an attribute on a cube ( pCube1.Grades) as a drop down menu.
I want the value of the drop down menu to control the loading/unloading of reference files.

this is how far I got:

getAttr pCube1.Grades;
float $Grade="getAttr pCube1.Grades";
if ( $Grade = 1 )
{
file -loadReference "sphere1RN" "C:/sphere1.mb";
file -unloadReference "sphere3RN" "C:/sphere3.mb";
}
else if ( $Grade = 2 )
{
file -unloadReference "sphere1RN" "C:/sphere1.mb";
file -loadReference "sphere3RN" "C:/sphere3.mb";
}
else
{
print "Hello";
};

But it seems that the value I got from getAttr pCube1.Grades; doesn't work.
Could someone show what I am doing wrong?

Thanks,

Yannick

zeroeffect
01-11-2012, 11:59 AM
In your "if" and "else if" statements, you are assigning the number to the variable instead of comparing the two values.

if ( $Grade = 1 ) {}

should be:

if ($Grade == 1) {}


Also:

float $Grade="getAttr pCube1.Grades";

Maybe this is just a typo, but you should be using backticks (`) around the MEL command instead of double quotes (").

Yann1ck
01-11-2012, 12:14 PM
Thank you Chris.

I still get this error:
// Warning: float $Grade="getAttr pCube1.Grades";
//
// Warning: Converting string "getAttr" to a float value of 0. //

My attribute value is changed to 0 even if it set to another value.
I think the problem comes from here:
float $Grade="getAttr pCube1.Grades";
it convert the value to 0 (see error message).

I am not sure how to fix this.

Cheers,
Yannick

Nyro
01-11-2012, 12:15 PM
This may sound arrogant, so don't be offended, but I think you should read the documentation provided with maya to get a hang of proper MEL syntax.

There are several issues with your script, let's step through them one at a time:

getAttr pCube1.Grades;
This line doesn't make any sense, as it effectively doesn't do anything.

float $Grade="getAttr pCube1.Grades";
You're using the wrong quotes. In fact, to retrieve the result of a function in MEL, you don't use quotes at all, you use what in English is referred to as a backtick: | ` |
It's really an accent (grave, to use french terminology).

So the correct syntax would look like this:
float $Grade = `getAttr pCube1.Grades`;

As a side note, I don't understand why you're using float values anyway. If all you're doing is using numbers to switch through different states, then use either integers or Enum. Integers compute faster than float values. This may not be relevant in this case, but it's simply a matter of good practice and efficient programming. They also take up less memory ;)

if ( $Grade = 1 )
else if ( $Grade = 2 )

The equal symbol (=) is used to assign values. In the if statements, you're assigning values to $Grade instead of comparing them.
To compare values, use double equal symbols (==).


Finally, to be nitpicky, get in the habit of using interCaps notations for variables and attributes. It doesn't really make a difference but it does make your code follow the same style as Maya internal programming (and that of most other MEL programmers). Once you are in this habit, you will find yourself doing far less typos when referring to Mayas internal commands and variables and you won't have to second guess the spelling of said names.
So instead of $Grade, write $grade. Same with the attribute.


EDIT
Agh, beat me to it.

Referring to your second question: the answer was in Chris' post, at the very end. Also in my post above.

Yann1ck
01-11-2012, 12:31 PM
Hi Nyro,
Not offended. I suck at MEL.
I found my error I did try the ` but kept using ' so I thought it was something else not working.

Now it is working.
here is what I have now:

$Grade= `getAttr pCube1.Grades`;
if ( $Grade == 1 )
{
file -loadReference "sphere1RN" "C:/sphere1.mb";
file -unloadReference "sphere3RN" "C:/sphere3.mb";
}
else if ( $Grade == 2 )
{
file -unloadReference "sphere1RN" "C:/sphere1.mb";
file -loadReference "sphere3RN" "C:/sphere3.mb";
}
else
{
print "Hello";
};


Thanks,

Yannick

CGTalk Moderation
01-11-2012, 12:31 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.