PDA

View Full Version : how to get local bounding box values?


cgbeige
12-16-2012, 06:12 PM
I'm making a simple pivot toolbox and have it set up for world space bbox coordinates but I'm wondering how to get these values as something that can be input into the xform -piv command:

http://www.can-con.ca/tumblrpics/localtransform.png

That's an arbitrarily rotated object so world space doesn't work and querying/setting object space sets the pivot in the wrong spot. I want to be able to set the pivot to any point on the local bounding box (mid, min, max). I realize Local Tools does this but I just want to know for myself

zaskar
12-16-2012, 08:26 PM
Now you have the shape node with its local bounding box information and the transformation matrix of the parent transform. With both you can calculate the world space points of the local bbox midpoints as you sketched. Either use some python api or faster, use mel:

{
string $sel[] = `ls -sl`;
string $shape[] = `ls -dag -shapes $sel[0]`;
string $mat = `createNode pointMatrixMult`;
connectAttr ($sel[0] + ".worldMatrix") ($mat + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),$bbOSMax[2]};
setAttr ($mat + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $result[] = `getAttr ($mat + ".output")`;
print $result;

delete $mat;
}

cgbeige
12-17-2012, 03:26 PM
thanks - it was more work than I thought it would be so this is a big help.

cgbeige
12-17-2012, 05:35 PM
ok - I seem to be almost there but these min/max values aren't correct for some reason - the mid ones work fine. If you can figure out where I'm wrong, it would be great. This is the first time I'm dealing with matrixes for stuff like this.

//START LOCATION BBOX proc
string $sel[] = `ls -sl`;
//do the X coords
switch($currentXPivotMenuSelection) {
case "min":
for ($n=0 ; $n<size($sel) ; $n++)
{
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//minPoint of +x
float $zPositive[] = {$bbOSMin[0],(($bbOSMin[1] + $bbOSMax[1]) * .5),$bbOSMin[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $resuloot[0] $resuloot[1] $resuloot[2] $sel[$n];
}
break;
case "mid":
for ($n=0 ; $n<size($sel) ; $n++)
{
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),(($bbOSMin[2] + $bbOSMax[2]) * .5)};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $resuloot[0] $resuloot[1] $resuloot[2] $sel[$n];

}
break;
case "max":
for ($n=0 ; $n<size($sel) ; $n++)
{
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {$bbOSMax[0],(($bbOSMin[1] + $bbOSMax[1]) * .5),$bbOSMax[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $resuloot[0] $resuloot[1] $resuloot[2] $sel[$n];

}
break;
default:
$currentXPivotMenuSelection = "min";
}
//do the Y coords
switch($currentYPivotMenuSelection) {
case "min":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//minPoint of +y
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),($bbOSMin[1]),$bbOSMin[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $resuloot[1] $resuloot[2] $sel[$n];
}
break;
case "mid":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),(($bbOSMin[2] + $bbOSMax[2]) * .5)};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $resuloot[1] $resuloot[2] $sel[$n];

}
break;
case "max":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),($bbOSMax[1]),$bbOSMax[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $resuloot[1] $resuloot[2] $sel[$n];

}
break;
default:
$currentYPivotMenuSelection = "min";
}
//do the Z coords
switch($currentZPivotMenuSelection) {
case "min":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//minPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),$bbOSMin[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $currentPivCoord[1] $resuloot[2] $sel[$n];

}
break;
case "mid":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;//problematic
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),(($bbOSMin[2] + $bbOSMax[2]) * .5)};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $currentPivCoord[1] $resuloot[2] $sel[$n];

}
break;
case "max":
for ($n=0 ; $n<size($sel) ; $n++)
{
float $currentPivCoord[]=`xform -q -ws -piv $sel[$n]`;
string $shape[] = `ls -dag -shapes $sel[$n]`;
string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
//midPoint of +z
float $zPositive[] = {(($bbOSMin[0] + $bbOSMax[0]) * .5),(($bbOSMin[1] + $bbOSMax[1]) * .5),$bbOSMax[2]};
setAttr ($matrixZee + ".inPoint") $zPositive[0] $zPositive[1] $zPositive[2];
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $currentPivCoord[0] $currentPivCoord[1] $resuloot[2] $sel[$n];

}
break;
default:
$currentZPivotMenuSelection = "min";
}

zaskar
12-17-2012, 08:38 PM
Hard to explain what the failure in your code above is, english isnt my favourite language. So here is a simplified but fully working version of your code:
string $sel[] = `ls -sl`;
for ($n=0 ; $n<size($sel) ; $n++){

string $shape[] = `ls -dag -shapes $sel[$n]`;
float $bbOSMin[] = `getAttr ($shape[0] + ".boundingBoxMin")`;
float $bbOSMax[] = `getAttr ($shape[0] + ".boundingBoxMax")`;
float $bbXValues[] = { $bbOSMin[0], (($bbOSMin[0] + $bbOSMax[0]) * .5), $bbOSMax[0]};
float $bbYValues[] = { $bbOSMin[1], (($bbOSMin[1] + $bbOSMax[1]) * .5), $bbOSMax[1]};
float $bbZValues[] = { $bbOSMin[2], (($bbOSMin[2] + $bbOSMax[2]) * .5), $bbOSMax[2]};

float $pivX, $pivY, $pivZ;
switch($currentXPivotMenuSelection) {
case "min":
$pivX = $bbXValues[0];
break;
case "mid":
$pivX = $bbXValues[1];
break;
case "max":
$pivX = $bbXValues[2];
break;
default:
$pivX = $bbXValues[0];
};

switch($currentYPivotMenuSelection) {
case "min":
$pivY = $bbYValues[0];
break;
case "mid":
$pivY = $bbYValues[1];
break;
case "max":
$pivY = $bbYValues[2];
break;
default:
$pivY = $bbYValues[0];
};

switch($currentZPivotMenuSelection) {
case "min":
$pivZ = $bbZValues[0];
break;
case "mid":
$pivZ = $bbZValues[1];
break;
case "max":
$pivZ = $bbZValues[2];
break;
default:
$pivZ = $bbZValues[0];
};

string $matrixZee = `createNode pointMatrixMult`;
connectAttr ($sel[$n] + ".worldMatrix") ($matrixZee + ".inMatrix");
setAttr ($matrixZee + ".inPoint") $pivX $pivY $pivZ;
float $resuloot[] = `getAttr ($matrixZee + ".output")`;
xform -ws -piv $resuloot[0] $resuloot[1] $resuloot[2] $sel[$n];

delete $matrixZee;
};
select $sel;

cgbeige
12-17-2012, 09:03 PM
awesome - thanks. This works great.

CGTalk Moderation
12-17-2012, 09:03 PM
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.