Call for Submission
NAB 2012 Best of the Best Show Reel
Submit your work today!
  • 1/3
You are here: Forum Home / Autodesk® Maya® / MEL / Noob script question
  RSS 2.0 ATOM  

Noob script question
Rate this thread
 
36874
 
Permlink of this thread   Subscribe to this thread
avatar
  • Location: Johannesburg
  • Total Posts: 16
  • Joined: 23 December 2006 10:42 AM

Ok. So I’m not a programmer at all. I know just enough to get myself into trouble like now. ;-)

I have modeled some hair I would like to attach to a character. Then I want to add a custom attribute with a dropdown menu for selecting between 4 different hair colors (Blonde, Brunette, Red, Black). So I managed to create a new attribute for the hair of the Enum type with the 4 options. Now the scripting part is where I’m stuck. Here is what I have:

The hair geometry is named Hair
The short name for the hair color attribute is HairColor
When I select say the first entry in the dropdown if the HairColor attribute, I see this in the script editor:

setAttr "Hair.HairColor" 0;

When I manually apply one of the 4 materials I created to the hair geometry, I see the following in the script editor:

select -r Hair ;
sets --forceElement lambert3SG;

Ok, so I’m trying to figure out how to determine which one of the dropdown menu options are selected. I created this if else code:

if "Hair.HairColor" == 0) {
 
// Assign blonde material
 
sets --forceElement lambert2SG;
else if "Hair.HairColor" == 1) {
 
// Assign brunette material
 
sets --forceElement lambert3SG;
else if "Hair.HairColor" == 2) {
 
//Assign red material
 
sets --forceElement lambert4SG;
else if "Hair.HairColor" == 3) {
 
// Assign black material
 
sets --forceElement lambert5SG;
}

That doesn’t seem to do the trick though. The way I’m testing this (and this might be where I’m going wrong) is by having the code in the input area of the script editor under the MEL tab and executing it. When I execute it I get the following message:

# Error: ('invalid syntax', ('<maya console>', 2, 40, 'if ( "Hair.HairColor" == 0) {\n'))
#   File "<maya console>", line 1
#      if ( "Hair.HairColor" == 0) {
#                      ^
#  SyntaxError: invalid syntax #

Ok, so please point me in the right direction (you may point and laugh as well while you’re at it). ;-)

What I would also like to find out is how do I get the code to be executed every time the dropdown menu changes. I’ve been looking around and it seems like I would need to add it to the expression editor. I am eager to learn. :-)

Thanks in advance for your help.



~Never underestimate the power of the Digital Highway

Replies: 0
avatar
  • Norbert
  • Posted: 19 November 2009 11:22 AM

Hello There,
Please execute this code in the MEL Tab, and not in the Python Tab.  Here’s the basic code:

int $mode = `getAttr("Hair.HairColor")`;
switch( $mode )
{
 
case 0
 
// code
 
break;
 case 
1:
 
// code
 
break;
 case 
3:
 
// code
 
break;
 case 
4:
 
// code
 
break;
 default:
 
error( "Invalid mode:" $mode )
}

If you wanna call a proc when a certain attribute has been changed, the use the scriptJob command.

scriptJob -ac "Hair.HairColor" "confirmDialog -m (`getAttr(\"Hair.HairColor\")`)";


Replies: 0
avatar

Awesome! Thanks a million Norbert. The code is working like a charm now. There was just a minor syntax problem (semicolon at the end of the error line) and the numbers for the modes that needed fixing and Bob’s your uncle. You rock! :-)



~Never underestimate the power of the Digital Highway

Replies: 0




   
  Settings Choose Theme color: