Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Maya Basics & Newbie Lounge (https://simplymaya.com/forum/forumdisplay.php?f=31)
-   -   Mel Syntax Question (https://simplymaya.com/forum/showthread.php?t=41309)

proximo4444 05-03-2015 05:23 AM

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.

Gen 05-03-2015 08:51 AM

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.

proximo4444 05-03-2015 10:37 PM

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.

Gen 06-03-2015 06:28 AM

"..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;

}



All times are GMT. The time now is 09:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018