mel

Maya Mel Script - Drop Pivot to Bottom of object?


I see someone asked this before (for example: here). So how to automatically set the pivot to the bottom of the model?


Solution

  • To do that:

    Step 1: find the bottom point.

    Step 2: Set the pivot to the bottom point.

    Here is the mel script help you do that:

        string $sel[]= `ls -sl`;
    
        //$sel[0] != "" to check if the first item is empty, but `size $sel` == 1 already cover that
        if(`size $sel` > 0)
        {
            int $vtxIdx;
            int $vCount[];
            float $lowestY = 2147483647.0;
            float  $crtY = 0.0;
            float $pos[];
    
            string $item;
            for ($item in $sel)
            {
                $vCount = `polyEvaluate -vertex $item`; //Get vertex count
                for ($vtxIdx = 0; $vtxIdx < $vCount[0]; $vtxIdx++)//Loop through vetex
                {
                    $pos = `xform -q -ws -t ($item+".vtx["+$vtxIdx+"]")`;//Get vertex position
                    $crtY = $pos[1];
                    if($crtY < $lowestY)
                    {
                        $lowestY = $crtY;//Get the lowest Y
                    }
                }
                $pos = `xform -q -ws -t ($item)`;
                xform -ws -a -piv $pos[0] $lowestY $pos[2] ($item);
                print ($lowestY);
            }
    
        }
    

    Usage:

    step 1: select the objects that need to set pivot to bottom

    step 2: execute the script

    The pivot point should be set like this Pivot to bottom If your requirement is: the pivot point must be inside the model, then you should edit this script a little bit.