The Bounding Box is a rectangular box, circumscribed around all vertices of the mesh. Usually, Bounding Box is used for
fast calculations of the physical object’s interaction, for example, collisions.
For each mesh in the scene, the Bounding Box is calculated automatically and recalculated every time the mesh is changed.
We can get the coordinates of all Bounding Box corners for any object through its “bound_box” property, which is an array of the “bpy_prop_array” type with a set of coordinates. Each element of the “bound_box” array, is the array of point coordinates:
1 2 3 |
[bbox_co[:] for bbox_co in bpy.context.object.bound_box[:]] # [(-1.328185796737671, -0.8224414587020874, -0.9786988496780396), (-1.328185796737671, -0.8224414587020874, 0.9557293057441711), ...] |
The object’s Bounding Box coordinates are in its local coordinate system. To get the global coordinates of the Bounding Box (in the scene coordinate system), we need to multiply its local coordinates by the object’s world matrix.
1 2 3 |
[bpy.context.object.matrix_world @ Vector(bbox_co[:]) for bbox_co in bpy.context.object.bound_box[:]] # [Vector((-0.8632605671882629, -0.7490095496177673, 0.94121915102005)), Vector((-0.8632605671882629, ... ] |
It should also be in mind that the Bounding Box is calculated for the base object, its coordinates depend, for example, if object modifiers are enabled or not.
To get the final coordinates of the Bounding Box, we need to use the evaluated object. We can get the evaluated object from the base object using the depsgraph:
1 2 |
depsgraph = bpy.context.evaluated_depsgraph_get() object_evaluated = bpy.context.object.evaluated_get(depsgraph) |
Now we can get the final coordinates of the Bounding Box:
1 2 3 |
[bpy.context.object.matrix_world @ Vector(bbox_co[:]) for bbox_co in object_evaluated.bound_box[:]] # [Vector((0.14480701088905334, 1.7784899473190308, 1.9723942279815674)), Vector((0.7954444885253906, ... ] |
For example, let’s create an empty in each corner of the Bounding Box of the object. Cycling through the list of the final coordinates of the Bounding Box, add an empty for each of them and set its location equal to the corner coordinates:
1 2 3 4 5 6 7 8 9 |
for co in bbox_cos_global: empty = bpy.data.objects.new( name='empty', object_data=None ) bpy.context.scene.collection.objects.link( object=empty ) empty.location = co |
The final code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import bpy from mathutils import Vector depsgraph = bpy.context.evaluated_depsgraph_get() object_evaluated = bpy.context.object.evaluated_get(depsgraph) bbox_cos_local = [bbox_co[:] for bbox_co in object_evaluated.bound_box[:]] bbox_cos_global = [bpy.context.object.matrix_world @ Vector(bbox_co) for bbox_co in bbox_cos_local] for co in bbox_cos_global: empty = bpy.data.objects.new( name='empty', object_data=None ) bpy.context.scene.collection.objects.link( object=empty ) empty.location = co |
*.blend and *.py files with the example for my subscribers on Patreon