123iamking
123iamking

Reputation: 2703

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?

Upvotes: 0

Views: 3316

Answers (3)

Thank you for the script! I add "CenterPivot" so the pivot position will be in the center-bottom of the mesh

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)`;
        CenterPivot;
        xform -ws -a -piv $pos[0] $lowestY $pos[2] ($item);            
        print ($lowestY);
    }

}

Upvotes: 0

Anuxi
Anuxi

Reputation: 1

Thank you! I used your code above, 123iamking but I found I wanted to drop pivot to world y0 instead of bounding box.

so I changed some stuff around.

Works on Maya 2020.

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)
{
    float $pos[];

    string $item;
    for ($item in $sel)
        {
            xform -ws -a -cp;
            CenterPivot;   
            $pos = `xform -q -ws -a -piv ($item)`;//Get piv position
            xform -ws -a -piv $pos[0] 0 $pos[2] ($item); //set pivot to 0 in world y
        }

};

Upvotes: 0

123iamking
123iamking

Reputation: 2703

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.

Upvotes: 0

Related Questions