View Full Version : Add a custom attribute to selected object

06 June 2007, 01:10 AM
I can't quite find why this doesn't work:

$mySel = `ls -sl`;
addAttr -ln "test" -at double $mySel;
setAttr -e -keyable true ($mySel + ".test");
I'm attempting to add a new attribute called "test" to the selected object. I get the error:
// Error: Cannot convert data of type string[] to type string. //
Ummm... ok. What am I doing wrong?

Thanks for any help

06 June 2007, 03:54 AM
Hey geoff3dnz, i dont have Maya in front of me, but try this:

Original Code:
$mySel = `ls -sl`;
addAttr -ln "test" -at double $mySel;
setAttr -e -keyable true ($mySel + "test");

New Code:
string $mySel[] = `ls -sl`;
addAttr -ln "test" -at double $mySel[0];
setAttr -e -keyable true ($mySel[0] + ".test");

I am not sure, but whenever you use `ls -sl` you have to store it into an array $mySel[], even if it is just 1 object. And to call the object, use $mySel[0], which would be the first element in the array. Also I tried it without decalring $mySel[] as a string, and it didn't work, so make sure that it reads: string $mySel[]

Also, when you are trying to set the attribute as keyable: ($mySel + "test"), the "test" part needs a period in it, like: ".test"

You could even modify the code to loop through a list of objects and add that attribute:
string $mySel[] = `ls -sl`;
for($obj in $mySel)
addAttr -ln "test" -at double $obj;
setAttr -e -keyable true ($obj + ".test");

06 June 2007, 10:07 PM
Awesome. Thanks very much for your help :)

CGTalk Moderation
06 June 2007, 10:07 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.