3d Mandelbrot fractal in Blender python




I am a huge fan of fractals and was hoping that the famous Mandelbrot set would look even better in three dimensions than in two.

(If you're not familiar with 2d Mandelbrot fractals, please read this short tutorial first.)

Honestly, I was disappointed: the valleys and peaks are too steep (even after applying logarithm to smooth them out) to have much appeal to a human brain. In 2d, the color transitions are smooth, resembling natural shorelines, whereas in 3d we see spikes which seem very artifical.

It still is an interesting exercise in Blender Python, so here are step by step instructions even for the absolute Blender beginners.

If you just want to see the result quickly, you can download the mandel.blend file for Blender 3d and just play with it.

Otherwise here are step-by-step instructions:

1. Open Blender 3d
2. Click on the 'clock' icon in the bottom left corner of the screen and select 'Text editor'



3. This will open the Text editor in the area where the 'Timeline' normally is. Click on the 'New' button to create a new text file:



4. Copy the code below and paste it. (If you're pasting using Ctrl+V, make sure the mouse cursor is in the Text editor area, otherwise it won't work.

5. Click the 'Run script' button:



6. The 3d set will show up in the 3d view. (Delete the default cube.) That's it! Use the Blender navigation to enjoy the views!




import bpy
import math

# mesh arrays
verts = []
faces = []

# mesh variables
numX = 100
numY = 100

# fill verts array
for i in range (0, numX):
    for j in range(0,numY):
        # nomalize range
        x = (i/numX*4)-2
        y = (j/numY*4)-2

        zx = 0
        zy = 0
        
        for it in range (0, 255):
            xt=zx*zy
            zx=zx*zx-zy*zy+x
            zy=2*xt+y
            if zx*zx+zy*zy>4:
                   break
        z = math.log1p(it)
        

        vert = (x,y,z)
        verts.append(vert)

# fill faces array
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)
        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0

# create mesh and object
mesh = bpy.data.meshes.new("mandelbrot")
object = bpy.data.objects.new("mandelbrot",mesh)

# set mesh location
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)

# create mesh from python data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
I plugged the Mandelbrot formula (blue font above) into the mesh creation algorithm from The Proving Ground.

Fractals in Excel

Fractals in JavaScript


Your first program in JavaScript: you need 5 minutes and a notepad