p2or
11/1/2015 - 3:43 PM

blender-random-xy-animation.py

import bpy
import random

def random_floats(min, max, size):
    return [random.uniform(min, max) for _ in range(size)]

# define a animation length
length = 10

# create a list, which contains 10 random x values
x_values = (random_floats(0, 4, length))

# create a list, which contains 10 random y values
y_values = (random_floats(0, 2, length))

# get selected object
obj = bpy.context.active_object

# animation start frame
start_frame = 1

# frame value
frame_number = start_frame

# keyframe offset
offset = 1

# iterate through length and apply them
for i in range(length):
    # set the frame
    bpy.context.scene.frame_set(frame_number)
    # set new location
    obj.location = (x_values[i],y_values[i],0)
    # insert keyframe
    obj.keyframe_insert(data_path="location", index=-1)
    # add offset value
    frame_number += offset