# How to rotate past 180 degrees with python?

I need to rotate an object along arbitrary axis (local Z) in the range of 360 degrees. However, the rotate_axis method limits the angle value to 180 degrees and adds a sign (+/-) to allow for 360 degrees rotation. So if I need to animate a rotation of an object by more than 180 degrees it behaves in an undesired way (Animation 1).

This is the code from first animation: obj.rotation_euler.rotate_axis("Z", radians(40))

The second animation was made with manual input into rotation Z field.

What I'd like to achieve is shown in the second animation. I know it is sometimes difficult to come up with a solution without knowledge of what the end product should look like. The final effect is a cube that follows an animated path combined of multiple keyframes on a sphere. Like a car driving on the surface of the earth. (see below animations)

What I need to do is to rotate a cube (first picture below) so that its' local Z axis is aligned with a sphere normal (blue side up - second picture below), and its' local X axis is aligned with a line joining two visible plain axes (red side facing one of plains - third picture below). Now at the end ot this operations I need to have a 360 degrees data instead of +/-180 in order to avoid overrotation as shown in the first animation above.

Take a look at the animation at the end.

Here you can see what I am dealing with. When rotation around Z skips the 180 the cube is overrotating.

• Possibly related: blender.stackexchange.com/a/215778/60486 May 31 '21 at 14:49
• What if the cube is at a place on the sphere, where the normal of the sphere (and so the Z axis of the cube) points where the X axis of the cube is supposed to point as well? 🤔 i.imgur.com/K8SwAB9.png Jun 1 '21 at 8:21
• That is impossible. There are over 1000 cubes like this moving around the sphere using various paths. Moreover, the local X axis must always be perpendicular to normal, while the local Z must be always parallel to spheres’ normal. Jun 1 '21 at 8:35
• Yes, it's impossible for a vector A to be parallel to a vector C and a vector B to be perpendicular to a vector A and parallel to a vector D if vectors C and D are parallel - because that would mean that vector B by being parallel to D is also parallel to vector C, and therefore parallel to vector A, and yet it's supposed to be perpendicular to vector A. So you need such a vector B that is both parallel and perpendicular to vector A. Jun 1 '21 at 8:47

try this:

import bpy
import math

scene = bpy.data.scenes["Scene"]
mycube = bpy.data.objects['Cube']
mycube.rotation_mode = 'XYZ'

scene.frame_start = 1
scene.frame_end = 100

mycube.rotation_euler = (0, 0, 0)
mycube.keyframe_insert('rotation_euler', index=2 ,frame=1)
mycube.location = (0,0,0)
mycube.keyframe_insert('location', index=1, frame = 1)