PDA

View Full Version : MEL - Move Object's Pivot


cppgraphics
03-10-2011, 02:43 PM
Hello folks,

I have 5 objects, which are rotated into different X direction. I would like to move the pivot of every object to its bottom. But through MEL how to find the exact bottom middle position of all given objects?

Someone please help me to find it out.

Thanks in advance.

haiddasalami
03-10-2011, 03:59 PM
Loop through objects and query bounding box. Do some simple math to average out the x and z .

Nyro
03-10-2011, 04:18 PM
Do you mean move the pivot of each individual object to that object's bottom center or to virtual center of all objects combined (basically an average of their positions)?

For the first case (each object's pivot individually):

$mySelection = `ls -sl -type "transform"`;
string $obj;

for ($obj in $mySelection) {

float $BBox[] = `xform -q -ws -bb $obj`; // get the bounding box info

xform -ws -p true -cp $obj; // center pivot

float $centerPos[] = `xform -q -ws -sp $obj`;

xform -ws -piv $centerPos[0] $BBox[1] $centerPos[2] $obj; //move Pivot to bottom, but keep in center
}


For the second option, use this code. NOTE: it will mess with your hiearchy, if you have previously grouped the objects into a custom hierarchy. All selected objects will end up parented to the World. But it's just a proof of concept, modify to suit your needs:

$mySelection = `ls -sl -type "transform"`;
string $obj;
// temporarily group objects for convenience
group -n "pivotTempGrp" -w $mySelection;

// query average center of objects and BoundingBox for minY (lowest point in Y)
float $objCenter[] = `objectCenter pivotTempGrp`;
float $objBB[] = `xform -q -ws -bb pivotTempGrp`;

// set the pivot for each object accordingly to centerX, minY, centerZ
for ($obj in $mySelection) {

xform -ws -piv $objCenter[0] $objBB[1] $objCenter[2] $obj;
}

// delete temporary group
ungroup pivotTempGrp;

The code above will determine the center of all objects combined in X and Z and the lowest point on Y (basically, the very bottom of the lowest object in the selection). All values in Worldspace for convenience.

cppgraphics
03-11-2011, 01:12 AM
Nyro, thanks mate. I need to get the first method to be done.

I think my objects are moved in Object Space, I will check it. For new objects it works perfectly.

CGTalk Moderation
03-11-2011, 01:12 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.