30 January 2012 Previous

Umdaniso weblocks - 'Dance of the Blocks'

Conways game of life

Cubes dancing across an African night sky.  This light and fun exploration into how to roll cubes in Blender 2.5 travels across veld planes, Baobab trees and into the timeless African night sky.

After an initial endeavour into how to roll a cube in Blender 2.5, it was realised that a number of techniques could be used.  This short piece makes use of three:  Drivers; An armature rig; A python script.  The results were wrapped in a short play of animation, set in Africa.

The ability to roll cubes on the ground in 3D seemed elusive, yet the math would surely be graspable to someone with an elementary training in maths and python scripting.  These are the results of my endeavour, one is straight forward, one is more complex -both of which have been documented as a tutorial into rigging and scripting in Blender 2.5- and the last is the simplest and most fun to integrate, which was only thought of and applied last.

On the side, I would love to know how Quentin Bolsee, who was just 16 at the time of writting his game ColorCube, rolled his cubes.  His insight would possibly be of greater value than mine.

The animation piece involves the use of the following in Blender 2.5:

  1. Using drivers to roll and bounce cubes.
  2. Using python scripting to roll a cube on one and two planes
  3. A python adoption of Conway's Game of Life in Blender

The process was documented for educational purposes, and can be found below.  It consists of a video tutorial series split into three chapters and a written explanation of the techniques that are not covered in the video tutorials.  @ The more advanced scripters and programmers: any advice or corrections you may have to offer with regards to my work here is greatly appreciated.  Blender's API had been developing rapidly, and there may be better solutions I have not been aware of.

The video tutorial series covers the following:

  • Creating a rig with armatures to roll a cube, which makes use of an Action constraint and control bone for animation.
  • Deconstructing the rig and writting a python script to animate the cube.  This encompasses rolling a cube in two planes (forward, backward, upward and downward)
  • These are split into 3 Chapters: Chapter 1 involves the rig, Chapter 2 and 3 involves the scripting.

The technique of using drivers and the adoption of Conway's Game of Life is illustrated below in this article.

Chapter 01

Download the blend file cube_rig.blend from blendswap and test it out

Cube Rig

  1. 01 - Rig With Empties
  2. 02 - Rig With Armature
  3. 03 - Action Constraint & linking

Chapter 02

Download the blend file cube_script.blend and test it out

cube_script

  1. 01 - Backwards Forwards
  2. 02 - Check Orientation
  3.  03 - Switch Orientation & set key
  4. 04 - Roll Cube
  5. 05 - Upwards Downwards - 01
  6. 05 - Upwards Downwards - 02

Chapter 03

Download the blend file cube_script.blend and test it out

python

  1. 01 - Determine Quadrant
  2. 02 - Check Orientation
  3. 03 - Switch Orientation - 01
  4. 03 - Switch Orientation - 02
  5. 04 - Roll Cube
  6. 05 - Testing - 01
  7. 05 - Testing - 02
The API had been changing frequently but has since stabilised.  All you need to now do to get the script to run is:
  • include "import bpy" at the top of the script
  • Do NOT include the poll functions
  • Do NOT include the Registration of operators at the bottom
The scripts were working as of Blender 2.55 revision 33141

Using Drivers

drivers_advancedThe driver technique makes use of a sin equation to approximate the cube's roll.  This works and is believable, but if you look closely you'll see the corner slip somewhat, a result of the sin graph not perfectly representing a circular arc.  An equation representing a circle works perfectly, and while Bassam figured this out and showed me how its done, I haven't been able to do it yet and won't be referring to it.  Generally this technique requires one controller bone which is keyed.

This is how it works:
The X Location of a bone drives the roll movement of the cube.  The movement of the cube is driven in three parts:  Z Location (Up and down), X Location (Forward and backward) and Y Rotation (for rolling).  While the cube rotates it will move both in a horizontal direction and in a vertical direction to mimic pivoting from its corners.

Z Location (Up and down):
A sin function is used to mimic the cyclic up and down movement.  The absolute value of this is used for the bouncing effect.  The peak of the sin wave is it's highest point, which is the length from the center of the cube to it's corner.  The cubes length is 2units, therefore the center of the cube is 1unit in X and 1unit in Y from any corner.  The distance from center to corner is the hypotenuse of a right angle triangle, which as per pythagorus will be sqrt(1^2 + 1^2) == sqrt(2).  The center of the cube will start at distance 1 and move up to a height of sqrt(2), therefore the displacement over this period will be sqrt(2) - 1.

X Location (Left and right):
The X Location of the cube is equal to the X Location of the control bone.

Y Rotation (roll):
The cube rolls 90 degrees (pi/2) each time it travels a distance equal to its length.  It's length is 2 units, therefore it moves distance*(pi/2)/2

Conway's Game of Life

Download the blend file conways_game_of_life.blend.  Run the script and press the 'Draw Board' button which appears in the 3D view's UI.

Conways game of lifeBeing on the topic of cubes, my friend Greg -the sound guy roped into all the animation I do- introduced me to 'Conways game of Life' - an insanely cool and simple algorithm for generating 'life-forms'.  We adopted the algorithm in Python for Blender 2.5 from G-Do's post here and had fun generating two patterns.  The first is simple and loops, the other is a more complex interaction on a larger grid.  Have a look at some of the life forms which have been discovered - they're beautiful!  In a nutshell, the python script would generate a specific sized grid of cubes and link them to a default material, the pattern of 'live' cubes would also be declared in the script.  A button is presented in the UI which steps forward one frame and updates the grid.  Depending on how old a cube is it'll get assigned a specific material colour, when it's dead it becomes black.  The rendering of the animation was done from the command line which invoked this python script in a loop, with a little flag to render and save the frame images.


Next
blog comments powered by Disqus