I've puzzled about this for a while, but haven't found an answer I understood. This snippet below does add geometry (3 verts and a face) but it also generates an error, Does going to object mode make the data reference in bm be cleared? if so, how does data.vertices
still get updated?
ReferenceError: BMesh data of type BMesh has been removed
Error: Python script fail, look in the console for now...
any ideas, this is the cut down script ( run it with a cube in edit mode)
import bpy, bmesh
obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)
bm.verts.new((2.0, 2.0, 2.0))
bm.verts.new((-2.0, 2.0, 2.0))
bm.verts.new((-2.0, -2.0, 2.0))
set_of_verts = set(bm.verts[i] for i in range(-3,0))
bm.faces.new(set_of_verts)
bpy.ops.object.mode_set(mode='OBJECT')
bm.to_mesh(obj.data)
I have a few scripts that might benefit using the BMesh module for adding geometry, I know how to do it in the pre BMesh way.
Edit, using answer: here's how to add tris and quads while staying in edit mode https://gist.github.com/zeffii/5668669
bm.verts[-3:]
. $\endgroup$