# How do I rotate an object by theta and phi?

I am trying to translate theta and phi rotation in Blender to the Euler XYZ rotation about each axis. To do this I am currently using the following code: """

 def Sphere2Euclid(theta, phi):
"""
Inputs: theta - Angle as defined in the GA
phi - Angle as defined in the GA

Outputs: x - The x rotation angle in Euler Angle Coordinates
y - The y rotation angle in Euler Angle Coordinates
z - The z rotation angle in Euler Angle Coordinates

Description: This function will take the theta and phi angles as defined in the GA and convert them to Euler Coordinates

Source for conversion: https://blender.stackexchange.com/questions/158377/object-rotation-with-polar-angles-python
"""

return x, y, z


This current code doesn't seem to rotation from a global orientation as it does not produce the correct orientation for testing angles. Is my method of converting from theta and phi to XYZ wrong or am I fundamentally misunderstanding how blender handles rotation?

• it should be z = math.cos(math.radians(theta)) i tested and it's correct. Commented Apr 18, 2023 at 4:08

You're almost there. Your z calculation was incorrect. It should be angle $$\theta\space(theta)$$ instead of $$\phi\space(phi)$$:

z = math.cos(math.radians(theta))


Also take note you are also missing the radius, but I assume it is just a unit sphere ($$r=1$$) so I guess it's not a problem if excluded.

To visualize the Spherical Coordinate System, here is a drawing I made where: $$v=\theta \space (theta)$$ $$u=\phi \space (phi)$$

This results in:

$$x=r\sin \theta \cos \phi$$ $$y=r\sin \theta \sin \phi$$ $$z=r\cos \theta$$

I made an example where $$\theta=67.5$$ and $$\phi=33.75$$ on a unit sphere ($$r=1$$)

After executing the script and checking the console I can see that the vertex location on the euclidean plane has been calculated correctly.

Working script here with example:

import bpy
import math

def Sphere2Euclid(theta, phi):
"""
Inputs: theta - Angle as defined in the GA
phi - Angle as defined in the GA

Outputs: x - The x rotation angle in Euler Angle Coordinates
y - The y rotation angle in Euler Angle Coordinates
z - The z rotation angle in Euler Angle Coordinates

Description: This function will take the theta and phi angles as defined in the GA and convert them to Euler Coordinates

Source for conversion: https://blender.stackexchange.com/questions/158377/object-rotation-with-polar-angles-python
"""