# Calculate angle between two vertices of two different objects

I have a scene in which there are two objects, a sphere and a pole: I would like to calculate the angle between two vertices: one belonging to the first object and the second belonging to the second object in such a way that then I can apply a rotation_euler to rotate the pole out of the sphere. The rotation point (= the origin of the pole) is already set well. So what I need is just the calculation of the angle between the two given points.
In the following picture I joined the two objects so I can show you which are the interesting points but keep in mind that these two objects are separate and I don't want to join them together: I know the indices of these two points for the two different objects. So, is there a way to calculate the angle between them so then I can apply a rotation_euler on the Z axis of the pole and bring it of the sphere in Python?

Hope it's clear.

• Things like this can be done with constraints and vertex groups. Is there a specific reason you want to do this all in Python? – dr. Sybren Jun 25 '17 at 10:53

I guess that when you ask the angle between vertices, you mean the angle between the two vectors, starting from the origin and going to the two vertice. Those Vectors are actually the coordinate of the vertice but I prefer to be clear to avoid misunderstanding since the angle between two points dont realy make sense.

So it is actually very easy to obtain an angle between two vectors, you can use the Blender implementation of vectors to do that:

mathutils.Vector((v0.x,v0.y,v0.z)).angle(mathutils.Vector((v1.x,v1.y,v1.z)))


(I usually prefer to work with noramlized() vector but I dont think it is necessary for Vector.angle()

BUT: if you want to compute the angle of the two vertice from the center of the sphere, then you need a vector starting from the center and going to each Vertice

You can do something like vector0 = vertice0.co - sphere.location to find this vector.

Keep in mind that if the object has some transformations, location, rotation and scale, you'll need to calculate the world vertice position. You can take a look there for this point

If you want to have a perfect mach of the two vertices when they dont have the same z coordinate, you'll need to rotate around a precies vector and not only the z axis. This vector can be find with a cross product that will give you a vector perpendicular to two other verctors. Your vectors do needs to be normalized in this case

axis = vector0.normalized().cross(vector1.normalized())


If you want tho obtain the angle so that the the rotation on the z axis will align the vertices, except in the z axis, you should compute the rotation on vectors based on a projection of the vertices on a plane with a z normal. To keep it simple in your case, you can juste set the z value to 0 for each vector then normalize them.

Tell me if you need some clarifications on some points.

• Your first guess was exactly what I was asking for. As you thought, yes, I meant the angle between the two vectors. I will use the blender implementation, as you suggest. Thank you for your very detailed explanation! – Rexam Jun 25 '17 at 12:25