get Dimensions of an object using python?

I know how to get the scaling of an object, but in my case that doesn’t help. I need the size aka dimensions (which you can change in the transform panel). Is there any way to do this?

http://www.blender.org/documentation/blender_python_api_2_66a_release/bpy.types.Object.html?highlight=dimensions#bpy.types.Object.dimensions

maybe this ?

^ That probably won’t help since it uses the bpy module, which isn’t accessible in the BGE, at least, not in a runtime.

I made a function that does this. It loops through all of the vertices and gets the largest distance between these on all axes. It multiplies these distances by the object’s world scale, which should be the dimensions of the object. Here it is. Since it’s looping through all of the vertices in the mesh, it would be wise to perform it only once when you need to, and then store the result somewhere. It only works for one mesh, though you can specify it. It shouldn’t be a problem, though, as I have yet to find a circumstance where an object in the BGE has more than one mesh.



def GetDimensions(object = None, roundit = 3, offset = 1, meshnum = 0):

    """
    Gets the dimensions of the object (what you see under dimensions in the properties window in the 3D menu).
    mesh = which mesh to use to get the object's dimensions.
    roundit = how far down to round the returned dimension values; set it to a negative number to not round the numbers off at all.
    offset = Whether or not to return the offset point of the dimensions (the center point);
    This negated (-offset, literally) is the origin point, generally.
    meshnum = The index of the mesh to use. Usually 0 is okay.
    """

    if object == None:    
        object = logic.getCurrentController().owner
        
    s = object.worldScale
    
    mesh = object.meshes[meshnum]

    #print (dir(mesh))
    
    verts = [[], [], []]
    
    originpos = [0, 0, 0]
    
    for mat in range(len(mesh.materials)):
        
        for v in range(mesh.getVertexArrayLength(mat)):
        
            vert = mesh.getVertex(mat, v)
            
            pos = vert.getXYZ()
            
            verts[0].append(pos[0])
            verts[1].append(pos[1])
            verts[2].append(pos[2])
            
    verts[0].sort()
    verts[1].sort()
    verts[2].sort()

    if offset != 0:
    
        offsetpos = [
            (verts[0][len(verts[0])-1] + verts[0][0]) / 2,
            (verts[1][len(verts[1])-1] + verts[1][0]) / 2,
            (verts[2][len(verts[2])-1] + verts[2][0]) / 2,
            ]
    
    if roundit >= 0:
        size = [
        round( (verts[0][len(verts[0]) - 1] - verts[0][0]) * s[0] , roundit),
        round( (verts[1][len(verts[0]) - 1] - verts[1][0]) * s[1] , roundit),
        round( (verts[2][len(verts[0]) - 1] - verts[2][0]) * s[2] , roundit) ]
    else:
        size = [(verts[0][len(verts[0]) - 1] - verts[0][0]) * s[0],
        (verts[1][len(verts[0]) - 1] - verts[1][0]) * s[1],
        (verts[2][len(verts[0]) - 1] - verts[2][0]) * s[2]]

    #originpos = [0, 0, 0]
            
    if offset:
        return (mathutils.Vector(size), mathutils.Vector(offsetpos))
    
    else:
        return (mathutils.Vector(size), None)


Thank you SolarLune; it works perfectly. :smiley: