Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 29, 2013

Adding geometry to existing geometry using BMesh

I resist asking questions until that feeling starts to kick where there has to be a fast but illusive answer. I want to see blender.stackexchange progress out of beta, it has been pretty useful already!
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))

bm.faces.new((bm.verts[i] for i in range(-3,0)))

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))
bm.verts.new((2.0, -2.0, -2.0))

bm.faces.new((bm.verts[i] for i in range(-4,0)))

# update, while in edit mode
# thanks to ideasman42 for making this clear
# http://blender.stackexchange.com/questions/414/
bmesh.update_edit_mesh(obj.data)
Adding edges is also similar
# let's add two verts and connect them as an edge
bm.verts.new((0.0, 1.0, 1.0))
bm.verts.new((0.0, 0.0, 1.0))
bm.edges.new((bm.verts[-2], bm.verts[-1]))

# remember to do a    bmesh.update_edit_mesh(obj.data)