using bmesh I want to use cursor matrix to rotate some selected geometry (edges or faces). using just cursor rotation, and using rotation and location to rotate around cursor


1 Answer 1


I found the answer in API, right after asking the question, using bmesh.ops.rotate(bm, cent, matrix, verts, space, use_shapekey).
To rotate around it is important to use "space"

import bpy
import bmesh
from mathutils import Vector

class MESH_OT_curs_rot_2_geo(bpy.types.Operator):
    """geo rotation from cursor rotation"""
    bl_idname = "mesh.curs_rot_2_geo"
    bl_label = "geometry rotation from cursor rotation"
    bl_options = {'REGISTER', 'UNDO'}
    around_curs: bpy.props.BoolProperty()
    use_shapekey: bpy.props.BoolProperty()

    def poll(cls, context):
        return context.mode == 'EDIT_MESH'

    def execute(self, context):
        cao = context.active_object
        mw = cao.matrix_world
        me = cao.data
        curs = context.scene.cursor
        mat = curs.matrix @ mw.inverted() #if cao has rotation...
        #R = mat.to_3x3().to_4x4() #cursor rotation value
        #previous line not ok, if scale not applied. prefer this
        R = mat.to_quaternion().to_matrix().to_4x4() #cursor rotation
        bm = bmesh.from_edit_mesh(me)
        sel = [v for v in bm.verts if v.select]
        sk = self.use_shapekey
        if self.around_curs:
            loc = curs.location
            bmesh.ops.rotate(bm, cent=loc, matrix=R, space=mw, verts=sel, use_shapekey=sk) #rotation around cursor
            loc = sum((v.co for v in sel), Vector())/(len(sel))
            bmesh.ops.rotate(bm, cent=loc, matrix=R, verts=sel, use_shapekey=sk) #just rotation
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

 [1]: https://i.imgur.com/AFJ4VY1.gif

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .