Matching the bounding box size of two objects with Python


#1

I have two objects that have different starting scales and bounding box sizes.

I need to match the bounding box sizes, but I’m not sure of the best way to approach it. I was thinking of doing a very small scale increment up/down until the boxes match, but that could get a little crazy.

Is there a good way to match with less steps? Any help would be appreciated!


#2

Get the overall size for each axis of the bounding box, then get the difference between the two objects’ bounding box size per axis, and multiply the scale of the object to modify by that value.
E.G:

import maya.cmds as mc

objA = mc.ls(sl=1)[0]
objB = mc.ls(sl=1)[1]

objA_BoundRaw = mc.xform(objA, q=1, bb=1)
objB_BoundRaw = mc.xform(objB, q=1, bb=1)

objA_Bound = [objB_BoundRaw[3]-objB_BoundRaw[0], objB_BoundRaw[4]-objB_BoundRaw[1], objB_BoundRaw[5]-objB_BoundRaw[2]]
objB_Bound = [objA_BoundRaw[3]-objA_BoundRaw[0], objA_BoundRaw[4]-objA_BoundRaw[1], objA_BoundRaw[5]-objA_BoundRaw[2]]


objBScaleOld = mc.xform(objB, q=1, s=1)
boundDifference = [objA_Bound[0]/objB_Bound[0], objA_Bound[1]/objB_Bound[1], objA_Bound[2]/objB_Bound[2]]
objBScaleNew = [objBScaleOld[0]*boundDifference[0], objBScaleOld[1]*boundDifference[1], objBScaleOld[2]*boundDifference[2]]
mc.xform(objB, scale=objBScaleNew)

This obviously isn’t the most optimised code by far, but it demonstrates the principle.


#3

Thanks a bunch man, this seemed to work!