Fix asset path usage and refactor 3D model generation logic

This commit is contained in:
2026-05-14 10:39:56 +02:00
parent 801007fe7f
commit 562a9ad2e6

33
main.py
View File

@@ -851,7 +851,7 @@ class Api:
def edit_external(self, filepath: str): def edit_external(self, filepath: str):
# filepath kommt von 2D als "output/foo.png" oder von 3D als "output/foo.png" # filepath kommt von 2D als "output/foo.png" oder von 3D als "output/foo.png"
ext = os.path.splitext(filepath)[1].lower() ext = os.path.splitext(filepath)[1].lower()
full_path = os.path.join(BASE_DIR, "web", filepath) full_path = os.path.join(WEB_DIR, filepath)
# Ist es ein Bild oder das PNG eines 3D-Modells? # Ist es ein Bild oder das PNG eines 3D-Modells?
if ext == ".png": if ext == ".png":
# Prüfe, ob ein GLB daneben liegt # Prüfe, ob ein GLB daneben liegt
@@ -879,7 +879,7 @@ class Api:
# ------------- Generate 3D Model ------------- # ------------- Generate 3D Model -------------
def generate_3d_model(self, filepath: str): def generate_3d_model(self, filepath: str):
# filepath kommt aus JS als "output/flower-1.png" # filepath kommt aus JS als "output/flower-1.png"
full_path = os.path.join(BASE_DIR, "web", filepath) full_path = os.path.join(WEB_DIR, filepath)
if not os.path.isfile(full_path): if not os.path.isfile(full_path):
self._js("window.on_error", f"Image not found: {full_path}") self._js("window.on_error", f"Image not found: {full_path}")
return {"status": "missing_file"} return {"status": "missing_file"}
@@ -901,33 +901,8 @@ class Api:
) )
return return
# Construct the command: sys.executable ensures we run with same Python interpreter glb_path = self._run_generate_3d_and_return(img_path)
script_path = os.path.join(BASE_DIR, "image_to_3d.py")
cmd = [sys.executable, script_path, img_path]
base, _ = os.path.splitext(img_path)
glb_path = base + ".glb"
previous_mtime = os.path.getmtime(glb_path) if os.path.isfile(glb_path) else None
result = subprocess.run(
cmd,
capture_output=True,
text=True,
env=self._subprocess_env(),
)
# Check for success: sidecar or actual .glb in same folder
current_mtime = os.path.getmtime(glb_path) if os.path.isfile(glb_path) else None
if current_mtime is not None and current_mtime != previous_mtime:
# Notify JavaScript
self._js("window.on_3d_generated", glb_path) self._js("window.on_3d_generated", glb_path)
else:
# If the script failed or no .glb was produced, send an error
err_msg = (
result.stderr.strip()
or result.stdout.strip()
or "3D conversion did not produce .glb"
)
self._js("window.on_error", f"3D Model failed: {err_msg}")
except Exception as e: except Exception as e:
self._js("window.on_error", f"3D conversion exception: {e}") self._js("window.on_error", f"3D conversion exception: {e}")
@@ -942,7 +917,7 @@ class Api:
Called when user wants both 3D model and equirectangular HDRI for an image. Called when user wants both 3D model and equirectangular HDRI for an image.
Kicks off both processes, waits for both, and (later) calls the next step. Kicks off both processes, waits for both, and (later) calls the next step.
""" """
full_path = os.path.join(BASE_DIR, "web", filepath) full_path = os.path.join(WEB_DIR, filepath)
if not os.path.isfile(full_path): if not os.path.isfile(full_path):
self._js("window.on_error", f"Image not found: {full_path}") self._js("window.on_error", f"Image not found: {full_path}")
return {"status": "missing_file"} return {"status": "missing_file"}