Sunday, January 17, 2016

Fractals

I had a revelation: Blender has a full-fledged scripting language based on Python!
This takes out much of the frustration with modeling on a laptop :-)

For instance the 3D version of the Sierpinski triangle below can be modeled in just a few lines of code. Note that I added 10% padding in the size of the small tetrahedra, otherwise they would not touch sufficiently to be connected to one another in Cura, which would result in yet another blob of plastic spaghettis if I tried to print it.



More ambitious (4096 tetrahedra):




So what does it look like when you print it out?
Below is a fairly simple Menger sponge. 



So far so good. This print took about 2 hours, it came out relatively clean despite the large number of overhangs. 


However, you can see the result of my attempt at printing the next level of the Menger sponge, which failed miserably after 4 hours. On one of the last few layers the filament was too damaged from continuously moving back and forth because of the multitude of disconnected squares and corners it had to print. So as soon as a tiny obstruction built up in the head, it just stopped printing and I had to resort to the Atomic method again (see here for the curious).


The code to produce the Menger sponge looks like this:
import bpy
import math
cubeobject = bpy.ops.mesh.primitive_cube_add

def recursecube(level, sx, sy, sz):
    global counter
    if level == 0:
        cubeobject(radius=0.5, location=(sx, sy, sz))
        return  
    nl = level - 1 
    for x in range(0,3):
        for y in range(0,3):
            for z in range(0,3):
                if not((x == 1 and y == 1) or 
                       (x == 1 and z == 1) or 
                       (y == 1 and z == 1)):
                    offset = math.pow(3, nl)
                    nx = sx + offset * x
                    ny = sy + offset * y
                    nz = sz + offset * z
                    recursecube(nl, nx, ny, nz)  

recursecube(4, 0, 0, 0)  



To be continued...

No comments:

Post a Comment