Vectors From-To Quaternion

I’m looking for a Blender API math function that takes in two (unit) vectors and spits out a Quaternion that when rotating the first vector would generate the second vector.

Basically the Blender equivalent of Unity’s Quaternion.FromToRotation() function.

I would expect Blender to have it, but for the life of me I can’t find it in the mathutils documentation or anywhere else.

Well, whatever it is, I got my issue solved. It was easy enough to create it from scratch.

def ShortestArc(v1, v2):
    # https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another
    a = v1.cross(v2)
    q = mathutils.Quaternion()
    q.x = a.x
    q.y = a.y
    q.z = a.z
    q.w = math.sqrt((v1.magnitude ** 2.0) * (v2.magnitude ** 2.0)) + v1.dot(v2)
    q.normalize()
    return q

I’m also a befuddled that I can’t find a magnitude-squared function for the Vectors. I guess if I dot the vector by itself there might be a little optimization there, or just unroll the operation out by hand?

But, if there’s still an official function out there, I’d be grateful to know about it.

To get a quaternion representing the rotation from v to v2 you can use:
q = v.rotation_difference(v2) (https://docs.blender.org/api/blender2.8/mathutils.html#mathutils.Vector.rotation_difference)

Some notes regarding OP’s equation:

To get the squared magnitude/length of a vector v you can use:
v.length_squared

Also note that the equation assumes that the direction of the vectors are not opposite of each other, which needs to be handled in a separate branch (if statement) similar to the solution in:
http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm

Thank you so much!