Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,8 @@ import trimesh
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import struct
|
| 7 |
-
import logging
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Tuple
|
| 10 |
-
import shutil
|
| 11 |
-
|
| 12 |
-
# Configure logging
|
| 13 |
-
logging.basicConfig(level=logging.INFO)
|
| 14 |
-
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
class VoxParser:
|
| 17 |
"""MagicaVoxel .vox file parser"""
|
|
@@ -33,8 +27,7 @@ class VoxParser:
|
|
| 33 |
if header != 'VOX ':
|
| 34 |
raise ValueError("Invalid VOX file header")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
offset += 4
|
| 38 |
|
| 39 |
voxels = []
|
| 40 |
palette = []
|
|
@@ -42,6 +35,9 @@ class VoxParser:
|
|
| 42 |
|
| 43 |
# Parse chunks
|
| 44 |
while offset < len(data):
|
|
|
|
|
|
|
|
|
|
| 45 |
chunk_id = data[offset:offset+4].decode('ascii', errors='ignore')
|
| 46 |
offset += 4
|
| 47 |
chunk_size = struct.unpack('<I', data[offset:offset+4])[0]
|
|
@@ -50,26 +46,28 @@ class VoxParser:
|
|
| 50 |
offset += 4
|
| 51 |
|
| 52 |
if chunk_id == 'SIZE':
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
offset += chunk_size
|
| 59 |
elif chunk_id == 'XYZI':
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
elif chunk_id == 'RGBA':
|
| 74 |
for i in range(256):
|
| 75 |
if offset + 4 <= len(data):
|
|
@@ -88,7 +86,7 @@ class VoxParser:
|
|
| 88 |
'size': size
|
| 89 |
}
|
| 90 |
except Exception as e:
|
| 91 |
-
|
| 92 |
|
| 93 |
def _default_palette(self) -> list:
|
| 94 |
colors = []
|
|
@@ -126,7 +124,6 @@ class VoxToGlbConverter:
|
|
| 126 |
return output_path, f"Converted {voxel_count} voxels to GLB format"
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
-
logger.error(f"Conversion error: {e}")
|
| 130 |
return "", f"Error converting file: {str(e)}"
|
| 131 |
|
| 132 |
def create_mesh_from_voxels(self, voxel_data: dict) -> trimesh.Trimesh:
|
|
@@ -214,8 +211,8 @@ def create_gradio_interface():
|
|
| 214 |
# Use Gradio's native Model3D component for GLB files
|
| 215 |
model_3d = gr.Model3D(
|
| 216 |
label="GLB Preview",
|
| 217 |
-
height=
|
| 218 |
-
camera_radius=
|
| 219 |
camera_center=(0, 0, 0),
|
| 220 |
zoom_speed=1.0,
|
| 221 |
pan_speed=2.0,
|
|
@@ -233,7 +230,7 @@ def create_gradio_interface():
|
|
| 233 |
|
| 234 |
convert_btn.click(
|
| 235 |
fn=convert_with_preview,
|
| 236 |
-
inputs=[
|
| 237 |
outputs=[glb_output, status_output, model_3d]
|
| 238 |
)
|
| 239 |
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import struct
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class VoxParser:
|
| 11 |
"""MagicaVoxel .vox file parser"""
|
|
|
|
| 27 |
if header != 'VOX ':
|
| 28 |
raise ValueError("Invalid VOX file header")
|
| 29 |
|
| 30 |
+
offset += 4 # Skip version
|
|
|
|
| 31 |
|
| 32 |
voxels = []
|
| 33 |
palette = []
|
|
|
|
| 35 |
|
| 36 |
# Parse chunks
|
| 37 |
while offset < len(data):
|
| 38 |
+
if offset + 12 > len(data):
|
| 39 |
+
break
|
| 40 |
+
|
| 41 |
chunk_id = data[offset:offset+4].decode('ascii', errors='ignore')
|
| 42 |
offset += 4
|
| 43 |
chunk_size = struct.unpack('<I', data[offset:offset+4])[0]
|
|
|
|
| 46 |
offset += 4
|
| 47 |
|
| 48 |
if chunk_id == 'SIZE':
|
| 49 |
+
if offset + 12 <= len(data):
|
| 50 |
+
size = {
|
| 51 |
+
'x': struct.unpack('<I', data[offset:offset+4])[0],
|
| 52 |
+
'y': struct.unpack('<I', data[offset+4:offset+8])[0],
|
| 53 |
+
'z': struct.unpack('<I', data[offset+8:offset+12])[0]
|
| 54 |
+
}
|
| 55 |
offset += chunk_size
|
| 56 |
elif chunk_id == 'XYZI':
|
| 57 |
+
if offset + 4 <= len(data):
|
| 58 |
+
num_voxels = struct.unpack('<I', data[offset:offset+4])[0]
|
| 59 |
+
offset += 4
|
| 60 |
+
|
| 61 |
+
for i in range(num_voxels):
|
| 62 |
+
if offset + 4 <= len(data):
|
| 63 |
+
x, y, z, color_index = struct.unpack('BBBB', data[offset:offset+4])
|
| 64 |
+
voxels.append({
|
| 65 |
+
'x': x,
|
| 66 |
+
'y': y,
|
| 67 |
+
'z': z,
|
| 68 |
+
'color_index': color_index
|
| 69 |
+
})
|
| 70 |
+
offset += 4
|
| 71 |
elif chunk_id == 'RGBA':
|
| 72 |
for i in range(256):
|
| 73 |
if offset + 4 <= len(data):
|
|
|
|
| 86 |
'size': size
|
| 87 |
}
|
| 88 |
except Exception as e:
|
| 89 |
+
return {'voxels': [], 'palette': self._default_palette(), 'size': {'x': 0, 'y': 0, 'z': 0}}
|
| 90 |
|
| 91 |
def _default_palette(self) -> list:
|
| 92 |
colors = []
|
|
|
|
| 124 |
return output_path, f"Converted {voxel_count} voxels to GLB format"
|
| 125 |
|
| 126 |
except Exception as e:
|
|
|
|
| 127 |
return "", f"Error converting file: {str(e)}"
|
| 128 |
|
| 129 |
def create_mesh_from_voxels(self, voxel_data: dict) -> trimesh.Trimesh:
|
|
|
|
| 211 |
# Use Gradio's native Model3D component for GLB files
|
| 212 |
model_3d = gr.Model3D(
|
| 213 |
label="GLB Preview",
|
| 214 |
+
height=300,
|
| 215 |
+
camera_radius=8.0,
|
| 216 |
camera_center=(0, 0, 0),
|
| 217 |
zoom_speed=1.0,
|
| 218 |
pan_speed=2.0,
|
|
|
|
| 230 |
|
| 231 |
convert_btn.click(
|
| 232 |
fn=convert_with_preview,
|
| 233 |
+
inputs=[vox_file],
|
| 234 |
outputs=[glb_output, status_output, model_3d]
|
| 235 |
)
|
| 236 |
|