# Align Object to Vector using python

I am trying to rotate an object using python, inline with a vector which gives me the direction I want (based on the difference between the last two coordinates of the curve I generated). I therefore have a vector which I'd like to align this object but not the XYZ angles. The options to specify rotation (XYZ Euler...) all seem to require some sort of angle definition. I've started playing with numpy with I'm not experienced enough to find the answer.

Would you be able to advise what would be the best way to align an object to a vector and (if necessary) how I could convert the vector coordinates in XYZ_Euler angles I could apply to the object?

for instance, Vector=[1,1,1] and I would like to rotate the object along this vector.

## update

Thanks guys, DirectionVector.to_track_quat('X', 'Z') did the trick very nicely!

#define direction
Vector=(1,1,1)
DirectionVector = mathutils.Vector(Vector)
#apply rotation
bpy.context.object.rotation_mode = 'QUATERNION'
bpy.context.object.rotation_quaternion = DirectionVector.to_track_quat('Z','Y')


Try this: DirectionVector.to_track_quat('X', 'Z').to_euler()

mathutils.Vector.to_track_quat in the docs

• Thanks. I don't know how to include linebreaks in this mini-markdown format so, see my comment below... Dec 5, 2014 at 18:01
• For me, for pointing a camera, what I needed was camera.rotation_euler = direction_vec.to_track_quat('-Z', 'Y').to_euler() Mar 31, 2019 at 5:20

This example uses Vector.rotation_difference or Vector.to_track_quat

import bpy
from mathutils import Matrix, Vector, Euler

obj = bpy.context.active_object
vec = Vector((1.0, 1.0, 1.0))

# object axis to align with vector vec
axis = Vector((1.0, 0.0, 0.0))

# rotation difference
q = axis.rotation_difference(vec)
# or
# track quaternion
q = vec.to_track_quat('X', 'Z')

loc, rot, scale = obj.matrix_world.decompose()

mat_scale = Matrix()
for i in range(3):
mat_scale[i][i] = scale[i]

obj.matrix_world = (
Matrix.Translation(loc) *
q.to_matrix().to_4x4() *
mat_scale)

• Hi Jerryno, could you please have a look at my question found here blender.stackexchange.com/questions/79344/… batFINGER suggested that this answer could help me solve what I'm trying to do. I'd appreciate it if you could advise me please.
– Tak
May 10, 2017 at 12:38

Yes thanks for that final hint. Super simple in my case.

[At first I had my up and nrml switched.]

up = Vector((0, 0, 1))
rot = up.rotation_difference(nrml).to_euler()
print("normal:", nrml, "rotation:", rot)