MySafeCode commited on
Commit
e5b87f1
·
verified ·
1 Parent(s): f4b5e4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -1,7 +1,6 @@
1
  """
2
- Gradio app: .vox → .glb conversion + built-in Model3D viewer.
3
-
4
- Now using safer voxel-to-mesh conversion via occupancy grid + marching cubes.
5
  """
6
 
7
  import gradio as gr
@@ -15,22 +14,32 @@ from pyvox.parser import VoxParser
15
  def vox_to_glb(file_path):
16
  vox = VoxParser(file_path).parse()
17
  model = vox.models[0]
18
- size_x, size_y, size_z = model.size.x, model.size.y, model.size.z
19
  voxels = model.voxels
 
20
 
21
  if not voxels:
22
- raise ValueError("No voxels found in file")
23
 
24
- # Create occupancy grid
25
- grid = np.zeros((size_x, size_y, size_z), dtype=bool)
26
- for v in voxels:
27
- grid[v.x, v.y, v.z] = True
 
 
 
 
 
 
 
 
 
28
 
29
- # Convert occupancy grid to mesh
30
- mesh = trimesh.voxel.ops.matrix_to_marching_cubes(grid)
 
 
31
 
32
- # Export to temporary glb
33
- tmp = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
34
  mesh.export(tmp.name)
35
  return tmp.name
36
 
 
1
  """
2
+ Gradio app: robust .vox → .glb conversion + built-in Model3D viewer.
3
+ Handles small and large voxel files, centers and colors mesh to avoid 'paper icon'.
 
4
  """
5
 
6
  import gradio as gr
 
14
  def vox_to_glb(file_path):
15
  vox = VoxParser(file_path).parse()
16
  model = vox.models[0]
 
17
  voxels = model.voxels
18
+ size_x, size_y, size_z = model.size.x, model.size.y, model.size.z
19
 
20
  if not voxels:
21
+ raise ValueError("No voxels found")
22
 
23
+ # Small grid: cube per voxel, Large grid: marching cubes
24
+ if max(size_x, size_y, size_z) <= 16:
25
+ cubes = []
26
+ for v in voxels:
27
+ cube = trimesh.creation.box(extents=(1,1,1))
28
+ cube.apply_translation([v.x, v.y, v.z])
29
+ cubes.append(cube)
30
+ mesh = trimesh.util.concatenate(cubes)
31
+ else:
32
+ grid = np.zeros((size_x, size_y, size_z), dtype=bool)
33
+ for v in voxels:
34
+ grid[v.x, v.y, v.z] = True
35
+ mesh = trimesh.voxel.ops.matrix_to_marching_cubes(grid)
36
 
37
+ # Center mesh, scale, add color
38
+ mesh.apply_translation(-mesh.centroid)
39
+ mesh.apply_scale(1.0)
40
+ mesh.visual.vertex_colors = [200, 100, 50, 255]
41
 
42
+ tmp = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
 
43
  mesh.export(tmp.name)
44
  return tmp.name
45