View Single Post
# 14 28-05-2012 , 08:58 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,998
doh! sirCharles just pointed out a flaw in the original script. If you freeze the transforms on any object using setAttr with the x y z transforms to zero will not move the object at all so I made the following changes to the script.

I use the mel command "move -rpr 0 0 0 $obj" to move the object (relative to its rotate pivot) to the global 0 0 0 position. This means the pivot needs to be centered first which is accomplished with the "xform -cp" command.

Since this is a learning exercise I found the move -rpr command by creating an object and then snapping it to the global origin holding the x-key down and then looked in the script editor to see the command that was generated, then went to the mel reference and read up a little on the move command. This is how a figure out most commands in mel. I did the same thing for the freeze transform block of code. I picked an object and froze its transforms then looked at what was generated in the script editor.

I also added the parameter $ft to control if the objects transforms are frozen after being moved to the global origin. (0 -- don't freeze, 1 -- freeze)

I tack "rm" in front of scripts I write so I can tell at a glance what scripts are mine in the scripts folder so changed the name to rmCenterObjects, and you call it using rmCenterObjects 0 | 1; You can chage it to whatever you want just remember the proc name and the file name must match.

I am not a mel programmer and there are many ways to do things in maya so this may not be the best way but it seems to work.

Code:
// procedure: rmCenterObjects()
// usage: rmCenterObjecst(0|1);
// author: ctbram
// date: 05272012
//
// Place this script into your maya folders scripts folder and use by 
// selecting objects that you want to center  pivot and move to the 
// global origin ( 0 0 0 ).
//
// examples:
// 1. rmCenterObjects(0) moves the selected objects to the global origin while
// 2. rmCenterObjects(1) moves the selected objects to the global origin and
// freezes the objects transforms
//
// alternatively you can drop the parenthesis and use the format...
//
// rmCenterObjects 0; or rmCenterObjects 1;
//
// modified 05282012
//   added the $ft flag
//   use the move command rather then setAttr to account 
//     for objects that have had their transforms frozen

global proc rmCenterObjects(int $ft)
{
    string $sel[] = `ls -sl`;
    string $obj;

    // center each objects pivot, move it to the global origin and if
    // the $ft parameter is 1 then freeze its transforms...
    for ($obj in $sel)
    {
        // center $obj's pivot
        xform -cp $obj;
        
        // move the $obj to the global origin using move
        // relative to the rotation pivot -rpr
        move -rpr 0 0 0 $obj;
        
        // if $ft is 1 then freeze transforms after moving 
        // $obj to theglobal origin
        if ($ft == 1)
        {
           select -r $obj ;
           FreezeTransformations;
           makeIdentity -apply true -t 1 -r 1 -s 1 -n 0;
        }     
    }
    
    // restore the original selection
    select -r $sel;
}


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675

Last edited by ctbram; 28-05-2012 at 10:41 PM.