Vector, Quaternion, Matrix

Various 3D mathematics related stuff

Matrix - Quaternion - Vector

Rotate around one point

From here

In order to rotate around an arbitrary point the process is:

  1. translate the object to location (0, 0, 0)
  2. rotate the object
  3. rotate the location
  4. translate it back to the new location

This is how it looks in code:

import math
import bpy
import mathutils

deg = 90.0 # the angle
center = mathutils.Vector((0, 0, 0)) # the point
obj = bpy.context.active_object

# get the rotation matrix (deg is negated - bpy goes clockwise, mathutils counter-clockwise
rot_matrix = mathutils.Matrix.Rotation(math.radians(-deg), 4, (1, 0, 0))

# decompose the world matrix
orig_loc, orig_rot, orig_scale = obj.matrix_world.decompose()
orig_rot_matrix =   orig_rot.to_matrix().to_4x4()
orig_scale_matrix = mathutils.Matrix.Scale(orig_scale[0],4,(1,0,0)) @ mathutils.Matrix.Scale(orig_scale[1],4,(0,1,0)) @ mathutils.Matrix.Scale(orig_scale[2],4,(0,0,1))

# this does the job
#                       1. translate to center
#         2. rotate     |
#         |             |                  3. translate back
#         |             |                  |
new_loc = rot_matrix @ ((orig_loc - center) + center)

# make a matrix out of the new location 
new_loc_matrix = mathutils.Matrix.Translation(new_loc)

# all together now
obj.matrix_world = new_loc_matrix @ rot_matrix @ orig_rot_matrix @ orig_scale_matrix