Rotate bmesh around Vector with Python

I try to rotate a bmesh around a vector as axis.

vec = vert1.co + vert2.co
rot = mathutils.Euler(vec/max(vec)).to_matrix()
bmesh.ops.rotate(bm, cent=vert2.co, matrix=rot, verts=bm.verts, space=objCT.matrix_world)


But unfortunately it rotates in a strange way. How could I fix this?

• do you have some answer for this?? – yhoyo Oct 23 '17 at 14:19
• @batFINGER do you have some explanation to this issue?? – yhoyo Oct 27 '17 at 14:00
• @yhoyo I'm not sure what the q'er wants to do here. What vector are you getting by adding two (which two and why those two?) vert coordinates and dividing by max(vec) ( assume here:want a unit vector for that one vec.normalized() ) Euler(vec) is not gong to give any sensible rotation matrix, considering for a start the vec is in length units, the euler expects angles. rot = Matrix.Rotation(angle, size, axis) might be a better idea. – batFINGER Oct 27 '17 at 14:22

Rotate bmesh vertices around a vector defined by two selected verts.

Test script, rotates 45 degrees around the vector defined by two selected verts. Select two vertices and run script. Running 8 times should rotate the full sweep.

import bpy
import bmesh
context = bpy.context
from mathutils import Matrix

obj = context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

# have 2 verts selected.
v1, v2, *_ = (v for v in bm.verts if v.select)
axis = (v2.co - v1.co).normalized()
bmesh.ops.rotate(bm, cent=v2.co, matrix=rot, verts=bm.verts)

me.update()


Note: because our rotation matrix is using an axis and both v1 and v2 are on the axis, either (or any point on) can be used for cent property of operator . The centre point would be calculated via v1.co + axis / 2 or (v1.co + v2.co) / 2

I am trying to do something similar but running into problems, so this is not an answer but a question about a slightly more advanced usage of this script.

I am also trying to rotate bmesh around Vector, but I only want to rotate the selected faces, not the whole mesh, around the Active Edge. Also, before that, I want to do an extrusion with 0 distance, so that the rotation kind of has a new segment. My problem is that I get this extra geometry. I read that I am supposed to delete the geometry that bmesh.ops.extrude_face_region() uses to make new geometry. I have tried that as well but blender crashes every time when I use bmesh.ops.delete(). So I have that part commented out in my script. Could anyone help me avoid the weird extra geometry when using this script. Again, this will work with copy-past into the Text Editor and Run Script, while having a mesh in Edit mode with one Active Edge.

import bpy
import bmesh
context = bpy.context
from mathutils import Matrix

obj = context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
selected = [v for v in bm.verts if v.select]
face_sel = [f for f in bm.faces if f.select]

edge = bm.select_history.active
axis_rot = (edge.verts[0].co - edge.verts[1].co).normalized()

r = bmesh.ops.extrude_face_region(bm, geom=face_sel)

# DEL_VERTS = 1
# DEL_EDGES = 2
# DEL_ONLYFACES = 3
# DEL_EDGESFACES = 4
# DEL_FACES = 5
# DEL_ALL = 6
# DEL_ONLYTAGGED = 7

# bmesh.ops.delete(bm, geom=face_sel, context=DEL_FACES)

bmesh.ops.rotate(bm, cent=edge.verts[0].co, matrix=rot, verts=selected)

me.update()