Maya for 3D Printing - Rapid Prototyping
In this course we're going to look at something a little different, creating technically accurate 3D printed parts.
# 1 05-03-2015 , 05:23 AM
Registered User
Join Date: Jul 2014
Posts: 6

Mel Syntax Question

Hey everyone, I'm currently trying to write a simple script that will change the attributes that I've added to a controller with an array but I seem to be getting the syntax wrong.

If I add a setAttr command (to make the attributes that I've made with the for loop) keyable, I seem to be getting the syntax wrong.

This is what I had:

string $mySel[] = `ls -sl`;
string $name;
int $arraySize = `size $mySel`;
for ($i = 0; $i < $arraySize; $i++)
{
$name = $mySel[$i];
select -r PLC;
addAttr -ln ($name) -at double;
setAttr -k on |PLC. + $name;
}

I know that I could just add the "-k 1" code into the addAttr command but I still need to figure out how to write the code for another part in my script.

I've tried it like

setAttr -k on |PLC.$name;
setAttr -k on |PLC.($name);
setAttr -k on |PLC. + ($name);
setAttr -k on |PLC. + \"$name\";

but seem to have no luck.

If anyone has any suggestions on what I'm doing wrong I would really appreciate the help.

Thanks in advance.

# 2 05-03-2015 , 08:51 AM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,522
The strings for the object and attribute your setAttr statement is supposed to act on, are not concatenated correctly. Use parentheses to create an expression that joins these strings properly to create a valid object name and since Maya will evaluate its contents first, now the rest of the statement knows what its working with.

setAttr -k on ("PLC." + $name);

or

setAttr -k on ("PLC" + "." + $name);

I put double quotes around PLC to denote that its a string so Maya doesn't freak out and tell me I'm using PLC incorrectly because it doesn't know for sure what it is.

Also, sometimes you might want to avoid hardcoding names into your script as it cuts down on flexibility but thats another thing altogether.

Hope that helps.


- Genny
__________________
::|| My CG Blog ||::
::|| My Maya FAQ ||::
# 3 05-03-2015 , 10:37 PM
Registered User
Join Date: Jul 2014
Posts: 6
Ahh yes that worked!!

Thank you. I was really banging my head against the wall on that one.

I've got a similar issue that I think I'm also getting the wrong syntax on:

I'm trying to assign the Intensity Mult value of a light to a string that gets run through an array but when I try to convert the string that holds the name of the attribute to a float value, it always tells me that it equals zero.

This is what I've got so far:

string $mySel[] = `ls -sl`;
string $myShapeSel[] = `ls -selection -dag -lf -ap`;
string $name;
string $shapeNode;
string $shapeNodeIntensity;
float $testing;
int $arraySize = `size $mySel`;

for ($obj in $myShapeSel) {
$obj = $shapeNode;
setAttr $shapeNode + ".intensityMult";
$testing = (float)$shapeNodeIntensity;
print $testing;
}
for ($i = 0; $i < $arraySize; $i++) {
$name = $mySel[$i];
//$shapeNode = $myShapeSel[$i];
//$myShapeSel[$obj] = $shapeNode;
//$shapeNodeIntensity = ($shapeNode + ".intensityMult");
//$testing = $shapeNodeIntensity;
select -r PLC;
addAttr -ln ($name) -at double -k 1;
//print $shapeNode;

}

I'm wondering if I have to call the objects outside of the for loop first because it doesn't see
$shapeNodeIntensity = ($shapeNode + ".intensityMult");
as equaling a number.

Any ideas?
It seems like the same sort of problem I was having earlier.

# 4 06-03-2015 , 06:28 AM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,522
"..when I try to convert the string that holds the name of the attribute to a float value, it always tells me that it equals zero"

Float variables only store numerical values. Converting a string "123" to float will work but converting "apple" won't. So make sure your dealing with the variable for the light intensity and not the light name.

Code:
string $mySel[] = `ls -sl`;
string $myShapeSel[] = `ls -selection -dag -lf -ap`;
string $name;
string $shapeNode;
string $shapeNodeIntensity;

float $testing;
int $arraySize = `size $mySel`;

//Gen~ obj variable could have just as easily been swapped out for shapeNode
// instead of declaring it above
for ($shapeNode in $myShapeSel) {
//Gen~ now you can ditch this ---> $obj = $shapeNode;
setAttr $shapeNode + ".intensityMult"; //Gen~ set intensityMult to what value?
$testing = (float)$shapeNodeIntensity; //Gen~ shapeNodeIntensity never had a value
// so nothing to convert
print $testing;
}
for ($i = 0; $i < $arraySize; $i++) {
$name = $mySel[$i];
//$shapeNode = $myShapeSel[$i];
//$myShapeSel[$obj] = $shapeNode;
//$shapeNodeIntensity = ($shapeNode + ".intensityMult");
//$testing = $shapeNodeIntensity;


//Gen~ selecting PLC to add attributes is unessecary
// simply add the name of the object you want the command to alter
// at the end of the statement --> addAttr -ln ($name) -at double -k 1 PLC;
select -r PLC; 

addAttr -ln ($name) -at double -k 1;
//print $shapeNode;

}


- Genny
__________________
::|| My CG Blog ||::
::|| My Maya FAQ ||::
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads