Noob script question

Noob script question

francois_maree
Participant Participant
277 Views
2 Replies
Message 1 of 3

Noob script question

francois_maree
Participant
Participant
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 -e -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 -e -forceElement lambert2SG;
} else if ( "Hair.HairColor" == 1) {
// Assign brunette material
sets -e -forceElement lambert3SG;
} else if ( "Hair.HairColor" == 2) {
//Assign red material
sets -e -forceElement lambert4SG;
} else if ( "Hair.HairColor" == 3) {
// Assign black material
sets -e -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: &#40;'invalid syntax', &#40;'<maya console>', 2, 40, 'if &#40; "Hair.HairColor" == 0&#41; {\n'&#41;&#41;
# File "<maya console>", line 1
# if &#40; "Hair.HairColor" == 0&#41; {
# ^
# SyntaxError: invalid syntax #


Ok, so please point me in the right direction &#40;you may point and laugh as well while you're at it&#41;. ;-&#41;

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. :-&#41;

Thanks in advance for your help.
0 Likes
278 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Hello There,
Please execute this code in the MEL Tab, and not in the Python Tab. Here's the basic code:


int &#36;mode = `getAttr&#40;"Hair.HairColor"&#41;`;
switch&#40; &#36;mode &#41;
{
case 0:
// code
break;
case 1:
// code
break;
case 3:
// code
break;
case 4:
// code
break;
default:
error&#40; "Invalid mode:" + &#36;mode &#41;
}


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

scriptJob -ac "Hair.HairColor" "confirmDialog -m &#40;`getAttr&#40;\"Hair.HairColor\"&#41;`&#41;";
0 Likes
Message 3 of 3

francois_maree
Participant
Participant
Awesome! Thanks a million Norbert. The code is working like a charm now. There was just a minor syntax problem &#40;semicolon at the end of the error line&#41; and the numbers for the modes that needed fixing and Bob's your uncle. You rock! :-&#41;
0 Likes