Sierpinski's Gasket - Houdini and Mantra


The purpose of this project was to write a Python script that would generate points in 3D space using the fractal algorithm for Sierpinski's Gasket. The intention was to make the script  renderable in several 3D rendering software packages.


Sierpinski's gasket is created by defining the vertices/points of a tetrahedron, choosing a random point within the tetrahedron, picking a vertex/point of the tetrahedron at random, finding the midpoint between that random point and random vertex and then storing that midpoint in a list. This process is done over and over again in order to create the fractal.

1. Pick a random clocation

2. Define a seed point

3. Collect points (vertices) from a list of 4 to generate the Sierpinski Fractal

4. Find the mid-point of 1 and 2

5. Save the mid-point

The Sierpinski triangle, also called the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.

The Following Code Was Modified For Houdini And Rendered With Mantra:

Python Code:

node = hou.pwd()
geo = node.geometry()

# Add code to modify contents of geo.
# Use drop down menu to select examples.

import random
#import math
#import socket

# Procedure halfstep returns a midpoint between two
# user-defined points
import random, math

def halfstep(p1, p2):
        a = float(p1[0] + p2[0]) / 1.5
        b = float(p1[1] + p2[1]) / 2
        c = float(p1[2] + p2[2]) / 2
        result = [a , b, c]
        return result

def pickpnt(pnts):
        result = random.choice(pnts)
        return result        

triangle = [ [0,0,1], [1,0,-1], [-1,0,-1], [0,1.5,-0.2] ]
seed_pnt = [0,0.5,0]
point_list = []

for n in range(25000):  
        vert = pickpnt(triangle)
        seed_pnt = halfstep(vert, seed_pnt)


Modifications To Midpint

def halfstep(p1, p2):
    a = float(p1[0] + p2[0]) / 2
    b = float(p1[1] + p2[1]) / 1.3
    c = float(p1[2] + p2[2]) / 2
    result = [a , b, c]
    return result

Through Manipulating The Initial Definition You Can Create Variations Of The Design.

def halfstep(p1, p2):
    n = random.randint(1, 10)
    a = float(p1[0] + p2[0]) / n
    b = float(p1[1] + p2[1]) / n
    c = float(p1[2] + p2[2]) / n
    result = [math.sin(a) , math.cos(b), c]
    return result

def pickpnt(pnts):
    result = random.choice(pnts)
    return result