# BMesh object rotation

I create a BMesh from a list of vertices like that:

mesh = bpy.data.meshes.new(str(self.shape) + "_Mesh")
obj  = bpy.data.objects.new(str(self.shape) + "_Object", mesh)

bm = bmesh.new()
bm.from_mesh(mesh)

for v in self.shape.vertices:
bm.verts.new(v)

bm.verts.index_update()
bm.faces.new(bm.verts)
bm.to_mesh(mesh)
bm.free()

It works, the mesh is created, but the rotation is set to 0,0,0.

Any idea how I can set the roation by the rotation of the first face for instance.

I know I can get the normal like this

bm.faces[0].normal

Best

See this answer for an explanation of how to determine the axis you want to rotate around. For example if you have two selected vertices, v1 and v2, this code will generate the necessary axis

axis = (v2.co - v1.co).normalized()

If you want to use the bounding box center of your mesh as the center of rotation you can use

from mathutils import Vector
center = sum((Vector(b) for b in object.bound_box), Vector())
center /= 8

replacing object with whatever object reference you are using.

The mechanism for rotating a bmesh around a standard axis is demonstrated by this code

from mathutils import Matrix, Vector
from math import radians
rotation_X = Matrix.Rotation(radians(45), 4, 'X')
bmesh.ops.rotate(bm, cent=center, matrix=rotation_X, verts=bm.verts)

Replace the quoted X with axis computed above to use an arbitrary axis.