Your browser does not support the video tag.
class Cube(object): def __init__(self, center, side): self.center = center self.sideLength = side def divide(self): newLength = self.sideLength / 5.0 centerX = self.center[0] centerY = self.center[1] centerZ = self.center[2] cubes = [] for x in range(5): newX = centerX + (x - 1) * newLength for y in range(5): newY = centerY + (y - 1) * newLength for z in range(5): newZ = centerZ + (z - 1) * newLength cubes.append(Cube([newX, newY, newZ], newLength)) return cubes
class Menger3D(): def __init__(self, sideLength, depth, toRemove=[4],freq=4.0,amp=0.2): self.sideLength = sideLength self.depth = depth self.freq = freq self.amp = amp self.toRemove = toRemove self.cubes = [] self.deleted = [] self.mengerDivide(Cube([1.0,1.0,1.0], self.sideLength), self.depth) self.getPoints() self.distort() self.getBounds(distorted = True) def mengerDivide(self,cube,depth): if depth == 0: self.cubes.append(cube) return divided = cube.divide() kept = [] for i, cube in enumerate(divided): if i in self.toRemove: self.deleted.append(cube) else: kept.append(cube) for cb in kept: self.mengerDivide(cb, depth - 1) def everyOther(self,index=[1]): return self.cubes[1::2] def getPoints(self, inverse = False): if inverse: cubes = self.deleted else: cubes = self.cubes self.pnts = [] for cube in self.cubes: self.pnts.append(cube.center) return self.pnts def sortBy(self,param=[0]): self.pnts.sort(key = lambda x: x[0]) return self.pnts def distort(self): self.newData = self.sortBy() for pnt in self.newData: pnt[1] -= self.amp*math.cos(self.freq*pnt[0]) return self.newData def getBounds(self, distorted = False): if distorted: pnts = self.newData else: pnts = self.pnts minx = min(pnts, key=lambda x: x[0]) miny = min(pnts, key=lambda x: x[1]) minz = min(pnts, key=lambda x: x[2]) maxx = max(pnts, key=lambda x: x[0]) maxy = max(pnts, key=lambda x: x[1]) maxz = max(pnts, key=lambda x: x[2]) self.bounds = [minx[0], miny[1], minz[2], maxx[0], maxy[1], maxz[2]] return self.bounds
from cube import Menger3D from ass_header import cube_info import time import random import math class MengerAss(Menger3D): def __init__(self, sideLength, depth, path, toRemove=[4]): Menger3D.__init__(self, sideLength, depth,toRemove) self.asspath = path def write(self): now = time.strftime("%Y %m %d %H:%M") num_pnts = len(self.cubes)/2 f = open(self.asspath, 'w') f.write('### exported: %s\n' %(now)) f.write('### user: jkengl20\n') f.write('### bounds: {} {} {} {} {} {} \n'.format(*self.bounds)) f.write('### fps: 24.000000\n### render_layer: defaultRenderLayer\n### frame: 1.000000\n') count = 0 for cube in self.cubes: f.write('polymesh\n{\n name pCubeShape%i \n' % count) f.write(' visibility 255\n sidedness 255\n receive_shadows on\n self_shadows on\n matrix\n') #side lengths f.write(' %f 0 0 0\n 0 %f 0 0\n 0 0 %f 0\n' % (cube.sideLength,cube.sideLength,cube.sideLength)) #positions f.write(' %f %f %f 1\n' % (cube.center[0],cube.center[1],cube.center[2])) f.write(' opaque on\n matte off\n id %i' % (141609554 + count)) f.write('%s' % cube_info) f.write('} \n') count += 1 f.close() if __name__ == '__main__': #toRemove = [4,10,12,13,14,16,22] toRemove = [6,8,12,16,18,31,33,37,41,43,56,58,62,66,68,81,83,87,91,93,106,108,112,116,118,52,26,28,76,78,34,44,64,84,94,46,48,72,96,98,30,40,60,80,90] m1 = MengerAss(1.0,2, 'D:/scad/vsfx/tech312/312/test3d.ass', toRemove) m1.write()