def export_voxel(path, image): print("image shape:", image.shape) image = wfc.get_image() image = np.squeeze(image, axis=3) image = image.astype(int) vox = Vox.from_dense(image) VoxWriter(path, vox).write()
def write_vox(filename, voxel, voxel_order = 'dhw', thereshold = 0.5): #transpose to match orders # if(voxel_order == 'dhw'): # _voxel_data = voxel.transpose([2,1,0]) # _voxel_data = _voxel_data[::-1,...] _voxel_data = voxel >= thereshold _voxel_data = _voxel_data[:,::-1,:] vox = Vox.from_dense(_voxel_data) VoxWriter(filename, vox).write()
def getVoxelsFromSTL(path_in, path_out): """ Load stl and scale before transform to voxels. Use new np array for cleanup. Save .vox at path_out. """ mesh = trimesh.load(path_in) mesh.apply_transform(trimesh.transformations.scale_matrix(0.1)) voxels_save = trimesh.voxel.VoxelMesh(mesh, .7) volume = voxels_save.matrix print("To export: ", os.path.basename(path_in), volume.shape) # del mesh # del voxels_save res = np.zeros_like(volume, dtype=np.int) res[np.nonzero(volume)] = 1 vox = Vox.from_dense(res) name = os.path.basename(path_in) VoxWriter(path_out + '%s.vox' % (os.path.splitext(name)[0]), vox).write()
# if step % (np.prod(world.shape) / 4) ==0: # draw_world(world, tiles, mask = decided) # time.sleep(.3) # state_report() print "generation complete." generate_world() print world worldchars = np.zeros([WORLD_WIDTH * TILE_WIDTH] * 3).astype(np.int32) stride = TILE_WIDTH - 1 for i in range(0, WORLD_WIDTH): for j in range(0, WORLD_WIDTH): for l in range(0, WORLD_WIDTH): if not tile_properties[world[i, WORLD_WIDTH - j - 1, l]].is_air: worldchars[i*stride:i*stride+TILE_WIDTH,j*stride:j*stride+TILE_WIDTH,l*stride:l*stride+TILE_WIDTH] \ = tiles[world[i,WORLD_WIDTH-j-1,l]][:,::-1,:] else: worldchars[i * stride:i * stride + TILE_WIDTH, j * stride:j * stride + TILE_WIDTH, l * stride:l * stride + TILE_WIDTH] = 0 print "writing .vox output" a = (worldchars).astype(np.int32) print a vox = Vox.from_dense(a) VoxWriter('test.vox', vox).write()
borde(posx, elMax + h, posy, wx, wy, colek) ventanas(posx, elMin, posy, wx, elMax + h, wy, colek) # Colores pal = [] for i in range(255): pal.append(Color(75, 75, 75, 0)) ## Colores especiales pal[0] = Color(210, 210, 157, 0) pal[1] = Color(200, 200, 147, 0) pal[2] = Color(137, 172, 120, 0) pal[3] = Color(155, 255, 0, 0) pal[4] = Color(195, 195, 185, 0) pal[5] = Color(245, 255, 169, 0) # Colores de las casas for i in range(10, 50): col = colorsys.hsv_to_rgb( random.random(), random.randint(saturacion[0], saturacion[1]) / 100, random.randint(valor[0], valor[1]) / 100) col = (int(col[0] * 255), int(col[1] * 255), int(col[2] * 255)) pal[i] = Color(col[0], col[1], col[2], 0) invertir() vox = Vox.from_dense(M) vox.palette = pal VoxWriter('ejemploc.vox', vox).write()
if i >= len(tiles): break if x % 2 == 0 and y % 2 == 0 and z % 2 == 0: print x, y, z world[y, x, z] = i i += 1 assert i == len(tiles) print "done placing" worldchars = np.zeros([world_width * TILE_WIDTH] * 3).astype(np.int32) stride = TILE_WIDTH for i in range(0, world_width - 0): for j in range(0, world_width - 0): for l in range(0, world_width - 0): if not tile_properties[world[i, j, l]].is_air: worldchars[i*stride:i*stride+TILE_WIDTH,j*stride:j*stride+TILE_WIDTH,l*stride:l*stride+TILE_WIDTH] \ = tiles[world[i,j,l]] else: worldchars[i * stride:i * stride + TILE_WIDTH, j * stride:j * stride + TILE_WIDTH, l * stride:l * stride + TILE_WIDTH] = 0 # print "air" print "writing .vox output" from pyvox.models import Vox from pyvox.writer import VoxWriter a = (worldchars).astype(np.int32) vox = Vox.from_dense(a) VoxWriter('tileset.vox', vox).write()
import numpy as np from pyvox.models import Vox from pyvox.writer import VoxWriter size = 100 f = open("out.dat", "r") contents = f.read() pixels = np.fromstring(contents, dtype=np.uint8, sep=' ') matrix = pixels.reshape((size, size, size)) vox = Vox.from_dense(matrix) VoxWriter('test_inverted.vox', vox).write()
import logging import coloredlogs from pyvox.parser import VoxParser from pyvox.writer import VoxWriter log = logging.getLogger(__name__) coloredlogs.install(level='DEBUG') parser = argparse.ArgumentParser( description='Testing roundtrip for vox saving and parsing') parser.add_argument("voxfilename", help="VOX model filename") args = parser.parse_args() log.info("Reading vox file: " + args.voxfilename) m1 = VoxParser(args.voxfilename).parse() log.info("File read; writing to test.vox") VoxWriter('test.vox', m1).write() log.info("Reading vox file: test.vox") m2 = VoxParser('test.vox').parse() if (m1.models == m2.models): log.info("Round trip: SUCCESS") else: log.error("Round trip: FAILURE, models do not match")
def voxelize(obj, file_path, vox_detail=32, use_default_palette=False): global image_tuples image_tuples = {} last_time = time.time() print('Converting to vox') source = obj source_name = obj.name bpy.ops.object.select_all(action='DESELECT') source.select_set(True) bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={ "linked": False, "mode": "TRANSLATION" }) bpy.context.object.name = source_name + '_voxelized' bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) bpy.ops.object.convert(target='MESH') target_name = bpy.context.object.name target = bpy.data.objects[target_name] TriangulateMesh(target) # voxelize vox_size = max(target.dimensions) / vox_detail half_size = vox_size * 0.5 bbox_min, bbox_max = find_bounds(target) a = np.zeros((vox_detail, vox_detail, vox_detail), dtype=int) dg = bpy.context.evaluated_depsgraph_get() orig_scene = bpy.context.scene.evaluated_get(dg) if not use_default_palette: palette = [] else: palette = get_default_palette()[1:256] print('Default palette length', len(palette)) for x1 in range(0, vox_detail): print(str(int(x1 / vox_detail * 100)) + '%...') x = bbox_min[0] + x1 * vox_size + half_size if x > bbox_max[0] + vox_size: break for y1 in range(0, vox_detail): y = bbox_min[1] + y1 * vox_size + half_size if y > bbox_max[1] + vox_size: break for z1 in range(0, vox_detail): z = bbox_min[2] + z1 * vox_size + half_size if z > bbox_max[2] + vox_size: break inside, inside_location, inside_normal, inside_face = get_closest_point( Vector((x, y, z)), target, max_dist=half_size * 1.5) if inside: inside = (inside_location[0], inside_location[1], inside_location[2]) vox_min = (x - half_size, y - half_size, z - half_size) vox_max = (x + half_size, y + half_size, z + half_size) if inside > vox_min and inside < vox_max: location = (inside_location[0] + inside_normal[0] * 0.001, inside_location[1] + inside_normal[1] * 0.001, inside_location[2] + inside_normal[2] * 0.001) normal = (-inside_normal[0], -inside_normal[1], -inside_normal[2]) color = get_color_from_geometry( target, location, normal, orig_scene=orig_scene, location=inside_location, polygon_index=inside_face) if color: if len(color) == 4 and color[3] < 0.1: continue color = Color(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255), 255) threshold = max(7, min(12, len(palette) * 0.65)) palette, color_index = try_add_color_to_palette( color, palette, color_threshold=threshold) #color_index = nearest_color_index(color, palette[1:]) a[y1, (vox_detail - 1) - z1, x1] = color_index + 1 vox = Vox.from_dense(a) print('Palette length', len(palette)) vox.palette = palette VoxWriter(file_path, vox).write() print('100%... Exported to', file_path) # delete temporary target bpy.ops.object.select_all(action='DESELECT') target.select_set(True) bpy.ops.object.delete() bpy.ops.object.select_all(action='DESELECT') source.select_set(True) bpy.context.view_layer.objects.active = source print('Took', int(time.time() - last_time), 'seconds')
v += (Dv * Lv + uvv - (F + k) * v) t = 0.3 for i in tqdm(range(n)): res[:, n - i - 1][v > t] = 256 * u[v > t] update() mask = res == 0 res += np.arange(n, dtype='B')[:, np.newaxis] // 8 res[mask] = 0 pal = [Color(0, 0, 0, 0)] + [ Color(*[int(255 * x) for x in inferno(i / 128)]) for i in range(255) ] # res[res<0.1] = 0 # res = (res*256).astype('B') res = res[:, :-3, ...] nz = len(res.nonzero()[0]) print(nz, 'non-zero') if nz: vox = Vox.from_dense(res) vox.palette = pal fn = 'test-%s.vox' % datetime.now().isoformat().replace(':', '_') print('wrote', fn) VoxWriter(fn, vox).write()