Introduction to Maya - Modeling Fundamentals Vol 1
This course will look at the fundamentals of modeling in Maya with an emphasis on creating good topology. We'll look at what makes a good model in Maya and why objects are modeled in the way they are.
# 31 30-05-2012 , 08:41 PM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,998
I am calling this done. I will continue to think about how to resolve the bounding box rotation issue.


"If I have seen further it is by standing on the shoulders of giants." Sir Isaac Newton, 1675
# 32 31-05-2012 , 03:51 AM
ctbram's Avatar
Moderator
Join Date: Jan 2004
Location: Michigan, USA
Posts: 2,998
Okay, here is the final and I hope bug free version of rmToFloor(). The solution to the FreezeTransformations problem was pretty simple. I simply Freeze the transforms, get the ymin of the unrotated bounding box, then execute an undo command. Only Maya commands are placed into the undo queue (command start with capital letters and functions start with lower case).


Here is the script....

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
//
// 05302012 -
// (1) Before computing the lower pivot using the bounding box I had to freetransformation
// on each object to zero out the rotation and align the bounding box to the global axis.
// Until I can find a better solution this is going to have to be lived with.
//
// (2) solved the freezetransform problem by simply executing an undo command after getting
// the $lowest value.  Only Commands are placed into the undo queue so exactWorldBoundingBox
// is a function and is not queued.  So once I have $lowest simply running undo, undoes the
// FreeTransformation.
//
global proc rmToFloor(int $pc)
{
    string $sel[] = `ls -sl`;
    string $obj;
    int $undoFlag = false;   // keep track of toggling the undo queue
    
    // 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
         
    	 float $wsPivot[] = `xform -q -ws -rp $obj`;            // save world space $obj pivot
         
         // if the undo queue is off turn it on
         if (!(`undoInfo -q -st`))
         {
             $undoFlag = true;
             undoInfo -st true;
         } 
              
         select $obj; FreezeTransformations;    	        // ensure the bounding box is not rotated

         float $bbox[] = `exactWorldBoundingBox $obj`;          // get bounding box world space coords
         float $lowest = $bbox[1];                              // get lowest vert (ymin)
         
         undo;                                                  // undo the FreezeTransformation command

         // if the undo queue was off, turn it back off
         if ($undoFlag)
         {
             $undoflag = false;
             undoInfo -st false;
         }
         
         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
    }
    
    select $sel;  // retore the original selection
}


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

Last edited by ctbram; 31-05-2012 at 04:48 AM.
# 33 10-08-2013 , 05:12 AM
Registered User
Join Date: Aug 2013
Posts: 3

One liner

HI,
I just started scripting in maya a few days back.
Please do not mind me sounding stupid.I probably dont know what i am talking bout, but...
if all you want to do is select the objects and center the pivot plus move it to (0,0,0)..
Why don't u use:

makeIdentity;

I made multiple objects changed their pivot points and tried it...
i selected all the objects and just clicked on the button i made in my shelf.
It got it to (0,0,0) and centered the pivot point.

Aaron.user added image

# 34 11-08-2013 , 04:54 AM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
That doesn't physically move the object to the origin if you have moved the vertices themselves.


Imagination is more important than knowledge.

Last edited by NextDesign; 11-08-2013 at 04:56 AM.
# 35 13-07-2015 , 09:12 PM
Registered User
Join Date: Jul 2015
Posts: 1

here u go

move -ls 0 0 0;
rotate -a 0 0 0;
CenterPivot;

user added image

# 36 14-07-2015 , 02:12 AM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988

move -ls 0 0 0;
rotate -a 0 0 0;
CenterPivot;

user added image

That won't work if the vertices have been moved. Take a sphere, move the vertices up, then run your script. By moving the vertices, you never changed the transform node's attributes, so you script won't do anything. The script by Rick above looks at the bounding box of the vertices, so it knows how far to move the transform node to sit the object on the floor.

(Plus, you should be moving in global space, not local space.)


Imagination is more important than knowledge.
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads