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




On Wednesday, 5 December 2018 04:47:03 UTC+11, James Kim wrote:
>
> 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.
>
> On Tuesday, December 4, 2018 at 12:45:16 AM UTC-8, Michael Boon wrote:
>>
>> (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*) 
>> [image: query]
>>
>> Returns the bounding box of an object. The values returned are in the 
>> following order: xmin ymin zmin xmax ymax zmax.
>> *-objectSpace*(*-os*) 
>> [image: create][image: query]
>>
>> 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.
>>
>> On Tuesday, 4 December 2018 17:40:56 UTC+11, Marcus Ottosson wrote:
>>>
>>> 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.
>>>
>>> On Mon, 3 Dec 2018 at 23:38, James Kim <[email protected]> wrote:
>>>
>>>> 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?
>>>>
>>>> -- 
>>>> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/python_inside_maya/3cab85d2-1c4e-4ae5-9ca6-15be3f3307a5%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/python_inside_maya/3cab85d2-1c4e-4ae5-9ca6-15be3f3307a5%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/6af47157-344e-44fc-b8d7-b8730e9cf2d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to