Houdini Dynamics and Effects
Python Driven Rubik Cube


With Houdini, a user has access to a robust library of python programming implementation.

This document summarizes and displays the scripts used to create a Rubik Cube Houdini Asset that is rigged and ready for play. All nodes and functionality are born from script.

In this workflow, two seperate python modules are written.

1. With Houdini's event handling clause, the first module defines a function that is called on creation (the setUp() function: Figure 03). This function creates, rigs, and assigns shaders to the digital asset.

2. The second module defines the function that drives the rotation. The analysis of geometry placement determines translation behavior.


Figure 01: Animated Rubick's Scramble Sequence

Figure 01 exhibits the result of the rubik's python project. Click the caption to jump to NTSC quality render. The sequence was animated in only a few minutes. This scripting workflow with Houdini is what enables such quick and easy animating.

To start, a digital asset node is required. An empty subnet node will suffice. After creating a digital asset from the empty subnet, from the digital asset we have the type properties window. In this window Houdini provides access to parameter and script editing options for the digital asset.

It's in this window, the scripting tab in particular, where the majority of the work and functionality of the rubik's python project is orchestrated.

From here we can create all the components of the asset with Python.

 


Figure 02: HDA Custom Parameter Tab Snapshot

The scripting tab provides event clauses. The onCreated event creates a python module that will run whenever the HDA is dropped to a scene. This event clause is the ideal condition to run a setUp module. This module will create the contents of the digital asset.

Figure 03 shows the final setUp script. This script is a single function that is called. Click the link or thumbnail for a full view of the script.

With the instance assigned to a variable. Python script can create geometry, shaders, UI tables, assign parameters, and essentially anything else that can be done manually.

The benefit of this asset is the ease and freedom in which it can be animated. Figure 01 was keyframed and animated in only a few minutes, this is due to the setup of the HDA custom parameters (as seen in Figure 02), and the ability to animate translation values at the HDA level without breaking rotation() functionality.

 


Figure 03: HDA Python setUp Function in onCreated Module Thumbnail


Comments provided inside the script will assist in explaining the order and functionality of the module.

One note to consider is the three nested for loops that create the twenty-seven boxes that consist of the rubik's cube. Three nested loops are used instead of a single for loop.

A rubik's cube consist of three 3x3 grids stacked atop eachother. The three nested for loops allow for box creation mimicking this fashion. This allows for the iteration variable (i, j, or k) to be used not only for iteration, but also parameter for box translation values.


Figure 04: HDA onCreated Script Result


The second module contains the functions required to perform the rigged translation and rotation operations, and assign keyframe methods to a parameter button at the HDA level.

A series of different functions are called to a master rotation() function. It is this function that is called on call-back scripts in the HDA parameters tab in Houdini.

The rotation() function reads three parameters:

The HDA instance (so paths are relative), the axis of rotation (after all, a rubik's cube can only be rotated on one axis at a time), and the degree of rotation (rotate clockwise or counter clockwise, 90 degrees or any other integer degree).

Click thumbnail or here for full script

Figure 05: HDA Python Module Rotation Function Thumbnail


Note:
The scripts can be improved. In the onCreated() function a new material is made for each geometry node. An improvement would be to loop instead only six materials and assign them to the geometry. Other improvements would be switch node implementation, so that the primitive boxes can be replaced with modeled, higher poly-count polygons.

Figure 06: Scene Lit for Rotation Animated Sequence