# How can I get dimensions of an object and rename the object?

I have a simple object, lets say a cube.

I need to divide one face into maybe 10 parts in runtime.

I am now trying to add planes (by putting a plane in the second layout and addObject during runtime) instead of really divide the cube
and set their locations to the one of the face of the cube.

How before doing this, I think I need to find out the real dimensions of the cube so that I can divide the height of the cube by 10 to get the equal height of the planes.
And the second thing is I may need to rename the object, but I cannot rename it during runtime also.

Can anyone teach me how to get the real height of the cube and rename the added Object during runtime?
Thank you.

There isn’t really an easy or simple way to get an object’s dimensions in the BGE. However, it’s very easy if you already know the un-scaled dimensions of the object. All you have to do is multiply the known dimensions by the object’s scale. Here’s how you would do it with the default cube (whose dimensions are 2x2x2):

``````import bge

# the cube's known dimensions
CUBE_SIZE = [2, 2, 2]

# get the object
cont = bge.logic.getCurrentController()
cube = cont.owner

# scale the dimensions
cubeDimensions = [CUBE_SIZE[i] * cube.worldScale[i] for i in range(3)]
``````

As for changing object names, this isn’t possible, but it’s also completely unnecessary. The addObject method returns a reference to the newly created object which you can save and access later.

The BGE does not support mesh generation. So this is not possible and you have a valid approach top work around that.

You can iterate other all polygons of the mesh to access the vertices (see BGE API). You convert the object local vertex locations into scene space (or whatever space you need). Now you can determine the dimensions by extracting the min and max values of each axis (x,y,z).
This requires a few loops and some grasp on how to convert coordinates from one space to another.

You can’t and you do not need.

It is a common mistake to mix names with objects.
An object has a name.
A name does not have an object.

There is not really much that you can do with a name. You can search for objects with a given name (and/or other attributes). There can be none, one or multiple objects. When you found your object of interest you can store a reference to it. This way you can access the object by this reference rather than doing the search again (with the risk to get a different object).

Thank you for suggestions

i use mobious suggestion to “solve” my problem at this stage
because it is easy to do.

You don’t need to know the dimensions - you can get the dimensions as Monster says. It doesn’t require more than one loop, though, as far as I can tell.

Here’s a function for it from my BGHelper module:

``````

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 &gt;= 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)

``````

It returns a tuple, consisting of the dimensions of the object, and the offset from the center the object is.

Hmm, I’ve come across a similar situation. I think there should be a function added to the BGE for KX_GameObject, like getVolume() or getDimensions(type=sphere,box,etc) since this would be faster in C than Python, especially on more complex meshes. Or can this also be retrieved from the physics properties?

Note to self: add to BGE TODO list.

Now I have a problem of the same name of the objects.
As I have a loop to add some objects (the same object in the second layer) to the first layer (active layer). The name of the added objects seems to be as same as the name of the object in the second layer.

Here is parts of my code

``````
obj = scene.objects["Eq"]
obj.localPosition = cabinet.localPosition
cabinet_size = Utility.Common.getDimensions(cabinet, "cube")
u_height = 1 / 42
obj.worldScale = Vector([cabinet.worldScale[0], u_height * float(Equipment.uocc), 1])
obj.localPosition[1] = obj.localPosition[1] - cabinet_size[1] / 2 - 0.001
obj.localPosition[2] = obj.localPosition[2] - cabinet_size[2] / 2 -  (2 * u_height) / 2 + (2 * u_height) * float(Equipment.uloc)

``````

when only one “Eq” object is added, everything fine
but if more than one “Eq” objects are added.
I think because of the same names, it get the wrong object (should be always refer to the first added “Eq” object)
the transformation, relocation of the objects go wrong.

if there is simple way to solve it? or i had to add some property to the object and search it with name + property?

A) there is no one and second layer (from logic perspective). There are active and inactive object. KX_Scene.objects returns a list of active objects.

B) You are right with your assumption regarding the names. Have a look at the API documentation. You can get the reference of the added object. Simply use that ;).

``````

``````
``````
obj.localPosition = cabinet.localPosition
cabinet_size = Utility.Common.getDimensions(cabinet, "cube")
u_height = 1 / 42
obj.worldScale = Vector([cabinet.worldScale[0], u_height * float(Equipment.uocc), 1])
obj.localPosition[1] = obj.localPosition[1] - cabinet_size[1] / 2 - 0.001
obj.localPosition[2] = obj.localPosition[2] - cabinet_size[2] / 2 -  (2 * u_height) / 2 + (2 * u_height) * float(Equipment.uloc)