Copying geometry from bmesh to a separate object using Blender Python API

When working with a bmesh object in Blender, sometimes there is a need to take part of the geometry and move it to a separate mesh. Let’s see how this can be done using Blender Python API.

Let’s take as an example an object in which we have several selected polygons. Create a bmesh object and transfer data from our current object to it.

Also match the indices of the bmesh vertices and polygons to the indices of the original mesh.

Note, that we mast work in the “object” mode.

Now we need to move the selected polygons into another object.

List of selected polygons:

Get a list of vertices for all selected polygons.

And a list of their coordinates.

Now we need to get a list through which we can match the vertex indices for the polygons from the “selected_faces” list with the ordinal arrangement of the vertices in the “vertices” list.

Create a dictionary where the keys are the vertex indices, and the values ​​are the ordinal position in the vertices list.

And associate each polygon with the ordinal position of its vertices indices.

Having received all the necessary data for transferring the selected geometry to a new mesh, we can delete it in the current object.

Now create a new mesh based on the data we got from the original mesh.

And link it to the scene to the same collection.

After that, the new object will appear in the 3D viewport.

Note that we didn’t actually “copy” the geometry, but read the data from the original mesh and created a completely new mesh based on that data. Therefore, in this case, the new mesh will lack materials, textures, scans, etc., which could exist on the original mesh. We also worked in local coordinates, and if the original mesh had unapplied transformations, then the coordinates of the vertices of the new mesh must be additionally multiplied by the world matrix of the original mesh.

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments