Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created May 26, 2015 19:45
Show Gist options
  • Save zeffii/43ee4e2e9ce15a09bb43 to your computer and use it in GitHub Desktop.
Save zeffii/43ee4e2e9ce15a09bb43 to your computer and use it in GitHub Desktop.
bmesh_from_pydata, pydata_from_bmesh
import bmesh
def bmesh_from_pydata(verts, edges=None, faces=None):
''' verts is necessary, edges/faces are optional '''
bm = bmesh.new()
add_vert = bm.verts.new
bm_verts = [add_vert(co) for co in verts]
bm.verts.index_update()
if faces:
add_face = bm.faces.new
for face in faces:
add_face([bm_verts[i] for i in face])
bm.faces.index_update()
if edges:
add_edge = bm.edges.new
for edge in edges:
edge_seq = bm_verts[edge[0]], bm_verts[edge[1]]
try:
add_edge(edge_seq)
except ValueError:
# edge exists!
pass
bm.edges.index_update()
return bm
def pydata_from_bmesh(bm):
v = [v.co[:] for v in bm.verts]
e = [[i.index for i in e.verts] for e in bm.edges[:]]
p = [[i.index for i in p.verts] for p in bm.faces[:]]
return v, e, p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment