PDA

View Full Version : UVset renaming script


arkanoid
12-29-2010, 03:04 PM
Hi, I don't really manage MEL nor any other programming matter, but I'm trying to put toghether a very simple script. I want the script to scan my file, pick each (poly) object in scene and rename each object's UVset to "objectName" + "UVset". That's useful when I export in FBX with layed out UVs, otherwise the host software will read only the default "map1" UVset for all the objects, with all my uvs massed toghether.
So my attempt for now is this:

string $eachElement;
string $sel[] =`ls -sl`;

for ($eachElement in $sel)

polyUVSet -rename -currentUVSet -newUVSet ($eachElement + "UVset");

which off course don't work. The currentUVSet flag don't seem to "upgrade" during the loop, which brings to renaming all the UVsets with the name of the last selected object.
My understanding of loops and arrays is under the basic, I know. Anyone can throw some light to me?

NaughtyNathan
12-29-2010, 09:20 PM
what you have done so far is fine. the problem is that you DON'T (http://forums.cgsociety.org/DON@T) tell the polyUVSet command WHAT object you want to operate on! so by default it is running that exact same polyUVSet command on the ENTIRE selection every time through the loop!
Just adding the object name ($element) to the end of the polyUVSet command line should fix it.

:nathaN

arkanoid
12-30-2010, 09:55 AM
Well thanks! It's working now with your correction:

string $eachElement;
string $sel[] =`ls -sl`;

for ($eachElement in $sel)

polyUVSet -rename -currentUVSet -newUVSet ($eachElement + "UVmap") $eachElement;

If you don't care now I also would like to know why it works :D
I thought that the for-in loop would tell the object to operate on, so to me the logic traduced was:

for each selected object
rename in loop every time the "current" uvset to the object name + "UVmap"

This in theory, but I can't really understand why the $eachElement variable should be "conscious" to really rapresent "each element" in the selection group. In fact the first variabe ($sel) is defined by a command. Contrarily the $eachElement variable is just declared with that name, that should not mean nothing

NaughtyNathan
12-30-2010, 12:47 PM
practically all Maya commands (Mel/python) MUST be told what item(s) to operate on, or they will operate on the selection. This is an absolute truth and does not change behaviour depending on which bit, or type, of code they are called inside.

a for loop is simply a convenient way of setting up a fixed loop so the commands within can be executed multiple times. The for loop itself cannot alter how the code within works, that would be very confusing. All the for loop can do is provide new variables that the code within can reference if necessary... they don't have to!

string $array[6] = { "a","b","c","d","e","f" };
for ($elem in $array)

all the above line does is construct a loop that will run "array length" times. If the array has six items, the loop will execute six times. However, each of those six times, the variable $elem will be assigned to one of the items in $array (in order!). The first time through the loop $elem will be set to "a", and the second time it will be set to: "b", etc...
If your code inside the loop doesn't reference the variable $elem then $elem will be effectively redundant, but the loop will still execute six times...

:nathaN

arkanoid
12-30-2010, 01:43 PM
Oooh now I think I get it! A big Thank you master coder! :buttrock:
So the "for loop" in english must be read like:

with $elem taking place in loop through the various $array[n] elements,
execute the underneath code n times

execute on what? I have to specify it; otherwise, as always in Maya, the commands will be executed to all the selected objects.
If I want to execute the command on each element of my array in loop, I put the $elem variable at the end of the command.
Right?

NaughtyNathan
12-30-2010, 02:11 PM
yep, that's essentially a correct summary... but "putting $elem at the end of the line" only works because most commands expect the item names at the end.. some commands behave a little differently, for example setAttr:

for ($elem in $sel)
{
move -a -rpr 0 0 0 $elem;
setAttr ($elem + ".template") on;
print ("object " + $elem + " has been templated!\n");
}


that loop would move all selected items to zero, template them, then tell you it had. (assuming they were nodes and had been previously grabbed into the array $sel[] like in your original example!! :) )

:nathaN

arkanoid
12-30-2010, 02:39 PM
Perfectly clear. But how can I know what the command expects and how? ES in the polyUVSet command page on the guide there's no trace of that behaviour.

CGTalk Moderation
12-30-2010, 02:39 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.