mhtbhatia's picture
Upload 12 files
cb789b0 verified
import os
import time
import sys
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
PYTHON_EXE = sys.executable
ENGINE = os.path.join(os.path.dirname(__file__), "genmake_crystal_engine.py")
INPUT_DIR = os.path.join(os.path.dirname(__file__), "GenMake_Input")
READY_DIR = os.path.join(os.path.dirname(__file__), "GenMake_XCS_Ready")
os.makedirs(INPUT_DIR, exist_ok=True)
os.makedirs(READY_DIR, exist_ok=True)
def wait_until_stable(path, timeout=40):
last = -1
for _ in range(timeout):
try:
s = os.path.getsize(path)
if s > 0 and s == last:
return True
last = s
except OSError:
pass
time.sleep(1)
return False
class Handler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
src = event.src_path
if not src.lower().endswith((".jpg", ".jpeg", ".png")):
return
print("\nNew image:", src)
if not wait_until_stable(src):
print("File not stable; skipping:", src)
return
# Default production settings (tune once you test crystals)
product = "block_50x50x80"
quality = "medium"
cmd = [
PYTHON_EXE, ENGINE, src,
"--product", product,
"--quality", quality,
"--fp16",
"--face_crop",
]
print("Running:", " ".join(cmd))
r = subprocess.run(cmd, capture_output=True, text=True)
if r.stdout:
print(r.stdout)
if r.returncode != 0:
print("ERROR:\n", r.stderr)
return
# Find newest GenMake output folder and copy GLB to Ready folder
out_dir = os.path.join(os.path.dirname(__file__), "GenMake_Output")
newest = None
if os.path.isdir(out_dir):
folders = [os.path.join(out_dir, f) for f in os.listdir(out_dir)]
folders = [f for f in folders if os.path.isdir(f)]
if folders:
newest = max(folders, key=os.path.getmtime)
if newest:
glb = os.path.join(newest, "GenMake_EngraveReady.glb")
if os.path.exists(glb):
base = os.path.splitext(os.path.basename(src))[0]
dest = os.path.join(READY_DIR, f"{base}_{product}.glb")
if os.path.exists(dest):
os.remove(dest)
os.replace(glb, dest)
print("READY:", dest)
# Clean input
try:
os.remove(src)
print("Input cleaned.")
except Exception as e:
print("Could not delete input:", e)
observer = Observer()
observer.schedule(Handler(), INPUT_DIR, recursive=False)
observer.start()
print("GenMake Watchdog running.")
print("Drop photos into:", INPUT_DIR)
print("GLB output appears in:", READY_DIR)
print("Press CTRL+C to stop.\n")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()