getting bounding box of object in object space

438 views
Skip to first unread message

James Kim

unread,
Dec 3, 2018, 6:38:48 PM12/3/18
to Python Programming for Autodesk Maya
I'm trying to get the size of any object using the bounding box

def getSizes(self):

objSize = cmds.ls(sl = True)



#get the bounding box of x,y,z
sizeMinX = cmds.getAttr(objSize[0] + '.boundingBoxMinX')
sizeMaxX = cmds.getAttr(objSize[0] + '.boundingBoxMaxX')
sizeMinY = cmds.getAttr(objSize[0] + '.boundingBoxMinY')
sizeMaxY = cmds.getAttr(objSize[0] + '.boundingBoxMaxY')
sizeMinZ = cmds.getAttr(objSize[0] + '.boundingBoxMinZ')
sizeMaxZ = cmds.getAttr(objSize[0] + '.boundingBoxMaxZ')

#get the sizes
if (sizeMinX < 0 and sizeMaxX > 0) or (sizeMaxX < 0 and sizeMinX > 0):
sizeX = abs(sizeMinX) + abs(sizeMaxX)
else:
sizeX = abs(sizeMaxX) - abs(sizeMinX)


if (sizeMinY < 0 and sizeMaxY > 0) or (sizeMaxY < 0 and sizeMinY > 0):
sizeY = abs(sizeMinY) + abs(sizeMaxY)
else:
sizeY = abs(sizeMaxY) - abs(sizeMinY)


if (sizeMinZ < 0 and sizeMaxZ > 0) or (sizeMaxZ < 0 and sizeMinZ > 0):
sizeZ = abs(sizeMinZ) + abs(sizeMaxZ)
else:
sizeZ = abs(sizeMaxZ) - abs(sizeMinZ)

This works fine when the object is perpendicular to the x,y, or z axis
however when i rotate the object it gives me different sizes. for example if i have a cube(10,10,10) it will return these values normally but when i rotate the object it will give me different values.
Is there a way to get the bounding box of the object not in world space?

Marcus Ottosson

unread,
Dec 4, 2018, 1:40:56 AM12/4/18
to python_in...@googlegroups.com
I think what you're looking for is called Axis-Aligned Bounding Box. In the simple case, if you have a single object you'd like to know the bounding box for, you could build a transformation matrix out of those coordinates, and multiply that with the transform of your object. That would give you a new transformation matrix, aligned to that object. However that would stop working once you move vertices around or when there is more than one object involved.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3cab85d2-1c4e-4ae5-9ca6-15be3f3307a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Boon

unread,
Dec 4, 2018, 3:45:16 AM12/4/18
to Python Programming for Autodesk Maya
(Marcus: I thought axis-aligned bounding box specifically meant world-space axis aligned.  AABBs are used for culling so all AABBs need to be in the same space.)

The xform command has switches that should do what you want:
-boundingBox(-bb)
query

Returns the bounding box of an object. The values returned are in the following order: xmin ymin zmin xmax ymax zmax.
-objectSpace(-os)
createquery

treat values as object-space transformation values (only works for pivots, translations, rotation, rotation axis, matrix, and bounding box flags)

Also note, sizeX = xmax-xmin, regardless of whether one or both is > 0, so you probably don't need all that code in your example.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

James Kim

unread,
Dec 4, 2018, 12:47:03 PM12/4/18
to Python Programming for Autodesk Maya
I tried using xform before but it still gives me the same problem where rotating the object gives me different values. Perhaps there is another way of getting the size of an object? I want something like the tool you get when you press ctrl+t which shows the size of any selected object.

Michael Boon

unread,
Dec 4, 2018, 4:34:19 PM12/4/18
to Python Programming for Autodesk Maya
Using OpenMaya you can get the object-space bounding box of a MFnDagNode.
PyMel exposes that function, which might be the easiest way to get it. something like this (untested!)
import pymel.core as pm

obj
= cmds.ls(sl=True)[0]
pymelObj
= pm.PyNode(obj) # Alternatively you could just do pymelObj = pm.selected()[0]
bb
= pymelObj.boundingBox()
sizeX
= bb.width()
sizeY
= bb.height()
sizeZ
= bb.depth()

James Kim

unread,
Dec 4, 2018, 5:33:58 PM12/4/18
to Python Programming for Autodesk Maya
Thanks but its still giving me the same result. I'm looking into using the api right now

Michael Boon

unread,
Dec 4, 2018, 5:58:29 PM12/4/18
to Python Programming for Autodesk Maya
Ah, you need to select the shape node. If you call boundingBox on the transform, you get world space (or something, not local space anyhow)
Reply all
Reply to author
Forward
0 new messages