PDA

View Full Version : renaming selected objects.


Knylen
12-07-2010, 11:38 PM
What im trying to do is selecting for objects, say in this case 4 cylinders, i choose them in a specific order, cylinder1, cylinder2, cylinder3, and finally cylinder 4.

What i want now is to rename those objects to left_frontwheel, right_frontwheel, left rearwheel, right_rearwheel.


to be honest i dont have a clue what im doing, just testing my way in the mel world really.

i have started with the simple list selection command. what i dont know is how to use the information that gets return. im making a string wich contains the selected objects. can i make a string wich contains the new name and use this to rename the objects in the first one? doh, really dont have a clue.


thanks!

Nyro
12-08-2010, 09:27 AM
Ok, since simply posting a solution wouldn't do you any good (since you won't be learning anything), here's some clues.

http://download.autodesk.com/us/maya/2009help/Commands/ls.html
http://download.autodesk.com/us/maya/2009help/files/Arrays_vectors_and_matrices_Arrays.htm
http://download.autodesk.com/us/maya/2009help/files/Controlling_the_flow_of_a_script_forin.htm
http://download.autodesk.com/us/maya/2009help/Commands/rename.html

And here's the biggy, the site you should always have open and loaded in a tab while scripting:
http://download.autodesk.com/us/maya/2009help/Commands/index.html

Geuse
12-08-2010, 11:39 AM
To start off with I'd recommend you simply type a checklist for each step.

First you select 4 objects, then you run the procedure:

1. list my selection.
2. for 1st in selection rename "left_frontwheel"
3. for 2nd in selection rename "right_frontwheel"
4. for 3rd in selection rename "left rearwheel"
5. and for 4th in selection rename "right_rearwheel"
6. Done!

1. list the names in an array of what I have selected " string $sel[] = `ls -long -sl`; "
(a string is only to handle a single name, array is like a drawer with infinite drawers which handles multiple names) the difference is you add " [ ] ".
The -long flag means we want the whole name including the path, like for instance it's in a group so we're getting the exact object to rename if there would be several named the same.

2. So the names are now in an array called " $sel ".
if you were to execute " print $sel " you'd get all the selected objects name printed out to the script editor.

3. Ok, so in the array we have 4 items where the first item is $sel[0] (data slot begins with zero, so our last should be 3) try typing " print $sel[4] " and you'd get nothing.

4. so now you just simply us the rename command for each of the four:
rename $sel[0] "left_frontwheel";
rename $sel[1] "right_frontwheel";
rename $sel[2] "left rearwheel";
rename $sel[3] "right_rearwheel";


So the final script for this should simply read:

global proc knylen_renameObjsToWheel ()
{
string $sel[] = `ls -long -sl`;

rename $sel[0] "left_frontwheel";
rename $sel[1] "right_frontwheel";
rename $sel[2] "left rearwheel";
rename $sel[3] "right_rearwheel";

}

This will suffice for what you want to do, but you could always add commands to check that you only selected 4 objects, or add padding if the names already exists in the scene, etc.

Knylen
12-08-2010, 09:54 PM
Thank you very much, and yes i have looked at the help in maya, thou im not a coder and this is the very start so therefore whats simple for others is pretty darn hard for me =)

Geuse, the code you presented for me works good, however if the objects has already been renamed once i get an error message, nothing strange with this, however could i put this in an if statement? so that when the wheels already have the right names i get a popup wich says already renames or something?

thanks again!

Geuse
12-08-2010, 11:00 PM
I know what you mean, took a while for me to understand the process, but once I understood and could break it up, it was all downhill from there. You can achieve very much just using for loops and if statements. It still is what I seem to be using most often and by each day you learn something new.

Regarding the script. You can make all sorts of alterations to it. For just a renaming script, since it's not going to be very sophisticated. You only want to rename the meshes you use for tires and to do so they are to be selected in a specific order. However, you could check to see which is the farthest back and most to the left so based on that given that you modeled it in z positive direction you could select all four tires without any defined order and set the appropriate names that way, also you'd want to make sure you only apply this for 4objects so if you have 5 selected you print out an error.
Also if they are already renamed correctly, just skip those.

So continue on what you already have.
1. Make sure you only have four objects selected, if not print an error.
2. Compare each of the tires worldspace position and sort out which one is farthes back to the left etc. We can't use the getAttr as the translate attributes most likely are frozen.
3. Rename each one, but in chances one already has the correct name, skip that one and print out a message saying "hey, the "third" object is already named correctly".
4. Done!


so the first one is easy! a short if/else statement will suffice.

if (size($sel) != 4 )
{ error "Hey, hey. Only select four tire meshes!" }

else
{
all the code

}


2.Ok, the next one is little harder as we need to get the translation in worldspace and then compare all of them. I can skip this one as it's late and seems It perhaps a bit steep and unecessary(tell me if you need to know it though)

so number 3.
For each object we want to get the name and se if it matches the right one. This is based on the specific order you select them by.
So first we will need to also get the short names of the objects. the long ones are only used to find the right way to them.
also a nice thing to do is to declare variables for the names if you ever want to rename them it's easier to have all variables in one place at the top so you don't need to scroll down and find where in the script they are renamed. We will also specify another variable contaning the message which will append each object with the right nameand be printed out at the end of the script.

Some commands we're going to use:
gmatch = command for matching strings. The "`" are used in order to execute it and store the value inside the variable $match. gmatch is either 1 or 0. true or false
int = integer = specifies a full number, not a decimal number , but a full number so, 1, 2, 7, 20

This is from before
string $sel[] = `ls -long -sl`;

if(size($sel) != 4)
{
error "select four objects in the correct order.";
}

else
{
And here we continue:
string $message = ""; //leave it empty

//Specify the names you wish to use
string $nameFL = "left_frontwheel";
string $nameFR = "right_frontwheel";
string $nameBL = "left_rearwheel";
string $nameBR = "right_rearwheel";

//We convert all our nice long names to short names, but still keeping the paths intact.
string $shortNameFL[] = `ls $sel[0]`;
string $shortNameFR[] = `ls $sel[1]`;
string $shortNameBL[] = `ls $sel[2]`;
string $shortNameBR[] = `ls $sel[3]`;

//Ok, match each object with the name it is going to be given
if (`gmatch $shortNameFL[0] $nameFL` == 1) //If the name matches the one it's having right now, don't rename it, instead just append it to the $message
{
$message += "1st ";
}
else
{
rename $sel[0] $nameFL;
}

if (`gmatch $shortNameFR[0] $nameFR` == 1)
{
$message += "2nd ";
}
else
{
rename $sel[1] $nameFR;
}

if (`gmatch $shortNameBL[0] $nameBL` == 1)
{
$message += "3rd ";
}
else
{
rename $sel[2] $nameBL;
}

if (`gmatch $shortNameBR[0] $nameBR` == 1)
{
$message += "4th ";
}
else
{
rename $sel[3] $nameBR;
}

//Check the size of the message. If all objects have been renamed, there's nothing to report. We could report each object if it has been renamed or if it hasn't, but I just wanted to show how something like this could be done.
if (size($message) != 0)
{
$message += "object already have the correct name";
print $message;
}

}//end of else statement

rahulsg2901
12-09-2010, 04:09 AM
What im trying to do is selecting for objects, say in this case 4 cylinders, i choose them in a specific order, cylinder1, cylinder2, cylinder3, and finally cylinder 4.

What i want now is to rename those objects to left_frontwheel, right_frontwheel, left rearwheel, right_rearwheel.


to be honest i dont have a clue what im doing, just testing my way in the mel world really.

i have started with the simple list selection command. what i dont know is how to use the information that gets return. im making a string wich contains the selected objects. can i make a string wich contains the new name and use this to rename the objects in the first one? doh, really dont have a clue.


thanks!

chk out ths

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

CGTalk Moderation
12-09-2010, 04:09 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.