View Single Post
# 25 29-05-2012 , 11:39 AM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,998
The pivot thing was buggin me and I could not sleep so I made a new version of the program and added a pivot control parameter (int $pc) that determines if the object space (relative) or word space (global) position of the pivot is maintained or if the pivot is simply centered.

Code:
// procedure rmToFloor(int $pc)
// Move the lowest point of all objects in the selection so they sit exactly on the 
// top of the grid (global Y=0).
//
// params:
// int $pc - pivot control - (0 | 1 | 2)
// 0 = maintain object space (relative) pivot position
// 1 = maintain world space (global) pivot position
// 2 = center the pivot
// 
// usage: rmToFloor( 0 | 1 | 2);
// Copy this script to your maya scripts folder and source it and then make a selection of all
// the objects you want to move so they are sitting on the grid and enter the command
// rmToFloor 0 | 1 | 2;. 
//
// notes: 
//(1) This procedure depends on the mel command "exactWorldBoundingBox $obj" which 
// returns a float[] xmin ymin zmin xmax ymax zmax.  I use ymin to temporarily set
// the Y position of $obj's pivot so I can move it along Y such that it sits on the grid. 
// Moving each object to the grid has a constant complexity O(1).  This procedure has a 
// complexity of O(n), where n is the number of objects in the selection.
//
// [strike](2) this procedure moves the object but leaves the objects pivot in the same worldspace
// location.  I am not sure of the value of doing this and may want to either keep the
// local (relative) pivot location or just center the pivot.
//
// [strike](3) changed the script to maintain the relative pivot when the object is moved.
//
// (4) changed script to accept a pivot control parameter to control if the object object
// space or world space pivot is maintained or if the pivot is simply centered.
// 
// author: ctbram with valuable input from nextdesign
// date: 05292012
//
// change log:
// 05292012 - 
// (1) changed the program so the relative pivot of the object is maintained rather
// then the world pivot
// (2) added $pc (pivot control) parameter to control how the pivot is handled when the 
// object is moved.  0 = maintain object space, 1 = maintain world space, 2 = center pivot
//
global proc rmToFloor(int $pc)
{
    string $sel[] = `ls -sl`;
    string $obj;
    
    // make sure the pivot control parameter is 0 | 1 | 2.
    if (!(($pc == 0) || ($pc == 1) || ($pc == 2)))
         error("pivot control parameter must be (0 | 1 | 2).");

    for($obj in $sel)
    {
         float $osPivot[] = `xform -q -os -rp $obj`;                // save the object space (relative) pivot
         
    	 // move the object using world space coordinates...
    	 float $wsPivot[] = `xform -q -ws -rp $obj`;                // save world space $obj pivot
         float $bbox[] = `exactWorldBoundingBox $obj`;              // get bounding box world space coords
         float $lowest = $bbox[1];                                  // get lowest vert (ymin)
         xform -ws -rp $wsPivot[0] $lowest $wsPivot[2] $obj;        // reset the pivot to the lowest point in world space
         move -rpr $wsPivot[0] 0 $wsPivot[2] $obj;                  // move the object to the grid
         
         // handle the pivot based on the pivot control parameter $pc
    	 if ($pc == 0)
    	     xform -os -rp $osPivot[0] $osPivot[1] $osPivot[2] $obj;    // restore the object space (relative) pivot
    	 else if ($pc == 1)
    	     xform -ws -rp $wsPivot[0] $wsPivot[1] $wsPivot[2] $obj;    // restore the world space (relative) pivot
    	 else
    	     xform -cp $obj;    // center the pivot
    }
}


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

Last edited by ctbram; 29-05-2012 at 11:54 AM.