Remove a Vertex from a bMesh?

Hi All,

I am looking for a way to remove a vertex from a mesh.

I thought it would be as simple as this…


ob = bpy.data.objects["Cube"]
me = ob.data
me.vertices.remove(0)

But there is no remove method for vertices. It can be done in the interface, how do we do it under code?

It’s almost as simple as that :wink:

Every operation that can be performed in Blender’s interface can also be found in the bpy.ops module.


bpy.ops.mesh.delete()

Be advised though, this operation will work exactly as deleting vertices from a menu, so it will only work while in edit mode, and it will remove all vertices that are marked as selected. However, selecting vertices and toggling edit mode can also be done using Python script, so this shouldn’t be a problem.

You can also take a more low-level approach by rebuilding your mesh from scratch, like in this tutorial.

Yeah, I am afraid I need something that does not require a mode or context it needs to operate directly upon the data.

I guess I will have to rebuild a new mesh using the original mesh as a source and just include one less vertex.

Seems kind of inefficient:confused:

Hi Atom,

If you want to do mesh editing through Python I recommend you use the bmesh module as this is what it was designed for. Here is an example where the active object is a mesh.

import bpy, bmesh
ob = bpy.context.active_object
me = ob.data
bm = bmesh.new()
bm.from_mesh(me)
 
# First vertex
v = bm.verts[0]
 
# Remove this vert
bm.verts.remove(v)
 
# Write the mesh back
bm.to_mesh(me)

For nicer mesh element removal and editing have a look into bmesh.utils module. Also, this script requires the object to be in object mode, if you want to edit the mesh in editmode, then replace:


bm = bmesh.new()
bm.from_mesh(me)

with

bm = bmesh.from_edit_mesh(me)

and remove the to_mesh() call at the end.

Hope this helps a bit.

Cheers,
Truman

2 Likes

Thank you for the example, I did not realize there was another layer to bMesh. With all the noise about it replacing the standard mesh model I just assumed it was all built in to the mesh.

@Truman: thanks from me as well. The bMesh module does indeed look a lot friendlier than what I’ve worked with up until now. This will speed things up nicely.