Is it possible to create an object with bmesh without use an existing mesh?

In the example that I found in API documentation is shown as create a bmesh starting from an existing mesh!
I’m wondering if it’s possible to create from scratch a bmesh with the associated object without initialize the bmesh from an existing mesh and how!
Can someone show me an example, please? Thanks


bm = bmesh.new()

after that you can push the data into an existing mesh, or not and write data to a file - or some other external purpose.

see:
http://www.blender.org/documentation/blender_python_api_2_63_release/bmesh.html#mesh-access
http://www.blender.org/documentation/blender_python_api_2_63_release/bmesh.html#bmesh.new

Thank you for the reply!
Maybe it’s wrong what I have understood about the bmesh, I was thinking that bmesh can completely replace the old way to make meshes, but probably it’s not correct and both mesh and bmesh are necessary!

As reported here with the new release “the mesh data is accessed in object mode and intended for compact storage, for more flexible mesh editing from python see bmesh”.
So mesh data is for acces in Object Mode and bmesh for editing in Edit Mode, is that correct?

The old way to add faces to a mesh now doesn’t work in 2.63 version, the mesh hasn’t the attribute faces, do you know how to do it with the ner release? Thanks in advance!

Maybe I have found the answer

Maybe this simple example about the use of the bmesh API could be usefull to somoeone!


import bpy
import bmesh

# Create mesh 
me = bpy.data.meshes.new('myMesh') 

# Create object
ob = bpy.data.objects.new('myObject', me)   

#ob.location = origin
ob.show_name = True

# Link object to scene
bpy.context.scene.objects.link(ob)

# Get a BMesh representation
bm = bmesh.new()   # create an empty BMesh
bm.from_mesh(me)   # fill it in from a Mesh

# Hot to create vertices
vertex1 = bm.verts.new( (0.0, 0.0, 3.0) )
vertex2 = bm.verts.new( (2.0, 0.0, 3.0) )
vertex3 = bm.verts.new( (2.0, 2.0, 3.0) )
vertex4 = bm.verts.new( (0.0, 2.0, 3.0) )

# Initialize the index values of this sequence.
bm.verts.index_update()
 
# How to create edges    
bm.edges.new( (vertex1, vertex2) )
bm.edges.new( (vertex2, vertex3) )
bm.edges.new( (vertex3, vertex4) )
bm.edges.new( (vertex4, vertex1) )

# How to create a face
# it's not necessary to create the edges before, I made it only to show how create 
# edges too
bm.faces.new( (vertex1, vertex2, vertex3, vertex4) )

# Finish up, write the bmesh back to the mesh
bm.to_mesh(me)

little late but thanks for this lol…was trying to figure out the bmesh way of doing this and stumbled across this post. worked like a charm (slight changes due to api changes)

I am glad to know that it helped someone, even after so many years :smiley:

do you know if there’s a way to add verts/faces without having to do them individually?

See my question here for better explanation: