[maya] Object Aligned Bounding Box?

Can I query a bounding box in an object’s local space?

I can see that Maya is maintaining an Object-Aligned Bounding Box when I change the display to Bounding Boxes in the viewport. However, I cannot figure out a way using either the ‘polyEvaluate’ or ‘xform’ commands to retrieve this information - the bounding box is always returned as an Axis Aligned Bounding Box.

Thanks.

If you were able to retrieve the object’s geometry, you could probably create your own bounding box.
Well, I don’t know if Maya provides object measurement via code, but that could be something to check out.

You need to use the API to get the object-space bounding box and then transform the min/max by the objects transformation matrix:


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()

2 Likes

Wow, thanks for the thorough example!

No problem. I needed to make sure my idea worked before I suggested it:)