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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -1,12 +1,13 @@
1
  """
2
  Gradio app: .vox → .glb conversion + built-in Model3D viewer.
3
 
4
- Now we no longer use HTML + WebGL. This is simpler, more reliable, and fully compatible with Gradio 5+.
5
  """
6
 
7
  import gradio as gr
8
  import tempfile
9
  import trimesh
 
10
  from pathlib import Path
11
  from pyvox.parser import VoxParser
12
 
@@ -14,15 +15,21 @@ from pyvox.parser import VoxParser
14
  def vox_to_glb(file_path):
15
  vox = VoxParser(file_path).parse()
16
  model = vox.models[0]
 
17
  voxels = model.voxels
18
 
19
- cubes = []
 
 
 
 
20
  for v in voxels:
21
- cube = trimesh.creation.box(extents=(1,1,1))
22
- cube.apply_translation([v.x, v.y, v.z])
23
- cubes.append(cube)
 
24
 
25
- mesh = trimesh.util.concatenate(cubes)
26
  tmp = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
27
  mesh.export(tmp.name)
28
  return tmp.name
@@ -33,7 +40,11 @@ def handle_upload(file):
33
  return None
34
  ext = Path(file.name).suffix.lower()
35
  if ext == '.vox':
36
- return vox_to_glb(file.name)
 
 
 
 
37
  elif ext in ['.glb', '.gltf']:
38
  return file.name
39
  else:
@@ -49,5 +60,4 @@ with gr.Blocks() as demo:
49
 
50
 
51
  if __name__ == '__main__':
52
- demo.launch()
53
-
 
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
8
  import tempfile
9
  import trimesh
10
+ import numpy as np
11
  from pathlib import Path
12
  from pyvox.parser import VoxParser
13
 
 
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
 
40
  return None
41
  ext = Path(file.name).suffix.lower()
42
  if ext == '.vox':
43
+ try:
44
+ return vox_to_glb(file.name)
45
+ except Exception as e:
46
+ print(f"Failed to convert vox: {e}")
47
+ return None
48
  elif ext in ['.glb', '.gltf']:
49
  return file.name
50
  else:
 
60
 
61
 
62
  if __name__ == '__main__':
63
+ demo.launch()