Creating a polygon in the plane of another mesh polygon using the Blender Python API

When working with geometry in Blender using the Blender Python API, we may need to create a new polygon in the current mesh that lies in the same plane as one of the existing polygons of the mesh.

 

For simplicity, we’ll assume we’re in object editing mode and don’t switch modes.

First, create a BMesh object and copy the geometry of the currently active mesh into it. Also recalculate the vertex table for correct indexing.

Set the initial coordinates for the new polygon. Let’s say it’s a square centered at (0.0, 0.0) and has a side length of 2. We define the initial coordinates in the 2D space of the polygon’s plane.

To manipulate mesh coordinates in the global coordinate system, we’ll get a pointer to our mesh’s transformation matrix and also make an inverted copy of it.

Again, for simplicity of example, let’s assume our original mesh consists of just one triangular face, in the plane of which we need to construct a new polygon. In real cases, we can take, for example, three selected points on any polygon of the mesh.

Get the coordinates of three points of the mesh polygon in the global coordinate system.

Now, based on these three points, we can calculate the “basis”—the transformation matrix defined by these three points.

Add an offset to the basis so that the starting point of the basis coincides with the first of the three points of our original mesh polygon.

Now we have a ready-made transformation matrix for recalculating coordinates into the plane of the original polygon.

Сonvert the 2D coordinates of our new polygon into 3D coordinates using the basis.

Add new points to the BMesh using the calculated coordinates.

And fill them with a polygon.

For correct display, recalculate the normals.

That’s all. Now we can return the updated geometry from the BMesh object to the active mesh in the scene and clear the BMesh we no longer need.

This creates a new polygon in the mesh that lies exactly in the plane of the original polygon from which we took three points.

Note that the center of the new polygon lies at the point with index 0 on the original polygon, as this is what we set as the center of the coordinate system in the basis.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted