Rotate a vector
import bpy
from math import pi
from mathutils import Vector, Euler, Matrix, Quaternion
v = Vector((1.0, 1.0, 1.0))
#point which will be rotated around the cursor
cursor_loc = bpy.context.scene.cursor_location
rot_mat = Matrix.Rotation(pi / 2.0, 3, 'Z')
v_new = rot_mat * (v - cursor_loc) + cursor_loc
There are further options available (see api docs):
You can construct a matrix, this is most efficient if you want to apply the same transformation on many points.
mat = (Matrix.Translation(cursor_loc) *
Matrix.Rotation(pi / 2.0, 4, 'Z') *
Matrix.Translation(-cursor_loc))
v_new = mat * v
You can use quaternions:
# using (axis, angle) constructor
q = Quaternion((0.0, 0.0, 1.0), pi / 2.0)
v_new = q * (v - cursor_loc) + cursor_loc
or eulers:
eu = Euler((0.0, 0.0, pi / 2.0), 'XYZ')
v_new = v - cursor_loc
v_new.rotate(eu) # works for quat's and matrix types too
v_new += cursor_loc
Rotate an object
mat = (Matrix.Translation(cursor_loc) *
Matrix.Rotation(pi / 2.0, 4, 'Z') *
Matrix.Translation(-cursor_loc))
obj.matrix_world = mat * obj.matrix_world
Euler
and Quaternion
both offer a method to convert them to a matrix:
q = Quaternion()
eu = Euler()
mat = q.to_matrix().to_4x4()
mat = eu.to_matrix().to_4x4()