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
We will express our random rotation as the composition of one fixed orientation quaternion composed with another quaternion representing the spin which varies over time.
To pick 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 in the range [-1,1] and then calculate the and based on the random .
Once we have the random orientation and the varying rotation we want to compose them . The formula for composing two quaternions is
When you substitute the axis+angle formulation for you end up with
If we regroup the equation around the cos and sin terms we get
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 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.
Let's multiply by r and replace with :
Now those terms involving r and φ look a lot like polar coordinates, and we can use that technique to transform
using the following relationships familiar to anyone versed in polar coordinates:
To actually replicate the sine wave in the fcurve we need 5 keyframes corresponding to the following expressions:
Let us use to represent the values , , , , and . We will also designate how long it takes for the object to complete a single rotation by defining as the period of the rotation as a number of video frames. Next, we designate to represent a frame number in the following formulas. Now we have the formulas to compute the keyframe and value for the five control points of the fcurve for each quaternion channel.
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 . This is because the quaternions and 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 and the second half can be considered .
When we put this into practice with the animate-random-spin.py script we end up with fcurves that look like this: