PDA

View Full Version : Shot gun rename


nottoshabi
03-21-2009, 05:15 AM
How do you rename a group of objects in an array with an incremental number at the end?

For example if you have a selection of spheres and you would want to name them:
sphere01
sphere02 etc....

This is what I have so far. I just can't get it to increment the number.

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

int $i;
for ($i = 0; $i < size($sel); $i++)

{
rename Sphere;
}

greatPumpkin
03-21-2009, 06:28 AM
it gets a bit more complicated than this if you want a '0' in front if the number is less than 9 but that should work, also you don't need to declare the '$i' if you're using a counter-type loop


string $sel[] = `ls -sl`;
for ($i = 0; $i < size($sel); $i++)
{
rename $sel[i] ("Sphere" + i);
}

Pyrokinesis
03-21-2009, 10:44 AM
import maya.cmds as mc

selected = mc.ls(sl=True)

for sel in selected:
mc.rename(sel, 'newName' + '#')

nottoshabi
03-23-2009, 11:51 PM
Thanks guys.

nottoshabi
03-25-2009, 08:51 PM
How do I make the name to be a variable?

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


for ($i = 0; $i < size($sel); $i++)
{
rename $sel[$i] ("$sel2" + $i);
}


It only names the first one. It never adds the number and I get a warning.
// Warning: Removing invalid characters from name. //

In my $sel2[] I have an object named Blahh. In my $sel[] I have 5 objects it only names the first object Blahh. And the rest are left alone.

Why?

greatPumpkin
03-25-2009, 09:02 PM
don't put quotes around $sel2. You also need to specify which item in sel2 you want.

Also note that unless you run the first two lines separately $sel[] and $sel2[] would be the same objects.

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


for ($i = 0; $i < size($sel); $i++)
{
rename $sel[$i] ($sel2[$i] + $i);
}

nottoshabi
03-25-2009, 09:51 PM
$sel and $sel2 are going to be to different variables fed in from other proc's. Just this part of the code got me stuck. Now I'm getting an error and it only names the first obj.
// Error: New name has no legal characters. //

CGTalk Moderation
03-25-2009, 09:51 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.