Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ from pathlib import Path
|
|
| 7 |
import logging
|
| 8 |
from typing import Tuple, Optional
|
| 9 |
import struct
|
| 10 |
-
import shutil
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -15,24 +14,22 @@ logger = logging.getLogger(__name__)
|
|
| 15 |
|
| 16 |
class VoxParser:
|
| 17 |
"""Parse MagicaVoxel .vox files"""
|
| 18 |
-
def __init__(self,
|
| 19 |
-
self.
|
| 20 |
self.voxels = []
|
| 21 |
self.palette = []
|
| 22 |
self.size = {}
|
|
|
|
| 23 |
|
| 24 |
def parse(self) -> dict:
|
| 25 |
"""Parse the .vox file and extract voxel data"""
|
| 26 |
try:
|
| 27 |
-
# Handle both
|
| 28 |
-
if hasattr(self.
|
| 29 |
-
|
| 30 |
-
data = self.file_path.read()
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
data = f.read()
|
| 35 |
-
|
| 36 |
self.buffer = data
|
| 37 |
self.view = memoryview(data)
|
| 38 |
self.offset = 0
|
|
@@ -150,41 +147,27 @@ class VoxToGlbConverter:
|
|
| 150 |
Convert .vox file to .glb file
|
| 151 |
|
| 152 |
Args:
|
| 153 |
-
vox_file: Gradio file object
|
| 154 |
|
| 155 |
Returns:
|
| 156 |
Tuple of (glb_file_path, status_message)
|
| 157 |
"""
|
| 158 |
try:
|
| 159 |
-
#
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
return "", "No voxels found in the file"
|
| 175 |
-
|
| 176 |
-
# Create mesh from voxels
|
| 177 |
-
mesh = self.create_mesh_from_voxels(voxel_data)
|
| 178 |
-
|
| 179 |
-
# Save as GLB to a permanent location
|
| 180 |
-
output_path = str(Path(tempfile.gettempdir()) / f"converted_{os.path.basename(temp_vox_path)}.glb")
|
| 181 |
-
mesh.export(output_path)
|
| 182 |
-
|
| 183 |
-
# Clean up temp file
|
| 184 |
-
if temp_vox_path != vox_file:
|
| 185 |
-
os.unlink(temp_vox_path)
|
| 186 |
-
|
| 187 |
-
return output_path, f"Successfully converted {len(voxel_data['voxels'])} voxels to GLB format"
|
| 188 |
|
| 189 |
except Exception as e:
|
| 190 |
logger.error(f"Conversion error: {e}")
|
|
@@ -321,4 +304,4 @@ if __name__ == "__main__":
|
|
| 321 |
server_port=7860,
|
| 322 |
share=True,
|
| 323 |
show_error=True
|
| 324 |
-
)
|
|
|
|
| 7 |
import logging
|
| 8 |
from typing import Tuple, Optional
|
| 9 |
import struct
|
|
|
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 14 |
|
| 15 |
class VoxParser:
|
| 16 |
"""Parse MagicaVoxel .vox files"""
|
| 17 |
+
def __init__(self, file_data):
|
| 18 |
+
self.file_data = file_data
|
| 19 |
self.voxels = []
|
| 20 |
self.palette = []
|
| 21 |
self.size = {}
|
| 22 |
+
self.offset = 0
|
| 23 |
|
| 24 |
def parse(self) -> dict:
|
| 25 |
"""Parse the .vox file and extract voxel data"""
|
| 26 |
try:
|
| 27 |
+
# Handle both bytes and file objects
|
| 28 |
+
if hasattr(self.file_data, 'read'):
|
| 29 |
+
data = self.file_data.read()
|
|
|
|
| 30 |
else:
|
| 31 |
+
data = self.file_data
|
| 32 |
+
|
|
|
|
|
|
|
| 33 |
self.buffer = data
|
| 34 |
self.view = memoryview(data)
|
| 35 |
self.offset = 0
|
|
|
|
| 147 |
Convert .vox file to .glb file
|
| 148 |
|
| 149 |
Args:
|
| 150 |
+
vox_file: Gradio file object
|
| 151 |
|
| 152 |
Returns:
|
| 153 |
Tuple of (glb_file_path, status_message)
|
| 154 |
"""
|
| 155 |
try:
|
| 156 |
+
# Parse the .vox file directly from the file object
|
| 157 |
+
parser = VoxParser(vox_file)
|
| 158 |
+
voxel_data = parser.parse()
|
| 159 |
+
|
| 160 |
+
if not voxel_data['voxels']:
|
| 161 |
+
return "", "No voxels found in the file"
|
| 162 |
+
|
| 163 |
+
# Create mesh from voxels
|
| 164 |
+
mesh = self.create_mesh_from_voxels(voxel_data)
|
| 165 |
+
|
| 166 |
+
# Save as GLB
|
| 167 |
+
output_path = str(Path(tempfile.gettempdir()) / f"converted_model.glb")
|
| 168 |
+
mesh.export(output_path)
|
| 169 |
+
|
| 170 |
+
return output_path, f"Successfully converted {len(voxel_data['voxels'])} voxels to GLB format"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
logger.error(f"Conversion error: {e}")
|
|
|
|
| 304 |
server_port=7860,
|
| 305 |
share=True,
|
| 306 |
show_error=True
|
| 307 |
+
)
|