Getting an on-screen bounding box for an object in Blender

For calculating simplified interactions between objects, bounding boxes are typically used—parallelepipeds whose volume completely encloses the entire mesh geometry. Because such a box has and uses only six points for calculations, unlike the mesh itself, which can have millions of points, all calculations are very fast. Every object has a built-in bounding box, and its display can be enabled in the mesh’s parameters. This bounding box operates in 3D scene space, but there could be some cases when we need to control object interactions in 2D screen space. There are no pre-calculated bounding boxes for the viewport, but we can get one ourselves using the Blender Python API.

To create an object’s bounding box in the 3D viewport, we first need to obtain the coordinates of all its points in viewport space.

To account for unapplied transformations and modifiers on the mesh, we obtain its evaluated data using depsgraph.

By accessing the object by the obtained “object_evaluated” pointer, we can get the coordinates of its points, calculated taking into account the effects of modifiers and transformations.

Get a list of our object’s global coordinates. To do this, we multiply each coordinate of the evaluated mesh by its world transformation matrix.

Now we need to convert the object coordinates from the 3D scene space to the 2D viewport space – we need to project the mesh points onto the viewport screen. For this, we can use the “location_3d_to_region_2d()” function.

We’ll get pointers to the area, the viewport itself, and its region for drawing.

Now we can get the screen coordinates for each vertex of our object.

Let’s define a function that will calculate the Bounding Box coordinates based on a list of vertex coordinates.

And call it, passing in paramter the resulting screen coordinates of the object.

The function returns four coordinates. These are the coordinates of the two points on the main diagonal of the calculated bounding box. From these, we can easily get the coordinates of the remaining two points of the bounding box.

We have obtained the bounding box coordinates for the object in the 3D viewport. We can then use them in the calculations we need.

For clarity and to verify the correctness of our calculations, let’s display the resulting screen bounding box in the viewport.

For drawing in the viewport, we will use the “gpu” module of the Blender Ptyhon API.

Prepare the data needed for drawing. We need the coordinates of four points to draw our Bounding Box.

Colors for each of these points.

And a shader.

Collect all the data into a batch for drawing.

Define the drawing function and add it to the viewport drawing handler. We’ll also redraw the 3D viewport so that the Bounding Box we’ve rendered become immediately visible.

Now we can clearly see the bounding box in the viewport for our object.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments