Ok your confusing 2 things. Scripts and expressions. They are NOT the same thing. They dont work on same principles. You get into BIG problems if you use setAttr inside expressions. The most notable problems are (in no particular order):
- This screws your undo. setAttr changes mayas undo state. You can turn undo off but you need to set it back on after this.
- It makes it impossible for you to use referencing. This can be a very bad thing if you design reusable assets.
- Its a magnitude slower.
- ... (there are more reasons one is that screwing up with this in a hurry is likely to get you fired because the renderfarm just died on you)
So do not confuse MEL and expressions together. They work on the same language set but are not the same thing. A expression in a way compiled once whilst you can freely eval stuff in mel. But should avoid doing so in expressions (it works but it can end up shooting in your own foot eventually).
Ok then for the problem at hand. Your not being quite particular HOW you copy the expression. Say you have following scene:
// this is a way for me to upload a scene to you
// warning this will clear what you are doing
file -f -new;
sphere;
expression -s "nurbsSphere1.translateY=sind(time*60 - nurbsSphere1.translateZ)"
-o nurbsSphere1 -ae 1 -uc all ;
And you would copy the sphere as follows:
// edit -> duplicate special
// duplicate input graph checked
duplicate -rr -un;
Then maya would indeed have referenced new sphere data. If you go into the expression editor then yes the name has changed as follows:
nurbsSphere2.translateY=sind(time*60 - nurbsSphere2.translateZ)
I dindt type that. So clearly this is not what your doing so i assume your actually copying TEXT. Simply put the -o flag is there to accomodate for this. The expression node itself IS NOT holding the names a s text. It actually compiles them to references. You can do two other things, first if you disconnect the attributes:
// you can do this in the node editor or hypergraph
// if you dont want to script this.
disconnectAttr nurbsSphere1.translateZ
expression1.input[0];
disconnectAttr expression1.output[0]
nurbsSphere1.translateY;
Now the expression is simply there without any fixed names. if you look at this in the expression editor it now looks as follows:
.O[0]=sind(time*60 - .I[0])
This is what the expression really eats, it has 2 arrays the output array O and the input array I. you are now free to connect the node as you please for example:
polyCube;
connectAttr pCube1.translateZ
expression1.input[0];
connectAttr expression1.output[0]
pCube1.translateY;
Oh the names changed if you ask the expression editor. So really under the hood there are NO NAMES in the strings. Thats just one way of presenting things for you.
Theres a second thing the expression does not need to reference the names when one object is selected it is enough to do as follows instead of what i started with:
// warning this will clear what you doing
file -f -new;
for ($i = 0; $i<10; $i++){
$name=sphere();
expression -s ".translateY=sind(time*60 - .translateZ)"
-o $name -uc all ;
}
See no object names at all in the string. the -o flag takes care of this it handles things for you. This works even in the script editor for the selected object but not for 2 objects.
This then leads to 4 possible and good solutions:
- disconnect copy expression and reconnect, this can ahndle any number of objects but can be havey if its just one name
- Wirte names as O[x] and I[x] and then connect
- use the selection and don't type the selected object name, maya will figure it out. This is fine if you insist on typing and only have one name to change.
- Copy the input graph if its the same object or just a transform (you can change the object underneath).
Last but a fifth not so good option you could do a string replace with regular expressions but i would avoid this.
PS: i did not say you can NOT use setAttr i said you should not. You are free to cause as much problems for yourself as you like.
PPS: its possible to use for loops without setAttr for many objects to simplify the expression and still using references.