Sierpinski Fractal


This web page displays the results of the use of writing python scripts that create a Sierpinski fractal.





This project consists of two parts: (1) Writing two procedures into a python module named Sierpinski. (2) Generating a cloud of 3D points of a fractal.




The first procedure is named "halfstep". The procedure will return the midpoint between the two input points. A second procedure "pickpnt" randomly returns a point from a list of points.



# Sierpinski 1

import random
import socket

# Returns a point midway between point "p1" and point "p2"

def halfstep(p1, p2):
    return ( (p2[0] + p1[0]) * 0.5,
             (p2[1] + p1[1]) * 0.5,
             (p2[2] + p1[2]) * 0.5 )

# Randomly returns a point from a list of points
def pickpnt(pnts):
    return random.choice(pnts)

# Test the procedure using four 3D points.
pnts = [ [0,0,1], [1,0,-1], [-1,0,-1], [0,2,0]]
seed = [0,0,0]



Next is to take in the cloud of points from above into a mel script. So it could then be brought into Maya for rendering. Below is the result 35,000 cubes.


# Open a .mel script for writing

f= open('/home/micgao20/mount/stuhome/tech312/mel/sierpinski.mel', 'w')
size = 0.015
for n in range (35000):
     pickpnt(pnts)
     seed = halfstep(pickpnt(pnts),seed)
     f.write( 'polyCube -w %1.3f -h %1.3f -d %1.3f\n' % (size, size, size) )
     f.write( 'move %1.3f %1.3f %1.3f;\n' % ( seed[0], seed[1], seed[2] ) )
f.close()




Particles

As part of one alternation, I played with placing particles in the part of the written .mel script. By using the particle function: "particle" and then followed by a string of '-p x y z' , I'm telling through mel to write particle points. In the following code, it is creating 50,000 particle points.


# Open a .mel script for writing

f= open('/home/micgao20/mount/stuhome/tech312/mel/sierpinski_particles.mel', 'w')
size = 0.015
f.write( 'particle \n')
for n in range (50000):
    pickpnt(pnts)
    seed = halfstep(pickpnt(pnts),seed)
    f.write( '-p %1.3f %1.3f %1.3f\n' % (seed[0], seed[1], seed[2]) )
    f.write( 'move %1.3f %1.3f %1.3f;\n' % ( seed[0], seed[1], seed[2] ) )
f.write(';')
f.close()


Particles had been placed within my Maya scene. And then with a polygon cube, I instanced with the particles to achieve the shape of the sierpinski gasket. Field dynamics were then applied to the particle system.




Alternative Tests



Above are renders of the sierpinski gasket with the use of RIB archives. The RIBs method of rendering is much faster in comparison to the renders produced with the basic Sierpinski 1 codes. For a fully matted shader attached to the sierpinski gasket, it took around 10 seconds--as compared to 2 minutes. Below is the result of the modified codes from some scripts demonstrated during class.



def cloudArchive(num, path):
    pnts = [ [0,0,1], [1,0,-1], [-1,0,-1], [0,2,0]]
    seed = [0,0.5,0]

    for n in range (num):
        pickpnt(pnts)
        seed = halfstep(pickpnt(pnts),seed)
        sys.stdout.write( 'TransformBegin\n' )
        sys.stdout.write( 'Translate %1.3f %1.3f %1.3f\n' % (seed[0], seed[1], seed[2]))
        sys.stdout.write('ReadArchive "%s"\n' % (path) )
        sys.stdout.write( 'TransformEnd\n' )

# This is automatically used by PRMan

args = sys.stdin.readline()
random.seed(1)
while args:
    arg = args.split()
    pixels = float(arg[0])
    path = arg[1]
    num = int(arg[2])

    cloudArchive(num, path)

    sys.stdout.write('\377')
    sys.stdout.flush()
args = sys.stdin.readline()




Above is an example of a couple renders done with eight intial points.





At 10 points, the Sierpinski shape breaks.


Conclusion


In a nutshell, I found this assignment a really good exercise because I was able to make something looking pretty cool with python codes! As a first timer with the programming language, I learned the difference of python and MEL--their strengths and weaknesses of both. As well as combining both python and MEL to create a working script.