Splitting the sequence of vertices into chunks to create polygons

To build polygons using the existing list of vertices, we need to split this list into chunks of the required length. One chunk for each polygon. For a good mesh topology, square polygons are usually built, so the list of vertices is best divided into blocks of 4 vertices.

In general, splitting a list of objects into blocks using Python is not difficult, but we need to take into account that each subsequent polygon uses two points from the previous polygon.

Therefore, having the initial list of points, for example

we can’t use a simple sequence of blocks like

but we need the following sequence of blocks

The blocks must overlap by two points.

Let’s write a function that takes a sequence of points in its parameters and returns the sequence, divided into chunks with the specified offset.

In the input parameters of the function, we pass a list of points “lst”, the number of elements in one block “n”, and the required value for overlapping points in blocks “offset”.

To test the function in action, let’s create a bmesh object from the current active mesh with just a sequence of vertices.

and call our funcion

We need blocks of 4 vertices, so in the “n” parameter we pass a value of 4, and accordingly the overlap of vertices will be equal to 2, we pass 2 in the “offset” parameter. In the “lst” parameter, we pass a list of mesh vertices.

As a result, we get a set of necessary chunks:

Using the resulting list of chunks, we can create polygons.

Checking for the number of points in a chunk is needed to avoid trying to create a polygon based on the last block, which will only have two vertices.

0 0 votes
Article Rating
Notify of

0 Comment
Inline Feedbacks
View all comments