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

Animated fractal in JavaScript


Check out these programming tutorials:

Python in Blender 3d:

Domino effect (10 lines)


Wrecking ball effect (14 lines)

3d fractal in Blender Python

JavaScript:

Animated particle constellations (42 lines)

Optical illusion (18 lines)

Spinning squares - visual effect (25 lines)

Oldschool fire effect (20 lines)

Fireworks (60 lines)

Minesweeper game (80 lines)

Physics engine for beginners

Physics engine - interactive sandbox

Physics engine - silly contraption

Starfield (21 lines)

Yin Yang with a twist (4 circles and 20 lines)

Tile map editor (70 lines)

Sine scroller (30 lines)

Turtle graphics

Interactive animated sprites

Image transition effect (16 lines)

Wholla lotta quadratic curves (50 lines)

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