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 https://andreiclinciu.net
# Licensed under GPL 3.0  - Attribution Required


import bpy
from random import randint,random

bpy.ops.group.create(name="CubeXGroup")
 
group = bpy.data.groups['CubeXGroup']

def main():
    start_pos=(0,0,0)
    for i in range(77):
        add_box(i);
   
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 = bpy.context.scene.objects.active
    object_random_location_animation(obj,-10,10)
    object_random_color(obj,i)
    obj.select = True
    bpy.ops.object.group_link(group="CubeXGroup")

#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:
        bpy.context.scene.frame_set(frame_num)
        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 = bpy.data.materials.new(name = "RandomMaterial" + str(i))
    obj.data.materials.append(material)
    bpy.context.object.active_material.diffuse_color = (random(),random(), random())
   # bpy.context.object.active_material.name = "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

main();

 

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


What are your thoughts?

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

You might enjoy these similar articles:

Subscribe to my newsletter!