Adding frames without keying sets

I would like to create an animation of random poses as I have mentioned in another post. I do not want to use any form of interpolation.

How do I write a script to do build this sequence for me in python?

So as an example I have the cube and a defined animation length in my timeline as 20 frames.

import bpy
import random

cube =["Cube"]

i = 1
while i < 21:
    cube.rotation_euler = (0, random.random(), random.random())
    # What goes here?
i = i + 1

How do I set the cube rotations for each frame?
I thought it should be something like bpy.ops.anim.keyframe_add but I dont think it is because then it requires a keying_set which is only relevant for interpolated data.

Similarly you can also use the driver system

import bpy
from  random import random

def random_rot():
    return random()["rand_rot"] = random_rot

Now you can go straight to the rotation properties boxes and type in


wherever you want random rotation. You could restrict it to a frame range too

import bpy
from  random import random

def random_rot(frame,frame_range=(-9999999999,99999999),default_val=0.0):
    if frame in frame_range:
         return random()
         return default_val["rand_rot"] = random_rot

then if you want random rotation for 20 frames

ran_rot(frame,frame_range = (1,20))

You will always need to pass frame in this case. frame is already defined to the driver system as the current_frame.

Cool stuff i never had any idea about :spin: thanks :smiley:

Thanks @Brenel and @batFINGER, both your posts were extremely useful!

For anyone who uses this method and you want to remove the handler one way is to just type

in the Python console which will remove the last handler you added.