cloning
link-mesh-array.py
link-mesh.py

mesh fabrication
staircase.py
triangle-donut.py
vertexAccumulator.py
randomSquareArray.py
meshFromBathymetry.py
cylinders-from-list-of-radii.py
binary-image-to-mesh.py
sphere-minecraft-schematic.py
spikify.py
add-to-mesh.py
mobius-strip.py
split-copy-mesh.py

fabricating other objects
create-text.py
text-from-file.py
create-camera.py
create-bezier.py
helix-bezier.py

material slots
cube-copy-blue.py
cube-turns-red.py
red-blue-per-object.py

animation and fcurves
csv-to-fcurve-loc-rot.py
csv-to-fcurve.py
pop-in-material.py
spike-wiggle-2.py
spike-wiggle.py
sweep-animate-size.py
animate-cycles-lamp-strength.py

incorporating python libraries
exec-text-library.py
exec-external-python.py
import-python.py

constraints
camera-track-object.py
text-track-camera.py

shape keys
explore-shape-keys.py
shape-key-fin.py
docking-tube.py

animating curve bevel
data-graph.py

drivers
scan-drivers.py
copy-drivers.py
driver-fin.py
driver-multi-chain.py

UV layers
barber-pole.py
expand-uv-to-fit.py
uv-from-geometry-cubic.py
flip-texture-v-coordinate.py

modifiers
hook-modifier-curve.py
rounded-prisms.py
make-tile.py
remove-action-modifiers.py

NLAs
explore-NLAs.py
spinning-frogs.py

video sequence editor (VSE)
create-vse-image-strips.py
slide-show.py
vse-strip-gap.py

images and textures
image-on-mesh.py
image-to-material-node.py
load-image-texture.py
texture-one-cube-face.py
condense-duplicate-images.py

analytic geometry
animate-random-spin.py
camera-cone-exp-2.py
camera-cone-exp.py
compute-circle-center.py
dihedral-angle-from-xy.py
extrude-edge-along-custom-axis.py
orientation-matrix.py
two-spheres.py
bezier-interpolate.py
rotate-to-match.py

node trees
change-diffuse-to-emission-node.py

etc
add-plane-from-selected-vertices.py
adjust-all-materials.py
all-nodes-cycles-materials.py
bit_shift.py
bone-orientation-demo.py
cannonball-packing.py
comb.py
convert-quaternion-keyframes-to-euler.py
copy-location-from-vertex-group.py
create-cycles-material.py
demonstrate-decomposition-instability.py
dump-point-cache.py
dump-screen-layout-info.py
expand-nla-strips.py
explore-edge-bevel-weight.py
find-action-users.py
find-green-rectangle.py
find-new-objects.py
fix-scene-layers.py
generate-makefile.py
link-external-data-blocks.py
list-referenced-files.py
material-readout.py
movie-card-stack.py
movies-on-faces.py
next-file-name.py
object-font-from-regular-font.py
operator-mesh-gridify.py
particle-animator.py
particle_loop.py
pose-match.py
pose-sequence-to-fbx.py
prepare-texture-bake.py
raining-physics.py
random-pebble-material.py
reverse-keyframes.py
scale-parallelogram.py
screenshot-sequence.py
select-objects-in-modifiers.py
select-vertices.py
shift-layers.py
snapshot-keyframes-as-mesh.py
sphere-project-texture.py
squish-mesh-axis.py
subdivide-fcurve.py
thicken-texture.py
transform-selected.py
voronoi-madness.py

Supplemental narrative

__author__ = 'thoth'

"""
Give the selected objects a random spin.
http://blender.stackexchange.com/questions/33962/how-do-i-create-a-driver-with-a-random-value-and-apply-that-driver-to-multiple-o
This does not exactly answer the question as asked, but accomplishes a similar mission
"""

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_random_rotation_lame(obj, scn):
    # make the object spin on its Z axis
    obj.rotation_euler = (0,0,0)
    obj.keyframe_insert(data_path='rotation_euler', frame=1)
    obj.rotation_euler = (0,0,random_rotation_speed_radians())
    obj.keyframe_insert(data_path='rotation_euler', frame=1+scn.render.fps)
    # rig the keyframes for linear interpolation and extrapolation so it keeps spinning for the duration of the animation
    for fc in obj.animation_data.action.fcurves:
        if fc.data_path == "rotation_euler":
            fc.extrapolation = 'LINEAR'
            for kp in fc.keyframe_points:
                kp.interpolation='LINEAR'

    # now we make the axis random by parenting the object to an Empty with a thoroughly random orientation
    parent = obj.parent
    if parent is None:
        parent = bpy.data.objects.new("rotation axis of %s"%obj.name, None)
        scn.objects.link(parent)
        obj.parent = parent
        parent.layers = [i==19 for i in range(len(parent.layers))]
    parent.rotation_mode = 'QUATERNION'
    parent.rotation_quaternion = random_quaternion()

    # there is probably a way to avoid the empty and
    # give the object a looping quaternion rotation on a random axis using sinusoidal easing,
    # but I'm a little too lazy to work out the math right this instant.


def frame_by_frame(axis, period, q1, scn):
    # this is a debugging routine I used to validate my sinusoidal fcurve construction
    obj1 = bpy.data.objects.get("overkill")
    if obj1 is None:
        obj1 = bpy.data.objects.new("overkill", None)
        scn.objects.link(obj1)
    obj1.rotation_mode = "QUATERNION"
    for fr in range(1, 2 + 2*period):
        theta = (fr - 1) * 2 * pi / period
        q2 = Quaternion(axis, theta)
#        print(q2)
        obj1.matrix_local = (q1.to_matrix() * q2.to_matrix()).to_4x4()
        obj1.keyframe_insert('rotation_quaternion', frame=fr)


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 mission1(scn):
    for obj in scn.objects:
        if obj.select:
            rig_random_rotation_lame(obj, scn)

def mission2(scn):
    for obj in scn.objects:
        if obj.select:
            rig_random_rotation2(obj, scn)

#
#
#

scn = bpy.context.scene
mission2(scn)

Blender python API quick-start

Syntax highlighting by Pygments.