get bounding box in local space

get bounding box in local space

Craig_Lamorte
Advocate Advocate
8,425 Views
15 Replies
Message 1 of 16

get bounding box in local space

Craig_Lamorte
Advocate
Advocate

Hi,

 

I am looking to get the bounding box size of an object in local space, not in world space.  So no matter which way the object is rotated it will always give me the same X, Y, and Z.

 

I did try this, but it only does it in world space.  (the object I am selecting is a group node with meshes underneath it

cmds.xform(mesh, bb=True, q=True, os=True) 

I know I can also do this, but i dont see any flags to do local space, so it just returns in world space as well

cmds.getAttr("mesh.boundingBoxSizeX")

Are there any commands that will give me the bounding box in local space?

 

Thanks

Craig

 

0 Likes
8,426 Views
15 Replies
Replies (15)
Message 2 of 16

olarn
Advocate
Advocate
Message 3 of 16

hagen.deloss
Alumni
Alumni

Hi @Craig_Lamorte 

 

I'm curious if you still need any tech support help, or if @olarn comment helped resolve the issue? 

 

 



Hagen Deloss
Community Manager | Media & Entertainment
Installation & Licensing forums | Contact product support | Autodesk AREA


 

0 Likes
Message 4 of 16

Craig_Lamorte
Advocate
Advocate

Still have not found a solution.  Tried looking at those links, looks like one of them using maya api, but I am not really sure how to use it, there aren't any examples, and documentation is confusing.

0 Likes
Message 5 of 16

hagen.deloss
Alumni
Alumni

@Craig_Lamorte 

 

I was recommended this solution that may prove useful. 

 

import maya.OpenMaya as OM

def Run():
# Get the current selection.
# The first object in the selection is used, and is assumed
# to be a transform node with a single shape directly below it.
selectionList = OM.MSelectionList()
OM.MGlobal.getActiveSelectionList(selectionList)

selectedPath = OM.MDagPath()
selectionList.getDagPath(0, selectedPath)

# Get the transformation matrix of the selected object.
transform = OM.MFnTransform(selectedPath)
m = transform.transformationMatrix()

# Get the shape directly below the selected transform.
selectedPath.extendToShape()

fnMesh = OM.MFnMesh(selectedPath)
bounds = fnMesh.boundingBox()


center = bounds.center()
min = bounds.min()
max = bounds.max()

print('Center: (%f, %f, %f)' % (center.x, center.y, center.z))
print('Min: (%f, %f %f)' % (min.x, min.y, min.z))
print('Max: (%f, %f %f)' % (max.x, max.y, max.z))
print('')

# Transform the bounding box min/max by the objects transformation matrix.
minTransform = min * m
maxTransform = max * m
print('Transformed Min: (%f, %f %f)' % (minTransform.x, minTransform.y, minTransform.z))
print('Transformed Max: (%f, %f %f)' % (maxTransform.x, maxTransform.y, maxTransform.z))

Run()

 

Found on this website here. It looks like this may take some knowledge in Python scripting. 

 

 



Hagen Deloss
Community Manager | Media & Entertainment
Installation & Licensing forums | Contact product support | Autodesk AREA


 

Message 6 of 16

olarn
Advocate
Advocate

 

In addition to @hagen.deloss  's solution, this is the MFnDagNode/MFnTransform version. 

It's a standalone function, just give it transform node name and it will return tuple of min max point values.

(MFnTransform also gives out AABB)

 

I believe it will also gives:

  • AABB for any "shape" node other than mesh (nurbs, fluid container etc)
  • Cumulative bounding box at any point in the hierarchy without manual calculation.

 

import maya.api.OpenMaya as om2


def giv_transform_local_aabb_plz(partial_name):
    msellist = om2.MSelectionList()
    msellist.add(partial_name)

    try:
        trn_dag = msellist.getDagPath(0)
    except IndexError:
        return None

    local_bound = om2.MFnDagNode(trn_dag).boundingBox

    return (
        (
            local_bound.min.x,
            local_bound.min.y,
            local_bound.min.z,
        ),
        (
            local_bound.max.x,
            local_bound.max.y,
            local_bound.max.z,
        ),
    )


def dewit():
    import pymel.core as pm
    print(giv_transform_local_aabb_plz(
        pm.ls(sl=1)[0].name()
    ))

dewit()

 

there aren't any examples 

http://help.autodesk.com/view/MAYAUL/2019/ENU/?guid=Maya_SDK_MERGED_cpp_ref_examples_html

0 Likes
Message 7 of 16

Craig_Lamorte
Advocate
Advocate

Thanks for the examples.  I did try both of these and unfortunately it still seems to be giving me the world space bounding box.

 

I created a box, sized it to x = 50, Y = 75, Z = 100.  Ran the bounding box calculations and got this -

Transformed Min: (-25.000000, -37.500000 -50.000000)
Transformed Max: (25.000000, 37.500000 50.000000)
rotated the box by 45 degrees on the Y and got -

Transformed Min: (-53.033009, -37.500000 -17.677670)
Transformed Max: (53.033009, 37.500000 17.677670)

 

I was hoping it would be the same result, because the dimensions of the box did not change and if it is rotated at weird angle, I would like to know the actual dimensions of the object, and not of a box put around it.

0 Likes
Message 8 of 16

hagen.deloss
Alumni
Alumni

@Craig_Lamorte 

 

I got another suggestion from the Maya development team, not sure if this has been suggested before, but doesn't hurt to post it anyway 😄

 

Try querying all of the components instead of the shape itself.
e.g. If mesh = "pCube1"


Instead of this:

cmds.xform(mesh, bb=True, q=True, os=True)


Try this:

cmds.xform(mesh+".vtx[*]", bb=True, q=True, os=True)

 

Let me know if you have follow up questions!

 

 



Hagen Deloss
Community Manager | Media & Entertainment
Installation & Licensing forums | Contact product support | Autodesk AREA


 

Message 9 of 16

Craig_Lamorte
Advocate
Advocate

Tried it, and it only seems to give me this no matter the size

 

[-0.5, -0.5, -0.5, 0.5, 0.5, 0.5]

0 Likes
Message 10 of 16

olarn
Advocate
Advocate

That might have been the true bounding volume of your shape, in (untransformed) object space.

Transformation effects scale too, not just translation and rotation.

0 Likes
Message 11 of 16

olarn
Advocate
Advocate

It is axially-aligned so the shape changes as the object rotate

https://www.youtube.com/watch?v=C8jyLB9ji6g

Local in this sense means it's measured against immediate parent's axis, not the scene.

0 Likes
Message 12 of 16

Craig_Lamorte
Advocate
Advocate

ah yeah, I was hoping there was a way to get a bounding box that is not axially-aligned.  A bounding box that would rotate with an object.  Not sure if this is even possible.

0 Likes
Message 13 of 16

hagen.deloss
Alumni
Alumni

Hi @Craig_Lamorte 

 

It sounds like @olarn is spot on regarding the bounding box reflecting the objects current dimensions. It may be possible for the development team to implement a tool like you are describing in later releases though! I would recommend posting to the Maya Ideas forum here so others can vote on this feature suggestion 😄

 

Best of luck on your project!

 

 



Hagen Deloss
Community Manager | Media & Entertainment
Installation & Licensing forums | Contact product support | Autodesk AREA


 

0 Likes
Message 14 of 16

mspeer
Consultant
Consultant

Hi!

Not sure what's the problem here, but this gives me correct local space data and ignores transform values of the transform node as these are shape only data. Of course this has to be used with the Shape node only.

cmds.getAttr("Shape.boundingBoxMinX")
cmds.getAttr("Shape.boundingBoxMinY")
cmds.getAttr("Shape.boundingBoxMinZ")

cmds.getAttr("Shape.boundingBoxMaxX")
cmds.getAttr("Shape.boundingBoxMaxY")
cmds.getAttr("Shape.boundingBoxMaxZ")

Please show me an example scene where this does not work.

0 Likes
Message 15 of 16

olarn
Advocate
Advocate

Seems like you wanted the "boundingBox" the way it's displayed in viewport bounding box override?

Volume-bound to untransformed to shape but moved, rotate, scaled to expected object position(but in local space)?

 

Then @hagen.deloss 's first solution would probably be the nearest to what you wanted. (you have to make out the rest of the box corners on your own)

 

These "boundingBox" are only useful for visualization tho. To detect intersection etc you still have to get either the normal AABB or transform back to object space..

 

mbb1.JPGmbb2.JPG

Message 16 of 16

abasarabUSBZV
Community Visitor
Community Visitor

wow! Thank you that's works for my case!!

0 Likes