Hey, guys,
I want to create a bounding box for all selected objects in my scene and actually got help from an earlier post at: http://www.highend3d.com/boards/index.php?...09&hl=space.
The post explained very well the routine to get MBoundingBox in the world space.
so I wrote the following:
CODE
MSelectionList selection;
MGlobal::getActiveSelectionList( selection );
MDagPath dagPath;
MObject component;
std::vector vmbb = std::vector < MBoundingBox >();
MItSelectionList iter( selection, MFn::kTransform);
for (; !iter.isDone(); iter.next() )
{
stat=iter.getDagPath( dagPath, component );
if( stat )
{
MObject moTransform = dagPath.transform();
getBoundingBoxesAsWorld(moTransform, dagPath,vmbb);
}
}
for (size_t i = 1; i < vmbb.size(); i++)
vmbb[0].expand(vmbb[i]);
MPoint bCenter, bMax, bMin;
bCenter = vmbb[0].center();
bMax = vmbb[0].max();
bMin = vmbb[0].min();
then do my own things......
the getBoundingBoxesAsWorld function is copied from the code by Daniel_Faust on this earlier post as below (with a minor change to show where I have problem):
CODE
void getBoundingBoxesAsWorld(MObject &moTransform, const MDagPath &mdpBase, std::vector &vmbb)
{
MFnTransform mftTransform(moTransform);
unsigned int iChildCount =mftTransform.childCount();
for (unsigned int iChildIndex = 0; iChildIndex<iChildCount; iChildIndex++)
{
MObject moChild = mftTransform.child(iChildIndex);
if (moChild != mdpBase.node() )
{
if ( moChild.apiType() == MFn::kTransform || moChild.apiType()==MFn::kPluginTransformNode )
{
getBoundingBoxesAsWorld(moChild,mdpBase,vmbb);
}
else
{
MFnDagNode mfdnChild(moChild);
MBoundingBox mbbChild = mfdnChild.boundingBox();
MDagPath mdpChild;
mfdnChild.getPath(mdpChild);
MMatrix m1 = mdpBase.inclusiveMatrixInverse();
MMatrix m2 = mdpChild.inclusiveMatrix();
MMatrix mmiChild = m2*m1 -----------**----------
mbbChild.transformUsing(mmiChild);
vmbb.push_back(mbbChild);
}
}
}
}
but it seemed that the returned MBoundingBoxes are not always correct. for example, at the line I marked as ----*----, many times, m2m1 = identity matrix, so the bounding box end up still being placed at the origin rather than where it should be.
Anyone has some idea? Thank you!