Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# Updated app.py
|
| 2 |
import streamlit as st
|
| 3 |
import tempfile
|
| 4 |
import subprocess
|
|
@@ -6,8 +5,8 @@ import os
|
|
| 6 |
import requests
|
| 7 |
import base64
|
| 8 |
import sys
|
| 9 |
-
from streamlit_ace import st_ace
|
| 10 |
import shutil
|
|
|
|
| 11 |
|
| 12 |
st.set_page_config(page_title="Blender 3D Viewer", layout="wide")
|
| 13 |
st.title("🌍 Blender Script → 3D Viewer")
|
|
@@ -128,8 +127,9 @@ bpy.context.scene.render.engine = 'CYCLES'
|
|
| 128 |
bpy.context.scene.cycles.device = 'CPU'
|
| 129 |
bpy.context.scene.render.film_transparent = True
|
| 130 |
|
| 131 |
-
# Save the .blend file
|
| 132 |
-
|
|
|
|
| 133 |
bpy.ops.wm.save_as_mainfile(filepath=blend_file_path)
|
| 134 |
"""
|
| 135 |
|
|
@@ -229,14 +229,15 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 229 |
except Exception as e:
|
| 230 |
st.warning(f"pip install failed: {str(e)}")
|
| 231 |
|
| 232 |
-
# 4) Prepare environment with
|
| 233 |
env = os.environ.copy()
|
| 234 |
env["TEXTURE_PATHS"] = ",".join(texture_paths)
|
|
|
|
| 235 |
|
| 236 |
# 5) Run Blender to build .blend file
|
| 237 |
blend_path = os.path.join(tmp_dir, "scene.blend")
|
| 238 |
|
| 239 |
-
with st.status("Running Blender to create scene..."):
|
| 240 |
cmd1 = [blender_path, "--background", "--python", script_path]
|
| 241 |
try:
|
| 242 |
r1 = subprocess.run(
|
|
@@ -248,10 +249,15 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 248 |
text=True,
|
| 249 |
timeout=180 # 3 minute timeout
|
| 250 |
)
|
| 251 |
-
|
|
|
|
| 252 |
except subprocess.TimeoutExpired:
|
| 253 |
st.error("Blender process timed out after 3 minutes.")
|
| 254 |
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
except Exception as e:
|
| 256 |
st.error(f"Blender build failed: {str(e)}")
|
| 257 |
st.stop()
|
|
@@ -259,20 +265,32 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 259 |
# Check if blend file was created
|
| 260 |
if not os.path.exists(blend_path):
|
| 261 |
st.error("Blender did not create the expected scene.blend file.")
|
|
|
|
| 262 |
st.stop()
|
| 263 |
|
| 264 |
# 6) Export GLB with animation
|
| 265 |
glb_path = os.path.join(tmp_dir, "animation.glb")
|
| 266 |
|
| 267 |
-
with st.status("Exporting to GLB format..."):
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
cmd2 = [blender_path, "--background", "--python
|
| 276 |
try:
|
| 277 |
r2 = subprocess.run(
|
| 278 |
cmd2,
|
|
@@ -283,6 +301,8 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 283 |
text=True,
|
| 284 |
timeout=120 # 2 minute timeout
|
| 285 |
)
|
|
|
|
|
|
|
| 286 |
except Exception as e:
|
| 287 |
st.error(f"GLB export failed: {str(e)}")
|
| 288 |
st.stop()
|
|
@@ -292,6 +312,13 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 292 |
with open(glb_path, 'rb') as f:
|
| 293 |
data = f.read()
|
| 294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
b64 = base64.b64encode(data).decode()
|
| 296 |
|
| 297 |
# Display the 3D model viewer
|
|
@@ -303,7 +330,9 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 303 |
alt="3D Model"
|
| 304 |
auto-rotate
|
| 305 |
camera-controls
|
| 306 |
-
|
|
|
|
|
|
|
| 307 |
</model-viewer>
|
| 308 |
"""
|
| 309 |
st.components.v1.html(html, height=650)
|
|
@@ -317,13 +346,15 @@ if st.sidebar.button("Run & Export GLB"):
|
|
| 317 |
)
|
| 318 |
else:
|
| 319 |
st.error("GLB file was not generated successfully.")
|
|
|
|
|
|
|
| 320 |
|
| 321 |
finally:
|
| 322 |
# Clean up - remove temporary directory
|
| 323 |
try:
|
| 324 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
| 325 |
-
except:
|
| 326 |
-
|
| 327 |
|
| 328 |
# Add helpful information at the bottom
|
| 329 |
st.divider()
|
|
@@ -337,6 +368,36 @@ with st.expander("About this app"):
|
|
| 337 |
- Download the results as GLB files for use in other applications
|
| 338 |
|
| 339 |
The example script creates a rotating Earth with the uploaded texture.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
""")
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tempfile
|
| 3 |
import subprocess
|
|
|
|
| 5 |
import requests
|
| 6 |
import base64
|
| 7 |
import sys
|
|
|
|
| 8 |
import shutil
|
| 9 |
+
from streamlit_ace import st_ace
|
| 10 |
|
| 11 |
st.set_page_config(page_title="Blender 3D Viewer", layout="wide")
|
| 12 |
st.title("🌍 Blender Script → 3D Viewer")
|
|
|
|
| 127 |
bpy.context.scene.cycles.device = 'CPU'
|
| 128 |
bpy.context.scene.render.film_transparent = True
|
| 129 |
|
| 130 |
+
# Save the .blend file using an absolute path
|
| 131 |
+
output_dir = os.environ.get('BLENDER_OUTPUT_DIR', os.getcwd())
|
| 132 |
+
blend_file_path = os.path.join(output_dir, "scene.blend")
|
| 133 |
bpy.ops.wm.save_as_mainfile(filepath=blend_file_path)
|
| 134 |
"""
|
| 135 |
|
|
|
|
| 229 |
except Exception as e:
|
| 230 |
st.warning(f"pip install failed: {str(e)}")
|
| 231 |
|
| 232 |
+
# 4) Prepare environment with necessary variables
|
| 233 |
env = os.environ.copy()
|
| 234 |
env["TEXTURE_PATHS"] = ",".join(texture_paths)
|
| 235 |
+
env["BLENDER_OUTPUT_DIR"] = tmp_dir # Pass the tmp_dir to the Blender script
|
| 236 |
|
| 237 |
# 5) Run Blender to build .blend file
|
| 238 |
blend_path = os.path.join(tmp_dir, "scene.blend")
|
| 239 |
|
| 240 |
+
with st.status("Running Blender to create scene...") as status:
|
| 241 |
cmd1 = [blender_path, "--background", "--python", script_path]
|
| 242 |
try:
|
| 243 |
r1 = subprocess.run(
|
|
|
|
| 249 |
text=True,
|
| 250 |
timeout=180 # 3 minute timeout
|
| 251 |
)
|
| 252 |
+
status.update(label="Blender scene created successfully", state="complete")
|
| 253 |
+
st.text_area("Blender output", r1.stdout + r1.stderr, height=150)
|
| 254 |
except subprocess.TimeoutExpired:
|
| 255 |
st.error("Blender process timed out after 3 minutes.")
|
| 256 |
st.stop()
|
| 257 |
+
except subprocess.CalledProcessError as e:
|
| 258 |
+
st.error(f"Blender build failed with error code {e.returncode}")
|
| 259 |
+
st.text_area("Error details", e.stdout + e.stderr, height=150)
|
| 260 |
+
st.stop()
|
| 261 |
except Exception as e:
|
| 262 |
st.error(f"Blender build failed: {str(e)}")
|
| 263 |
st.stop()
|
|
|
|
| 265 |
# Check if blend file was created
|
| 266 |
if not os.path.exists(blend_path):
|
| 267 |
st.error("Blender did not create the expected scene.blend file.")
|
| 268 |
+
st.info("This might be due to an issue with the script or Blender configuration.")
|
| 269 |
st.stop()
|
| 270 |
|
| 271 |
# 6) Export GLB with animation
|
| 272 |
glb_path = os.path.join(tmp_dir, "animation.glb")
|
| 273 |
|
| 274 |
+
with st.status("Exporting to GLB format...") as status:
|
| 275 |
+
# Create a separate Python script for GLB export to avoid command line issues
|
| 276 |
+
export_script = os.path.join(tmp_dir, "export_glb.py")
|
| 277 |
+
with open(export_script, "w") as f:
|
| 278 |
+
f.write(f"""
|
| 279 |
+
import bpy
|
| 280 |
+
import os
|
| 281 |
+
|
| 282 |
+
# Load the blend file
|
| 283 |
+
bpy.ops.wm.open_mainfile(filepath=r'{blend_path}')
|
| 284 |
+
|
| 285 |
+
# Export to GLB
|
| 286 |
+
bpy.ops.export_scene.gltf(
|
| 287 |
+
filepath=r'{glb_path}',
|
| 288 |
+
export_format='GLB',
|
| 289 |
+
export_animations=True
|
| 290 |
+
)
|
| 291 |
+
""")
|
| 292 |
|
| 293 |
+
cmd2 = [blender_path, "--background", "--python", export_script]
|
| 294 |
try:
|
| 295 |
r2 = subprocess.run(
|
| 296 |
cmd2,
|
|
|
|
| 301 |
text=True,
|
| 302 |
timeout=120 # 2 minute timeout
|
| 303 |
)
|
| 304 |
+
status.update(label="GLB export completed successfully", state="complete")
|
| 305 |
+
st.text_area("Export output", r2.stdout + r2.stderr, height=100)
|
| 306 |
except Exception as e:
|
| 307 |
st.error(f"GLB export failed: {str(e)}")
|
| 308 |
st.stop()
|
|
|
|
| 312 |
with open(glb_path, 'rb') as f:
|
| 313 |
data = f.read()
|
| 314 |
|
| 315 |
+
file_size = len(data) / (1024 * 1024) # Size in MB
|
| 316 |
+
st.success(f"Successfully created GLB file ({file_size:.1f} MB)")
|
| 317 |
+
|
| 318 |
+
# Check if file isn't too large for embedding
|
| 319 |
+
if file_size > 50:
|
| 320 |
+
st.warning("The GLB file is quite large. The viewer might be slow to load.")
|
| 321 |
+
|
| 322 |
b64 = base64.b64encode(data).decode()
|
| 323 |
|
| 324 |
# Display the 3D model viewer
|
|
|
|
| 330 |
alt="3D Model"
|
| 331 |
auto-rotate
|
| 332 |
camera-controls
|
| 333 |
+
ar
|
| 334 |
+
shadow-intensity="1"
|
| 335 |
+
style="width:100%; height:600px; background-color: #f0f0f0;">
|
| 336 |
</model-viewer>
|
| 337 |
"""
|
| 338 |
st.components.v1.html(html, height=650)
|
|
|
|
| 346 |
)
|
| 347 |
else:
|
| 348 |
st.error("GLB file was not generated successfully.")
|
| 349 |
+
if os.path.exists(blend_path):
|
| 350 |
+
st.info("The Blender file was created, but the GLB export failed.")
|
| 351 |
|
| 352 |
finally:
|
| 353 |
# Clean up - remove temporary directory
|
| 354 |
try:
|
| 355 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
| 356 |
+
except Exception as e:
|
| 357 |
+
st.warning(f"Failed to clean up temporary files: {str(e)}")
|
| 358 |
|
| 359 |
# Add helpful information at the bottom
|
| 360 |
st.divider()
|
|
|
|
| 368 |
- Download the results as GLB files for use in other applications
|
| 369 |
|
| 370 |
The example script creates a rotating Earth with the uploaded texture.
|
| 371 |
+
|
| 372 |
+
### Troubleshooting
|
| 373 |
+
|
| 374 |
+
- If you encounter errors, check the console output for details
|
| 375 |
+
- Make sure your script correctly saves files using absolute paths
|
| 376 |
+
- For complex models, allow more time for processing
|
| 377 |
+
- If the app doesn't work at all, the Hugging Face Space might need proper Blender installation
|
| 378 |
""")
|
| 379 |
|
| 380 |
+
with st.expander("Script Tips"):
|
| 381 |
+
st.markdown("""
|
| 382 |
+
### Tips for writing Blender scripts
|
| 383 |
+
|
| 384 |
+
1. **Accessing textures**: Use the environment variable `TEXTURE_PATHS` to get the paths to uploaded textures
|
| 385 |
+
|
| 386 |
+
```python
|
| 387 |
+
# Example code to load the first uploaded texture
|
| 388 |
+
texture_paths = os.environ.get('TEXTURE_PATHS', '').split(',')
|
| 389 |
+
if texture_paths and texture_paths[0]:
|
| 390 |
+
image = bpy.data.images.load(texture_paths[0])
|
| 391 |
+
```
|
| 392 |
+
|
| 393 |
+
2. **Saving files**: Always use absolute paths with the environment variable
|
| 394 |
+
|
| 395 |
+
```python
|
| 396 |
+
# Example code to save your blend file
|
| 397 |
+
output_dir = os.environ.get('BLENDER_OUTPUT_DIR', os.getcwd())
|
| 398 |
+
blend_file_path = os.path.join(output_dir, "scene.blend")
|
| 399 |
+
bpy.ops.wm.save_as_mainfile(filepath=blend_file_path)
|
| 400 |
+
```
|
| 401 |
+
|
| 402 |
+
3. **Animations**: Make sure to set keyframes if you want your model to animate
|
| 403 |
+
""")
|