joints to string array

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  02 February 2013
joints to string array

Hi everyone, I'm having issues with this bit of code..

runs with the top joint of a chain selected:

global proc jgNewPrefix()
/*get prefix names*/
/*leave oldPrefix blank if no prefix exists*/
promptDialog -title "Get old prefix" -message "Enter the current prefix";
string $oldPrefix = `promptDialog -q`;
promptDialog -title "Get new prefix" -message "Enter the new prefix";
string $newPrefix = `promptDialog -q`;

       /*get children, remove any child that's not a joint, put in to array*/

        select -hi;
        string $correctJoints[] = `ls -sl -type "joint"`;
        string $buffer[];

        /*remove old prefix and rename*/
        for($correctJoint in $correctJoints)
       	  tokenize $correctJoint $oldPrefix $buffer;
          rename $correctJoint ($newPrefix + $buffer[0]);


The script works during first use, but when I duplicate my joint chain and try to run the script on it again, it only renames the first joint, then outputs

// Error: No object matches name //

I'm assuming that's because it can't find the children now that the parent's name has changed.

any suggestions?
  02 February 2013
Well, I've managed to get rid of the error by reversing the array before I try renaming elements. Now the chain is renamed starting at the last child going up.

However when I run the script a second time on a duplicated chain, it changes the prefix, but renames all the children to the name of the top joint chain selected when the script is ran.

Also after tokenize, I get a warning:
// Warning: Removing invalid characters from name.

Last edited by jgibz : 02 February 2013 at 09:26 PM.
  02 February 2013
Instead of selecting the hierarchy a better option would be listing the relatives as doing a selection will return pipe characters on duplicates. Also, be careful when duplicating joints as Maya wont give the children unique names and you'll get an Error: More than one object matches name.

        string $correctJoints[] = `ls -sl -type "joint"`;
        string $relatives[] = `listRelatives -ad $correctJoints[0]`;       
        appendStringArray($correctJoints, $relatives, `size($relatives)`);
  02 February 2013
To be honest, using tokenize to separate a prefix from a string strikes me as a rather odd way of going about it.

The most obvious methods I'd suggest are:

 for($correctJoint in $correctJoints) {
 	string $baseName = stripPrefixFromName($oldPrefix, $correctJoint);
 	string $newName = addPrefixToName($newPrefix, $baseName);
 	rename $correctJoint $newName;

 // or

 for($correctJoint in $correctJoints) {
 	string $newName = substitute($oldPrefix, $correctJoint, $newPrefix);
 	rename $correctJoint $newName;


Also, I would strongly discourage the use of 'select' when scripting. Selection is a step that is unique to user interaction, as it is purely a means for a user to tell the program interactively what objects to operate on. In scripting, you simply specify the objects (99% of all commands accept object names as arguments, only very few exclusively work on selected objects). Also, selecting objects severly slows down execution time. Granted, that might not be an issue with small scripts like these, but it's a bad habit nontheless

So, as to gathering a list of joints:

 string $selection[] = `ls -sl -head 1`;

 string $joints[] = `ls -dag -type "joint" $selection[0]`;

 for ($joint in $joints) {
  02 February 2013
thanks for the reply, certainly a faster way of reversing my array, but I'm not sure I understand why I should do

appendStringArray($correctJoints, $relatives, `size($relatives)`);
  02 February 2013
Thanks Nyro, I've used both of your suggestions and for the most part everything works, with the exception of the:
// Error: More than one object matches name //

that Mondo mentioned. which is troubling because this keeps the script from working on duplicated chains, which really the reason I'm trying to do this in the first place, so I can dup a chain and rename all the prefixes, like IK to FK and such.
  02 February 2013
If I duplicate special on the chain I can check on "assign unique child names" this fixes the issue, thanks guys.
  02 February 2013
always use long names when scripting.

the problem is that maya allows two joints to be named "joint1" as long as they have different parents.

When you ask Maya to rename "joint1", you need to tell maya which one by passing in either the shortest unique name, or the full name.

use `ls -sl -l` and `listRelatives -ad -f` to get the long names
  02 February 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 10:58 PM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.