How to keyframe a continuous rotation about a random axis in blender

I came across a question on blender stackexchange: How do I create a driver with a random value and apply that driver to multiple objects?

The reason he was asking that question is that he wanted to give various objects a random but physically plausible rotation. The question I intend to answer is how do I give an object in blender a continuous rotation about a random (but stable) axis?

Blender uses a quaternion representation based on a rotation θ about a normalized axis a
w = cos ( θ / 2 )
x = a x sin ( θ / 2 )
y = a y sin ( θ / 2 )
z = a z sin ( θ / 2 )
q = [ w , x , y , z ]

We will express our random rotation as the composition of one fixed orientation quaternion q 1 composed with another quaternion q 2 representing the spin which varies over time.

To pick q 1 we can use the Hypersphere Point Picking procedure. To pick a random axis we can use the Sphere Point Picking procedure, although I like to modify the procedure so that instead of calculating φ I pick a random z in the range [-1,1] and then calculate the x and y based on the random θ .

Once we have the random orientation q 1 and the varying rotation q 2 we want to compose them q 3 = q 1 q 2 . The formula for composing two quaternions is

w 3 = w 1 w 2 - x 1 x 2 - y 1 y 2 - z 1 z 2
x 3 = w 1 x 2 + x 1 w 2 + y 1 z 2 - z 1 y 2
y 3 = w 1 y 2 - x 1 z 2 + y 1 w 2 + z 1 x 2
z 3 = w 1 z 2 + x 1 y 2 - y 1 x 2 + z 1 w 2

When you substitute the axis+angle formulation for q 2 you end up with

w 3 = w 1 cos ( θ / 2 ) - x 1 a x sin ( θ / 2 ) - y 1 a y sin ( θ / 2 ) - z 1 a z sin ( θ / 2 ) x 3 = w 1 a x sin ( θ / 2 ) + x 1 cos ( θ / 2 ) + y 1 a z sin ( θ / 2 ) - z 1 a y sin ( θ / 2 ) y 3 = w 1 a y sin ( θ / 2 ) - x 1 a z sin ( θ / 2 ) + y 1 cos ( θ / 2 ) + z 1 a x sin ( θ / 2 ) z 3 = w 1 a z sin ( θ / 2 ) + x 1 a y sin ( θ / 2 ) - y 1 a x sin ( θ / 2 ) + z 1 cos ( θ / 2 )

If we regroup the equation around the cos and sin terms we get w 3 = w 1 cos ( θ / 2 ) + ( - x 1 a x - y 1 a y - z 1 a z ) sin ( θ / 2 ) x 3 = x 1 cos ( θ / 2 ) + ( w 1 a x + y 1 a z - z 1 a y ) sin ( θ / 2 ) y 3 = y 1 cos ( θ / 2 ) + ( w 1 a y - x 1 a z + z 1 a x ) sin ( θ / 2 ) z 3 = z 1 cos ( θ / 2 ) + ( w 1 a z + x 1 a y - y 1 a x ) sin ( θ / 2 )

With recent versions of blender that support sinusoidal easing on fcurves it is possible to create an fcurve in an action that accurately represents a sine wave or a cosine wave of a fixed magnitude by creating keyframe points where the value of the sine/cosine equals 0, 1, and -1 and setting the ease in/out for each keyframe point appropriately. Even functions of the form g cos ( θ ) are easy to express by just scaling the y element of the fcurve in the action. Unfortunately the formulas we have derived above combine both sine and cosine terms. In order to express this using blender fcurves, we would have to use NLA strips with blend mode addition.

or would we?

If we remember some of our old high school trigonometry identities we can use

sin ( ϕ + θ ' ) = sin ( ϕ ) cos ( θ ' ) + cos ( ϕ ) sin ( θ ' )

Let's multiply by r and replace θ ' with θ / 2 :

r sin ( ϕ + θ / 2) = r sin ( ϕ ) cos ( θ / 2) + r cos ( ϕ ) sin ( θ / 2)

Now those terms involving r and φ look a lot like polar coordinates, and we can use that technique to transform

b cos ( θ / 2) + c sin ( θ / 2)

into

r sin ( ϕ + θ / 2)

using the following relationships familiar to anyone versed in polar coordinates:

c = r cos ( ϕ ) b = r sin ( ϕ ) r = b 2 + c 2 ϕ = atan2 ( c , b )

To actually replicate the sine wave in the fcurve we need 5 keyframes corresponding to the following expressions:

0= rsin ( 0 ) r= rsin ( π 2 ) 0= rsin ( π ) -r= rsin ( 3 π 2 ) 0= rsin ( 2 π )

Let us use α to represent the values 0, π 2 , π, 3 π 2 , and 2π . We will also designate how long it takes for the object to complete a single rotation by defining p as the period of the rotation as a number of video frames. Next, we designate f to represent a frame number in the following formulas. f= f0 + p2πθ ϕ + θ / 2 = α θ = 2(α - ϕ) f=f0 + p2π 2(α - ϕ) f(α) =f0 + pπ (α - ϕ) Now we have the formulas to compute the keyframe and value for the five control points of the fcurve for each quaternion channel. ( f(0) , 0 ) ( f( π 2 ) , r ) ( f( π ) , 0 ) ( f( 3 π 2 ) , -r ) ( f( 2 π ) , 0 )

We will also want to use a CYCLES modifier on the fcurve so that it repeats infinitely and the object will keep spinning no matter how long the animation runs.

One oddity that you might notice is that the frame span on the fcurve is actually 2p . This is because the quaternions q and -q actually represent the same orientation, and since the sine waves we have crafted make each element vary over both negative and positive values, one cycle of the fcurve actually has one half that can be considered q and the second half can be considered -q .

When we put this into practice with the animate-random-spin.py script we end up with fcurves that look like this:

related links