
You can use the Vector.angle
function to find the angle between an arbitrary axis vector (like the Z axis "up" vector), and the 3D cursor. This will provide you the angle you need to rotate your object to align it with the cursor in that specific axis.
import bpy
from mathutils import Vector
curloc = bpy.context.scene.cursor_location # Cursor location
up = Vector((0,0,1)) # Positive Z axis
right = Vector((1,0,0)) # Positive X axis
forward = Vector((0,1,0)) # Positive Y axis
o = bpy.context.object # Active object
# Calculate angle between up and cursor vectors and set as Y rotation
o.rotation_euler.y = up.angle( curloc )
Notice that in the example above I'm aligning the object with the cursor on the Y axis specifically. You can change this by replacing the axis letter on o.rotation_euler
, to align with a different axis.