Blender Python Random Cube Animation Tutorial - Wallpaper - Video Generation

Blender Python Random Cube Animation Tutorial - Wallpaper - Video Generation

Show table of contents

Blender is awesome. The things you can create with it are endless. If would had discovered Blender during or before college I might have switched careers entirely.

I've created a Video Tutorial which shows how Python can be used to generate random animations. This is usefull when generating background video's or wallpapers.

The script is pretty straightforward:

#Random Cube Creation and Animation
# (c) Andrei Clinciu 2018
# Licensed under GPL 3.0  - Attribution Required

import bpy
from random import randint,random"CubeXGroup")
group =['CubeXGroup']

def main():
    for i in range(77):
def add_box(i):
    #generate our object
    bpy.ops.mesh.primitive_cube_add(location = [ randint(-10,10) for axis in 'xyz'])
    #Select the active object (last added is always the active one
    obj =
    object_random_color(obj,i) = True

#End AddBox

def object_random_location_animation(obj,min,max):
    positions =  []
    for i in range(0,9):
        positions.append([ randint(min,max) for axis in 'xyz'])
    frame_num = 0
    for position in positions:
        obj.location = position
        obj.keyframe_insert(data_path="location",index = -1)
        frame_num +=30
#End object_random_location_animation

def  object_random_color(obj,i):
    material = = "RandomMaterial" + str(i))
    bpy.context.object.active_material.diffuse_color = (random(),random(), random())
   # = "Object Material"
    #inputs[0].default_value = (random(),random(), random(), 1)
    #NOTE: Colours may be very interesting or not so great
    #You can select colours from a list of predefined colours
#End  object_random_color



First we generate a random number of cubes by calling the add_box function on each number. A random location is chosen for the x,y,z axis [ randint(-10,10) for axis in 'xyz']).

Then a each cube/object gets animated in  object_random_location_animation. What happens is we space each action a X number of keyframes appart(30 in our example) then it gets moved to a different location.

After this is done then each cube/object gets a random material and color.

This is just the starting point of what you can do. You can create different types of objects and give them random speeds and materials. Maybe even adding physics to them and bounce them together.

Download and enjoy the .blend and .py file to animate your own video's with random cubes.

Subscribe to my newsletter

NOTE:You will need to confirm your e-mail address in order to fully complete the subscription process.

What are your thoughts?

All comments are moderated and must adhere to the terms of service.

You might enjoy these similar articles: