Domino effect in Blender Python



Here's all the code you need to create the animation above:

0
1
2
3
4
5
6
7
8
9
10
import bpy
bpy.ops.mesh.primitive_plane_add(radius=145location=(000))
bpy.ops.rigidbody.object_add()
bpy.context.object.rigid_body.type = 'PASSIVE'
 
for i in range(025):
    bpy.ops.mesh.primitive_cube_add(radius=1location=(6*i010))
    bpy.ops.rigidbody.object_add()
    bpy.ops.transform.resize(value=(1610), constraint_axis=(FalseTrueTrue))
    if i==0:
        bpy.ops.transform.rotate(value=0.4axis=(010))


And here's how it works:

(If you're completely new to Blender Python, click here to find out how to copy-paste the code.)

Blender's physics engine is very powerful, but at the same time very easy to handle programatically. If you're learning Python, playing with physics is a great way to practice. For the domino effect, all we need is a plane and several blocks.

The plane, which will serve as a table on which the blocks stand [line 1], as well as the blocks are Rigid Bodies [2]. Once a Blender object is set as a Rigid Body, it behaves like, well, a rigid body, which means that other rigid bodies won't go through it, but rather push it away, bounce against it, etc. By default, Blender treats all objects as clouds or ghosts which do not interact when colliding. Turning them into Rigid Bodies is like changing them into stones and it's what gives them physical properties.

The plane is a "passive" [3] Rigid Body, which means gravity won't pull it down. The blocks are active.

A quick overview of the code:
[0] load the Blender Python module
[1-3] create the plane and set its physics
[5-7] create the domino blocks - they are simply flat rectangular prisms
[8] resize the blocks
[9-10] rotate the first block so that it falls down and starts the chain reaction

Try experimenting with different placement of the blocks - how about a sine curve or a circle?

Click here to download the .blend file which includes the code




3d fractal in Blender Python


Fractals in Excel

Your first program in JavaScript: you need 5 minutes and a notepad

Animated fractal in 32 lines of Javascript

Tutorial - interactive, animated sprites in JavaScript