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)