3
$\begingroup$

I want to delete vertices that are not part of a face (as for example the selected ones in the image) using python. What I tried so far is:

  • switch to face select mode
  • select all faces
  • switch to vertex select mode
  • invert selection
  • delete vertices

This method is very unstable though, blender crashes every other time.

enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$

This code will do literally the same thing as you suggest. It operates on the active object. Change the line that starts object = to the object you want, if you don't want to use the active object.

import bpy
import bmesh

object = bpy.context.active_object

if object.mode == 'OBJECT':
    bm = bmesh.new()
    bm.from_mesh(object.data)
else:
    bm = bmesh.from_edit_mesh(object.data)


bm.verts.ensure_lookup_table()
bm.faces.ensure_lookup_table()

for face in bm.faces:
    face.select_set(True)

for vert in bm.verts:
    vert.select_set(not vert.select) 
    
for vert in bm.verts:
    if vert.select:
        bm.verts.remove(vert)

if object.mode == 'OBJECT':
    bm.to_mesh(object.data)
    bm.free()
else:
    bmesh.update_edit_mesh(object.data)

There are other ways to do this; but this is the simplest to follow. For example, the two loops through bm.verts can be combined to one loop, by noting that all of the verts you want to delete are deselected after you've selected all the faces. So those two loops become simply

for vert in bm.verts:
    if not vert.select:
        bm.verts.remove(vert)
$\endgroup$
1
  • $\begingroup$ Thank you so much! Took me a bit to implement it in my code but now it works! $\endgroup$
    – Timmethy
    Mar 14, 2022 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .