I am working on a script-thingy where I need to be able to rotate an object so that it faces the same direction as a vector [x,y,z] passed as an argument. I am afraid my math skills are a bit low concerning vectors and matrices, but I seem to have been able to hack something together using the cgkit computer graphics library. It works by rotating each vertex (point) in the mesh around the axis perpendicular to the directional vector. This is not a very portable solution, though. And it generates zero-divide exceptions for certain directions.

```
def rotatePoint(vector, point):
"""Returns (x,y,z) of point rotated to face the same direction as vector."""
dirVec = cgtypes.vec3(vector[0], vector[1], 0)
angleVec = cgtypes.vec3(0,0,1).cross(dirVec) # Cross product
if vector[0] != 0:
hypotenuse = math.sqrt(vector[0]**2+vector[2]**2)
else:
hypotenuse = math.sqrt(vector[1]**2+vector[2]**2)
if hypotenuse == 0: hypotenuse = 0.0000000000000001 # Close enough, but prevents zero divide. A practical hack.
r_angle = math.acos(vector[2]/hypotenuse)
if vector[0] == 0 and vector[1] == 0: # This clause catches cases where directional vector is straight up or down
if vector[2] == 0: return point # If directional vector is null vector, return the point unmodified
elif vector[2] > 0:
angleVec = cgtypes.vec3(1,0,0)
r_angle = 0
elif vector[2] < 0:
angleVec = cgtypes.vec3(1,0,0)
r_angle = math.pi
rotMatr = cgtypes.mat4(1).rotate(r_angle, angleVec)
result = rotMatr*cgtypes.vec3(point[0], point[1], point[2])
return (result[0],result[1],result[2])
```

Now, I suppose it is possible to code something like this together with the Mathutils library. But I have been unable to produce good results using the Mathutils.RotationMatrix function.

Is someone able to help me?