__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.