I've recently started looking into how to create fractals. I started with the Sierpinski Carpet which looks like the image below.

I had to try a few approaches before I got it right but now I have a functioning script that will generate a Sierpinski carpet for however many n iterations that you specify (as long as your computer has the juice). I got stuck at the beginning until I did the math and started analysing various features of the result. It was a good lesson in sitting down an thinking instead of being lazy and searching up answers or beating your head against a wall trying random things out.

I have a finished version of the script ready at the bottom of this page but I encourage you to try coding this in Blender yourself. I'll describe the Sierpinski Carpet in a little detail first, so that you have some idea about how to approach coding it. Try to plan out your script first and don't peek at the answer. Very likely, there's more than one way to approach this and none of those approaches are necessarily more correct than another.

The Sierpinski Carpet is a recursive fractal first created by Waclaw Sierpinski. It's basic algorithm of construction is as follows:

- Start with a square
- Subdivide that square into a 3x3 grid
- Remove the central square

Sounds simple right? In practice, I couldn't get the subdivision method to work (cause Blender appears to assign indices to faces inconsistently) so I changed my approach to placing down squares instead of removing them. If I were to write this as an algorithm, the steps would be as follows.

- Start with a square
- Calculate 1/3 scale of the square
- Place 8 squares of 1/3 scale around the initial square
- Repeat for new squares

Here are the generalised co-ordinates for generating the 8 squares:

C1: (x_{i} + 2s_{i}, y_{i})

C2: (x_{i}, y_{i} + 2s_{i})

C3: (x_{i} - 2s_{i}, yi)

C4: (x_{i}, y_{i} - 2s_{i})

C5: (x_{i} + 2s_{i}, y_{i} + 2s_{i})

C6: (x_{i} - 2s_{i}, y_{i} - 2s_{i})

C7: (x_{i} + 2s_{i}, y_{i} - 2s_{i})

C8: (x_{i} - 2s_{i}, y_{i} + 2s_{i})

where, x_{i} and y_{i} are the co-ordinates of the current square and s_{i} is the scale which you can take either from the x or y since they're the same, cause the object is a square.

If any of you can find a way of getting the subdivision method to work, I'd be interested in seeing it. If you really want a single plane with the squares cut out, you could probably still use the code I've attached and just Bool Tool the holes into a plane. Might need to extrude the planes out a bit first, and you could probably write up a script to make the holes instead of individually selecting objects and applying a Difference operation to them manually.

But, there you go, a fun little coding challenge and I've done the math for you so you just have to code it up. Note that the more iterations you specify, the more work Blender has to do so start off small in case your computer can't handle so much at once.

If you run into any issues or if you've made a mad little render with this, send me an email at the following address: steambeanblog@gmail.com