Need Help - evaluate attribute

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 01 January 2012   #1
Need Help - evaluate attribute

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
__________________
Portfolio: CGIFocus.co.uk
 
Old 01 January 2012   #2
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 (").

Last edited by zeroeffect : 01 January 2012 at 01:05 PM.
 
Old 01 January 2012   #3
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
__________________
Portfolio: CGIFocus.co.uk
 
Old 01 January 2012   #4
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.

Last edited by Nyro : 01 January 2012 at 01:17 PM.
 
Old 01 January 2012   #5
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
__________________
Portfolio: CGIFocus.co.uk
 
Old 01 January 2012   #6
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 10:52 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.