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 07, 2014

Bmesh_from_pydata

maybe there are easier ways, but this facilitated what I needed it for
def bmesh_from_pydata(verts=[], edges=[], faces=[]):
    """
    Return a BMesh built from verts, edges, and faces.
    
    :arg verts: The verts to use (required).
    :type context: list
    :arg edges: The verts to use (optional).
    :type context: list
    :arg faces: The verts to use (optional).
    :type context: list

    :return: a BMesh.
    :rtype: :class:`bmesh.types.BMesh`
    """

    if not verts:
        print("verts data seems empty")
        return

    bm = bmesh.new()
    [bm.verts.new(co) for co in verts]
    bm.verts.index_update()

    if faces:
        for face in faces:
            bm.faces.new(tuple(bm.verts[i] for i in face))
        bm.faces.index_update()

    if edges:
        for edge in edges:
            edge_seq = tuple(bm.verts[i] for i in edge)
            try:
                bm.edges.new(edge_seq)
            except ValueError:
                # edge exists!
                pass

        bm.edges.index_update()

    return bm