Skip to content

Instantly share code, notes, and snippets.

@Airyzz
Last active November 9, 2020 01:26
Show Gist options
  • Save Airyzz/b2958cff4cc7f04fc0e336d1c4abf4ae to your computer and use it in GitHub Desktop.
Save Airyzz/b2958cff4cc7f04fc0e336d1c4abf4ae to your computer and use it in GitHub Desktop.
import bpy
import bpy
from mathutils import *
from math import *
scale = 0.33333333
class cube:
pos = Vector((0, 0, 0))
size = 0.0
index = 0
cubes = list()
def subdivide(self):
for x in range(3):
for y in range(3):
for z in range(3):
if not ((y == 1 and x == 1) or (y == 1 and z == 1) or (x == 1 and z == 1)):
newcube = cube()
newcube.size = self.size * scale
newcube.index = self.index + 1
vx = self.pos[0] + x * pow(scale, self.index + 1)
vy = self.pos[1] + y * pow(scale, self.index + 1)
vz = self.pos[2] + z * pow(scale, self.index + 1)
newcube.pos = Vector((vx, vy, vz))
newcube.cubes = list()
self.cubes.append(newcube)
collection = bpy.data.collections.new("aids")
bpy.context.scene.collection.children.link(collection)
def make_cube(size=1, location=Vector((0, 0, 0))):
verts = [(0.5 * size, 0.5 * size, -0.5 * size),
(0.5 * size, -0.5 * size, -0.5 * size),
(-0.5 * size, -0.5 * size, -0.5 * size),
(-0.5 * size, 0.5 * size, -0.5 * size),
(0.5 * size, 0.5 * size, 0.5 * size),
(0.5 * size, -0.5 * size, 0.5 * size),
(-0.5 * size, -0.5 * size, 0.5 * size),
(-0.5 * size, 0.5 * size, 0.5 * size)]
faces = [(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7)]
me = bpy.data.meshes.new("cube")
me.from_pydata(verts,[],faces)
ob = bpy.data.objects.new("cube", me)
ob.location = location
collection.objects.link(ob)
def create_cubes(c):
for cb in c.cubes:
if cb.index < 4:
cb.subdivide()
if len(cb.cubes) > 1:
create_cubes(cb)
else:
make_cube(cb.size, cb.pos)
#bpy.ops.mesh.primitive_cube_add(size=cb.size, enter_editmode=False, align='WORLD', location=cb.pos, scale=(1, 1, 1))
c = cube()
c.size = 1
c.subdivide()
create_cubes(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment