Checking object visibility in the 3D Viewport area

In some cases, it may be necessary to determine whether a certain object is currently visible in the 3D viewport area in Blender or not. We can determine the visibility of an object in a viewport with the Blender Python API using the location_3d_to_region_2d function of the view3d_utils module.

First, we need to get the area of the 3D viewport by looping through all currently opened in Blender areas:

To call the location_3d_to_region_2d function, we need to get a pointer to the 3D region of the viewport area.

In addition to the 3D region, the workspace itself is divided into several separate regions:

  • HEADER – area menu region,
  • TOOL_HEADER – tools header region. Here are the buttons for changing the selection mode and the Options menu,
  • TOOLS – region with tools toolbar (Т – panel),
  • UI – region for user interface (N – panel),
  • HUD – region for floating panels,
  • WINDOW – main 3D viewport region.

We need to get a pointer to the WINDOW region:

Now, having pointers to two required regions of the viewport, we can call the location_3d_to_region_2d function, passing these pointers as parameters.

The location_3d_to_region_2d function projects a point from the scene onto the viewport screen. In other words, from the coordinates of a point in the three-dimensional space of the scene, this function calculates the two-dimensional coordinates of this point on the screen.

The coordinates of the central point (origin) of the currently active object we will pass to the function as the third parameter.

In a result, we got the coordinates of a point on the viewport screen.

Now we can check, if the point falls within the boundaries of the 3D viewport area, then the object is visible in the viewport, and if it goes beyond the boundaries – it is not visible.

Full code:

0 0 votes
Article Rating
Notify of

0 Comment
Inline Feedbacks
View all comments