File size: 3,188 Bytes
cb789b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()