Here's a python script that gives each object its own quaternion fcurves using sinusoidal easing backed up by some fancy application of high school trig:
import bpy
import random
from math import *
from mathutils import *
def random_in_circle():
while True:
x = random.random()*2-1
y = random.random()*2-1
l2 = x * x + y * y
if (l2<=0):
continue
if (l2 <=1):
return x,y
def random_axis():
z = random.random()*2-1
theta = random.random()*pi*2
r = sqrt(1-z*z)
x = cos(theta)*r
y = sin(theta)*r
return [ x,y,z]
def random_quaternion():
"""
http://mathworld.wolfram.com/HyperspherePointPicking.html
"""
x,y = random_in_circle()
z_,w_ = random_in_circle()
r5 =sqrt( ( 1-x*x - y*y) / (z_*z_+w_*w_))
z = z_ * r5
w = w_ * r5
return [x,y,z,w]
def rig_quaternion_channel(action, channel, period, a, b):
"""
This is heavy-duty voodoo to figure out what keyframes to use with sinusoidal easing
to reconstruct a curve of the form
a*cos(theta) + b*sin(theta)
by converting it to the form
c*sin(theta+phi)
"""
c = sqrt(a * a + b * b)
phi = -atan2(a, b)
fc = action.fcurves.new(data_path="rotation_quaternion", index=channel)
fc.keyframe_points.add(5)
vals = [0, 1, 0, -1, 0]
for j in range(5):
kp = fc.keyframe_points[j]
frame = 1 + ( phi / (2 * pi) + j / 4.0) * 2 * period
kp.co = ( frame, c * vals[j])
kp.interpolation = 'SINE'
if 0 == j % 2:
kp.easing = 'EASE_OUT'
else:
kp.easing = 'EASE_IN'
fc.modifiers.new('CYCLES')
def rig_random_rotation2(obj, scn):
"""
This rigs obj with a random rotation about a random axis using quaternions and fcurves with sinusoidal easing.
I feel pretty smug for having pulled this off - RF
"""
q1 = Quaternion(random_quaternion())
axis = Vector(random_axis())
#print( [ q1, axis ])
# w2 = cos(theta/2)
# x2 = axis.x*sin(theta/2)
# y2 = axis.y*sin(theta/2)
# z2 = axis.z*sin(theta/2)
# w' = w1*w2 - x1*x2 - y1*y2 - z1*z2
# w' = w1*cos(theta/2) - x1*axis.x*sin(theta/2) - y1*axis.y*sin(theta/2)- z1*axis.z*sin(theta/2)
# w' = w1*cos(theta/2) - (x1*axis.2 +y1*axis.y+z1*axis.z)*sin(theta/2)
period=(2*pi/random_rotation_speed_radians()) * scn.render.fps
obj.rotation_mode = "QUATERNION"
obj.animation_data_clear()
obj.animation_data_create()
action = obj.animation_data.action = bpy.data.actions.new("groovy")
"""
Given
* one orientation quaternion q1,
and
* a rotation axis
use the formula for q2(theta) = Quaternion(axis, theta)
and the formula for q3 = q1*q2
figure out how q3 relates to theta, and reduce each w,x,y,z channel to an expression of the form
q3[i] = a_i * cos(theta/2) + b_i * sin(theta/2)
and pass those coefficients to rig_quaternion_channel so it can rig the fcurves correctly
"""
rig_quaternion_channel(action, 0, period, q1.w, -q1.x * axis.x - q1.y * axis.y - q1.z * axis.z)
rig_quaternion_channel(action, 1, period, q1.x, q1.w * axis.x - q1.z * axis.y + q1.y * axis.z)
rig_quaternion_channel(action, 2, period, q1.y, q1.z * axis.x + q1.w * axis.y - q1.x * axis.z)
rig_quaternion_channel(action, 3, period, q1.z, -q1.y * axis.x + q1.x * axis.y + q1.w * axis.z)
def random_rotation_speed_radians():
return random.random() + 1
def mission2(scn):
for obj in scn.objects:
if obj.select:
rig_random_rotation2(obj, scn)
#
#
#
scn = bpy.context.scene
mission2(scn)
This code is now the subject of http://web.purplefrog.com/~thoth/blender/python-cookbook/animate-random-spin.html and the math behind it is explained at http://web.purplefrog.com/~thoth/blender/python-cookbook/narratives/animate-random-spin.html .