File size: 2,130 Bytes
594e028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''

the http-reuqest for headless mode process

make sure you run

.\ForgeOptics.exe --headless --host 127.0.0.1 --backend cuda --port 8080



'''
import os, sys
import urllib.request
import urllib.error
import json

url = "http://127.0.0.1:8080/render"
status_url = "http://127.0.0.1:8080/status"

def status_check() -> bool:
    try:
        with urllib.request.urlopen(status_url, timeout=3) as response:
            body = response.read().decode('utf-8')
            status_data = json.loads(body)
            # Returns True only if 'ok' is True AND 'busy' is False
            return status_data.get("ok", False) and not status_data.get("busy", True)
    except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError) as e:
        # Debug line: uncomment this if it still returns False to see why!
        print(f"Connection failed: {e}")
        return False


def load_json(file_path):
    '''load dict -> json string -> raw bytes'''
    with open(file_path, "r", encoding="utf-8") as f:
        scene_data = json.load(f)
    # modif-scene settings here
    json_bytes = json.dumps(scene_data).encode("utf-8")
    return json_bytes

def structure_light_process(scene_config: str):
    '''http-request-process'''
    data = load_json(scene_config)
    req = urllib.request.Request(
        url,
        data=data,
        headers={"Content-Type": "application/json"},
        method="POST"
    )
    with urllib.request.urlopen(req) as response:
        print(response.status)
        print(response.read().decode('utf-8'))

def batch():
    # single-item test:
    # single_test_file_path = r"...\scene_1.json"
    # structure_light_process(single_test_file_path)

    scenes_dir = r"scene folder path"
    for index, item in enumerate( os.listdir(scenes_dir)):
        scene_path = os.path.join(scenes_dir, item)
        structure_light_process(scene_path)
    pass

def main():
    if status_check():
        print("DEBUG-->ready to process!")
        batch()
    else:
        print("ERROR-->server is down!")

if __name__ == "__main__":
    main()