I have gotten a little further, but the operator still only works correctly if you run it twice.
The extrapolation for the fcurve does not update until you run it again even though I can see in the data blocks browser that the code is indeed setting the Extrapolation to Linear.
My guess is the fCurve itself needs an update() fnuction which does not yet exist.
Also I am updating the scene as well and that does not help.
Here is the code for the entire operator.
USAGE: Copy this code into a text window, select the default cube or any object and press ALT-P in the code window.
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy # I'm not sure if this is really needed?
from random import random
"""
Name: 'Randomly Animate Rotation'
Blender: 250
"""
__author__ = ["Atom"]
__version__ = '0.1'
__url__ = ["http://blenderartists.org/forum/showthread.php?t=177757"]
__bpydoc__ = """
Usage:
Select a group of objects and activate the operator.
This script will randomly animate the XYZ of every object in the selection so they continuously animate.
Version history
v0.1 - Initial draft as an operator, no user interface.
"""
def main(context):
for ob in context.scene.objects:
if ob.selected == True:
context.scene.set_frame(1)
bpy.ops.transform.rotate(value=(0.0,), axis=(-0, -0, -1), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.transform.rotate(value=(0.0,), axis=(-0, -1, -0), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.transform.rotate(value=(0.0,), axis=(-1, -0, -0), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.anim.keyframe_insert_menu(type=-2, confirm_success=False, always_prompt=False)
rndFrame = 60
rndXRot = random()
rndYRot = random()
rndZRot = random()
#print ("RND Values = " + str(rndFrame) + ", [" + str(rndXRot) + ", " + str(rndYRot) + ", " + str(rndZRot) + "]")
context.scene.set_frame(int(rndFrame))
bpy.ops.transform.rotate(value=(rndZRot,), axis=(-0, -0, -1), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.transform.rotate(value=(rndYRot,), axis=(-0, -1, -0), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.transform.rotate(value=(rndXRot,), axis=(-1, -0, -0), proportional='DISABLED', proportional_editing_falloff='SMOOTH', proportional_size=1, mirror=False, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0))
bpy.ops.anim.keyframe_insert_menu(type=-2, confirm_success=False, always_prompt=False)
for fcurve in ob.animation_data.action.fcurves:
if fcurve.data_path == "rotation_euler":
fcurve.extrapolation = "LINEAR"
#var = bpy.ops.anim
#print(var)
#print(dir(var))
context.scene.update()
class RandomlyAnimateRotation(bpy.types.Operator):
# Tool tip for operator goes between these tripple single quotes.
'''Randomly animate the XYZ rotation of all objects in the selection.'''
bl_idname = "object.random_animate_rotation"
bl_label = "Randomly animate the XYZ rotation of all objects in the selection."
def poll(self, context):
return context.active_object != None
def execute(self, context):
print ("--> BEGIN: Operator - RandomlyAnimateRotation")
main(context)
print ("<--END: Operator - RandomlyAnimateRotation
")
return {'FINISHED'}
bpy.types.register(RandomlyAnimateRotation)
if __name__ == "__main__":
bpy.ops.object.random_animate_rotation() # Must match bl_idname?