SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.


SM Forums / Lounges / Maya Basics & Newbie Lounge / Mel Syntax Question

Maya Basics & Newbie Lounge Please post in this section ONLY if your question doesn't fit in anywhere else on the forum, or if you're not sure which section it's relevant to. We also have Free streaming trial available.

Join the conversation. Reply to post
 
Thread Tools
05-03-2015, 05:23 AM   #1
proximo4444
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.
  Reply with quote
05-03-2015, 08:51 AM   #2
Gen
Super Moderator
 
Gen's Avatar
 
Join Date: Dec 2006
Location: South FL
Posts: 3,518
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 ||::
  Reply with quote
05-03-2015, 10:37 PM   #3
proximo4444
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.
  Reply with quote
06-03-2015, 06:28 AM   #4
Gen
Super Moderator
 
Gen's Avatar
 
Join Date: Dec 2006
Location: South FL
Posts: 3,518
"..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 ||::
  Reply with quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
MEL - Syntax error IrenaDee Maya Technical Issues 4 28-02-2013 04:19 PM
mel question mortezarel Maya Basics & Newbie Lounge 3 26-10-2012 06:08 AM
Simple MEL question ctbram Maya Basics & Newbie Lounge 0 10-05-2006 02:01 AM
MEL Syntax Highlighting for BBEdit 8.0 on Mac OS X mark_wilkins Programming 0 31-08-2004 09:54 AM
MEL syntax mode for SubEthaEdit dannyngan Programming 0 24-05-2004 09:27 AM