• Bubble
  • Bubble
  • Line
Blender Python Random Cube Animation Tutorial - Wallpaper - Video Generation
Andrei Clinciu Article AUthor
Andrei Clinciu
  • 2018-12-09T14:28:00Z
  • 2 min to read

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.

UPDATE: Use the 2.79 version for Blender 2.79 or earlier, use the 2.8 version for the new version of blender!

 

The script is pretty straightforward.

Blender 2.80 and future releases:

#Random Cube Creation and Animation# (c) Andrei Clinciu 2018 https://andreiclinciu.net# Licensed under GPL 3.0  - Attribution Requiredimport bpyfrom random import randint,randombpy.ops.collection.create(name="CubeXGroup") #group = bpy.data.collection['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.active_object    object_random_location_animation(obj,-10,10)    object_random_color(obj,i)    obj.select_set(state = True)    bpy.data.collections["CubeXGroup"].objects.link(obj)#End AddBoxdef 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_animationdef  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(),1)   # 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_colormain(); 

Blender 2.79 and earlier

#Random Cube Creation and Animation# (c) Andrei Clinciu 2018 https://andreiclinciu.net# Licensed under GPL 3.0  - Attribution Requiredimport bpyfrom random import randint,randombpy.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 AddBoxdef 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_animationdef  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_colormain();

 

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.

Ideas and comments

Andrei Clinciu
Andrei Clinciu

I'm a Full Stack Software Developer specializing in creating websites and applications which aid businesses to automate. Software which can help you simplify your life. Let's work together!

Building Great Software
The digital Revolution begins when you learn to automate with personalized software.

Find me on social media