Menger Sponge Order 1

Hello
I am new with Blender and Scripting.
I would like (an exercise) to set up a Menger Sponge.
I am able to prepare all the components …
Sponge_2 ![Sponge_3|519x425]

But I am unable to perform the right code to make the difference of TheCube and all the other objects.

I prepared a very simple code as follow

import bpy

TheSize=3
TheWidth=5

bpy.ops.mesh.primitive_cube_add(size=TheSize, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “BigCube”

bpy.ops.mesh.primitive_cube_add(size=TheSize/3, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CenterThirdX”
bpy.ops.transform.resize(value=(TheWidth, 1, 1))

bpy.ops.mesh.primitive_cube_add(size=TheSize/3, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CenterThirdY”
bpy.ops.transform.resize(value=(1, TheWidth, 1))

bpy.ops.mesh.primitive_cube_add(size=TheSize/3, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CenterThirdZ”
bpy.ops.transform.resize(value=(1, 1, TheWidth))

bpy.ops.object.select_all(action=‘DESELECT’)

How to (by script) make the Menger Sponge Order 1

The result, in symbolic code, seems like : BigCube - (CenterThirdX + CenterThirdY - CenterThirdZ)

I tried different methods (even by hand) … The result is not good ! !

Cna you help me !

Best regards

Pascal

I can’t give you an script answer but as you said you only have to do the “addition“ and “subtraction“ of the objects. These are so called boolean operations. In Blender there is a boolean modifier to combine and subtract objects. And I think there is a way to do this directly in the script with BMesh. But it’s more likely to get a full answer if you ask this in the Coding section (and please use the code tag). But be careful, you will most probably have to deal with coplanar faces and these are only handled correctly with the new Boolean System in Blender, which is only available in the newest version.

Edit: this might help you: https://blender.stackexchange.com/questions/129853/boolean-on-two-simple-bmesh

Thanks Patrick

Yes … I highly suspect what you name ‘coplanar faces’ …

I ’ ll check with 2.9 version

Best regards

Pascal

Hello Patrick

Yessssss … Version 2.9 solves the ‘coplanar faces’

… I am happy

Best regards
P.

1 Like