PDA

View Full Version : setAttr for a variable


MikeRhone
01-09-2004, 05:43 PM
Can any MEL gurus help me with the sytax on this script. I want to run a setAttr on multiple objects, but I am stuck on the 2 main points of the script:

I want to run a variable in this script:

setAttr "myMeshShape.visibility" 0;

in a perfect world I would be able to type this:

setAttr "$currentMeshShape.visibility" 0;

but I obviously get a syntax error.

What is the best way to approach this?

Mike R

mhovland
01-09-2004, 06:16 PM
string $objs[] = `ls -sl -fl`;
for($i=0;$i<size($objs);$i++)
{
setAttr ($objs[$i]+"Shape.visibility") 0 ;
}


That should do it.

This assumes that the object shape nodes are in the form "ObjectNameShape". If they are in the default Maya format from when you create an object like this (object name: pSphere1 ShapeName: pSphereShape1) it won't work. The safest way to do this would be to get the shape node for each object in the selection, and use that instead of the objectname + "Shape".

Like this:

string $objs[] = `ls -sl -fl`;
for($i=0;$i<size($objs);$i++)
{
string $shape[] = `listRelatives -s $objs[$i]`;
setAttr ($shape[0]+".visibility") 0 ;
}

MikeRhone
01-09-2004, 07:39 PM
Thanks again... Perfect!

This is like the 3rd time you have saved my ass. Cheers!

Mike R

mhovland
01-09-2004, 07:41 PM
You owe me a beer. :beer:

mark_wilkins
01-09-2004, 11:43 PM
Originally posted by Rhonedog
in a perfect world I would be able to type this:

setAttr "$currentMeshShape.visibility" 0;

I didn't look closely at what Mike was trying to do, but it seemed kind of complicated. In this instance, you should be able to do this:

setAttr ($currentMeshShape + ".visibility") 0;

-- Mark

mhovland
01-09-2004, 11:56 PM
Marks' method assumes that you have already gotten the selection list you need to setAttr on.

So if getting the selection list was not an issue, Marks' method would be the quickest.

I assumed nothing. :shrug:

mark_wilkins
01-10-2004, 12:05 AM
Yeah, I was just answering the question about why he got a syntax error with that one line of code, assuming the correct shape node's name was in that variable.

-- Mark

MikeRhone
01-13-2004, 05:04 AM
Lets face it, my MEL syntax needs a lot of work, but I'm trying...!

Thanks again both of you. :)

P.S. Mark: I couldnt find my answer to this anywhere in your book!

mark_wilkins
01-13-2004, 05:12 AM
Originally posted by Rhonedog
P.S. Mark: I couldnt find my answer to this anywhere in your book!

I don't know whether it's explained as such, but examples of using setAttr with attributes that are constructed from variables are all over the book. Have a look at page 112 for one of them.

Take care!

-- Mark

CGTalk Moderation
01-17-2006, 04:00 AM
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.