def getPixel(image, x, y, dither=None): rgb = image.getpixel((image.size[0] - 1 - floor(x * image.size[0]), image.size[1] - 1 - floor(y * image.size[1]))) if dither is not None: tweaked = (rgb[0] + uniform(-dither, dither), rgb[1] + uniform(-dither, dither), rgb[2] + uniform(-dither, dither)) return colors.rgbToBlock(tweaked)[0] return colors.rgbToBlock(rgb)[0]
def ditheredBall(x0,y0,z0,r,rgb,done): for x in range(-r,r): for y in range(-r,r): for z in range(-r,r): if (x**2 + y**2 + z**2 <= r**2): if not (x0+x,y0+y,z0+z) in done: mc.setBlock(x0+x,y0+y,z0+z,colors.rgbToBlock(rgb, randomDither=30)) done.add((x0+x,y0+y,z0+z))
def ditheredBall(x0, y0, z0, r, rgb, done): for x in range(-r, r): for y in range(-r, r): for z in range(-r, r): if (x**2 + y**2 + z**2 <= r**2): if not (x0 + x, y0 + y, z0 + z) in done: mc.setBlock(x0 + x, y0 + y, z0 + z, colors.rgbToBlock(rgb, randomDither=30)) done.add((x0 + x, y0 + y, z0 + z))
def __init__(self, filename, myopen=open, swapYZ=False): super(MeshSTLBinary, self).__init__() with myopen(filename, "rb") as f: header = f.read(80) assert not header.startswith(b"solid") assert len(header) == 80 vertexCount = 0 lastAttribute = 0 curMaterial = 0 lastAttribute = None materialCount = 1 self.materials.append("default") numTriangles = struct.unpack("<I", f.read(4))[0] for i in range(numTriangles): assert len(f.read(12)) == 12 # skip normal for i in range(3): v = struct.unpack("<3f", f.read(12)) if swapYZ: v = (v[0], v[2], -v[1]) self.vertices.append(V3(v)) attribute = struct.unpack("<H", f.read(2))[0] if attribute is not lastAttribute: lastAttribute = attribute if attribute & 0x8000: r = int(((attribute >> 10) & 0x1F) / 31. * 255) g = int(((attribute >> 5) & 0x1F) / 31. * 255) b = int((attribute & 0x1F) / 31. * 255) materialName = "stl_{:02X}{:02X}{:02X}".format(r, g, b) else: materialName = "default" if materialName not in self.materials: self.materials.append(materialName) curMaterial = materialCount materialCount += 1 self.defaultMaterialBlockDict[ materialName] = colors.rgbToBlock((r, g, b))[0] else: curMaterial = self.materials.index(materialName) self.faces.append((curMaterial, (vertexCount, vertexCount + 1, vertexCount + 2))) vertexCount += 3 assert self.vertices assert self.faces
def __init__(self, filename, myopen=open, swapYZ=False): super(MeshSTL, self).__init__() with myopen(filename, "rb") as f: header = f.read(80) assert len(header) == 80 vertexCount = 0 lastAttribute = 0 curMaterial = 0 lastAttribute = None materialCount = 1 self.materials.append("default") if header.startswith(b"solid"): vertexDict = {} triangle = None for line in f: line = line.strip() if line.startswith(b'endfacet'): if triangle is not None: self.faces.append((curMaterial, tuple(triangle))) triangle = None elif line.startswith(b'facet'): triangle = [] elif triangle is not None and line.startswith(b'vertex'): v = tuple(float(x) for x in line.split()[1:4]) if swapYZ: v = (v[0], v[2], -v[1]) if v in vertexDict: triangle.append(vertexDict[v]) else: n = len(vertexDict) vertexDict[v] = n triangle.append(n) self.vertices.append(V3(v)) if self.faces: return else: f.seek(5) numTriangles = struct.unpack("<I", f.read(4))[0] for i in range(numTriangles): assert len(f.read(12)) == 12 # skip normal for i in range(3): v = struct.unpack("<3f", f.read(12)) if swapYZ: v = (v[0], v[2], -v[1]) self.vertices.append(V3(v)) attribute = struct.unpack("<H", f.read(2))[0] if attribute is not lastAttribute: lastAttribute = attribute if attribute & 0x8000: r = int(((attribute >> 10) & 0x1F) / 31. * 255) g = int(((attribute >> 5) & 0x1F) / 31. * 255) b = int((attribute & 0x1F) / 31. * 255) materialName = "stl_{:02X}{:02X}{:02X}".format(r, g, b) else: materialName = "default" if materialName not in self.materials: self.materials.append(materialName) curMaterial = materialCount materialCount += 1 self.defaultMaterialBlockDict[ materialName] = colors.rgbToBlock((r, g, b))[0] else: curMaterial = self.materials.index(materialName) self.faces.append((curMaterial, (vertexCount, vertexCount + 1, vertexCount + 2))) vertexCount += 3 assert self.vertices assert self.faces
def getPixel(image, x, y, dither=None): rgb = image.getpixel(( image.size[0]-1-floor( x * image.size[0] ), image.size[1]-1-floor( y * image.size[1] ) )) if dither is not None: tweaked = ( rgb[0] + uniform(-dither,dither), rgb[1] + uniform(-dither,dither), rgb[2] + uniform(-dither,dither) ) return colors.rgbToBlock(tweaked)[0] return colors.rgbToBlock(rgb)[0]
def __init__(self, filename, myopen=open, swapYZ=False): super(MeshSTL,self).__init__() with myopen(filename, "rb") as f: header = f.read(80) assert len(header) == 80 vertexCount = 0 lastAttribute = 0 curMaterial = 0 lastAttribute = None materialCount = 1 self.materials.append("default") if header.startswith(b"solid"): vertexDict = {} triangle = None for line in f: line = line.strip() if line.startswith('endfacet'): if triangle is not None: self.faces.append((curMaterial, tuple(triangle))) triangle = None elif line.startswith('facet'): triangle = [] elif triangle is not None and line.startswith('vertex'): v = tuple(float(x) for x in line.split()[1:4]) if swapYZ: v = (v[0],v[2],-v[1]) if v in vertexDict: triangle.append(vertexDict[v]) else: n = len(vertexDict) vertexDict[v] = n triangle.append(n) self.vertices.append(V3(v)) if self.faces: return else: f.seek(5) numTriangles = struct.unpack("<I", f.read(4))[0] for i in range(numTriangles): assert len(f.read(12))==12 # skip normal for i in range(3): v = struct.unpack("<3f", f.read(12)) if swapYZ: v = (v[0],v[2],-v[1]) self.vertices.append(V3(v)) attribute = struct.unpack("<H", f.read(2))[0] if attribute is not lastAttribute: lastAttribute = attribute if attribute & 0x8000: r = int(( ( attribute >> 10 ) & 0x1F ) / 31. * 255) g = int((( attribute >> 5 ) & 0x1F ) / 31. * 255) b = int( ( attribute & 0x1F ) / 31. * 255) materialName = "stl_{:02X}{:02X}{:02X}".format(r,g,b) else: materialName = "default" if materialName not in self.materials: self.materials.append(materialName) curMaterial = materialCount materialCount += 1 self.defaultMaterialBlockDict[materialName] = colors.rgbToBlock((r,g,b))[0] else: curMaterial = self.materials.index(materialName) self.faces.append((curMaterial,(vertexCount,vertexCount+1,vertexCount+2))) vertexCount += 3 assert self.vertices assert self.faces