Python: return size of object

Is there any way to do this? I can return the scale (which isn’t useful) but I can’t find anything for width/height.

Even if I could return the bounding box coordinates I’d be able to calculate the width and height but there doesn’t seem to be anything for this either :confused:

Any help would be great :slight_smile:

own = GameLogic.getCurrentController().owner

print (own.scaling)

Not sure if that works in 2.5, but it should. It gives you the scale on the x,y,z in I don’t know the units… I think it may be Blender units, but I could be wrong :confused:

You can also set the scale like so:

own = GameLogic.getCurrentController().owner

own.scaling = [5,5,5]


With a bit of python you could figure out a way of programming your own “bounding box”. Like maybe a couple of child boxes, one above and one below your object. At the beginning of each frame they they move until they touch your object and then you get the distance between them. Then they move apart to a relative position that you know will never touch. Then they move in again using a do… until loop. This all happens at the beginning of a single frame.

But I think physics visualization does this. If you rotate a cube with pv on you see the bounding box changing in size. Maybe there is a way of getting the bb size but I don’t know right now if you get a single bb for a complex object that might be rotating or changing in shape…

Getting an objects scale wont give you the objects size. I don’t think there is a way to get an objects size in real time. Given that many game objects don’t often change their size (even if they do the bounding box wont change unless it’s set to triangle mesh), I think the easiest way to do it would be to work out the dimensions of the object prior to run-time and save them in a variable if they’re needed in a script at some point. While in edit mode, in the editing panel (F9) there’s an option to show edge length that can help with this.

Hope this helps.

Thanks everyone for the suggestions.

I am a complete beginner at Python (I’ve been learning for about 3 days) and this is my first programming language, so everything’s new to me.

All the same I think I’ve written something that calculates the dimensions in realtime. If people who are more experienced at Python can take a look over it and let me know if it makes sense and is appropriately optimised I would be very grateful.

cont = GameLogic.getCurrentController()
owner = cont.owner

#Get Number of Vertexes
VertexQty = owner.meshes[0].getVertexArrayLength(0)
#Define Vertexe Number Variable
VertexNumber = 0
#Define Blank Lists to hold XYZ coordinates
vxlist = []
vylist = []
vzlist = []
#Go through each vertex and establish coordinates. Append XYZ values to self respective lists.
for i in range(0, VertexQty):
	vx, vy, vz = owner.meshes[0].getVertex( 0, i).getXYZ()
#Establish the biggest and smallest values
smallestx = min(vxlist)
biggestx = max(vxlist)
smallesty = min(vylist)
biggesty = max(vylist)
smallestz = min(vzlist)
biggestz = max(vzlist)
#Define dimensions
xdimension = biggestx + smallestx*-1
ydimension = biggesty + smallesty*-1
zdimension = biggestz + smallestz*-1
#Print Dimensions
print "X Dimension ="
print xdimension
print "Y Dimension ="
print ydimension
print "Z Dimension ="
print zdimension

Thanks again for everyone’s input :slight_smile: