forked from ss14Starlight/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_render.py
More file actions
193 lines (126 loc) · 4.18 KB
/
Copy pathauto_render.py
File metadata and controls
193 lines (126 loc) · 4.18 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import sys
import subprocess
from datetime import datetime
from PIL import Image
# ========= Options =========
CHUNK_SIZE = 256
# output renderer dir
RENDER_OUTPUT_DIR = "./Resources/MapImages"
# chunk output
CHUNK_OUTPUT_DIR = "map"
# log file
LOG_FILE = "render_log.txt"
# output viewer json?
VIEWER_JSON = True
# output parallax?
PARALLAX = False # Note: Parallax output doesn't work how it's supposed to be, It's isn't per-map parallax, it just saves default parallax at MapImages. So it's useless thing.
# ==============================
def log(message):
time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
text = f"[{time}] {message}"
print(text)
with open(LOG_FILE, "a", encoding="utf8") as f:
f.write(text + "\n")
def get_next_grid_id(path):
os.makedirs(path, 511, exist_ok=True)
grid = 0
while os.path.exists(os.path.join(path, CHUNK_OUTPUT_DIR, str(grid))):
grid += 1
return grid
def slice_map(image_path):
try:
img = Image.open(image_path)
except Exception as e:
log(f"Image opening error {image_path}: {e}")
return
width, height = img.size
img_dir = os.path.dirname(os.path.abspath(image_path))
grid_id = get_next_grid_id(img_dir)
log(f"Map slicing -> grid {grid_id}")
for x in range(0, width, CHUNK_SIZE):
col = x // CHUNK_SIZE
for y in range(0, height, CHUNK_SIZE):
row = y // CHUNK_SIZE
chunk = img.crop((x, y, x + CHUNK_SIZE, y + CHUNK_SIZE))
chunk_dir = os.path.join(img_dir, CHUNK_OUTPUT_DIR, str(grid_id), str(col), str(row))
os.makedirs(chunk_dir, exist_ok=True)
chunk.save(os.path.join(chunk_dir, "0"), format="PNG")
log("Cutting completed")
def render_map(map_id):
log(f"Render map {map_id}")
cmd = [
"dotnet",
"run",
"--project",
"Content.MapRenderer",
map_id
]
if (VIEWER_JSON):
cmd.append("--viewer")
if (PARALLAX):
cmd.append("--parallax")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
log(f"Map rendering error {map_id}")
return False
return True
def find_rendered_maps(map_id):
folder = os.path.abspath(os.path.join(RENDER_OUTPUT_DIR, map_id))
if not os.path.exists(folder):
log(f"Cant find render for {map_id} in {folder}")
return []
files = []
for name in os.listdir(folder):
lower = name.lower()
if lower.startswith(map_id.lower() + "-") and lower.endswith(".png"):
files.append(os.path.join(folder, name))
return sorted(files)
def process_map(map_id):
success = render_map(map_id)
if not success:
return
images = find_rendered_maps(map_id)
if not images:
log(f"Map render {map_id} not found")
return
for image_path in images:
log(f"Render found: {image_path}")
slice_map(image_path)
def load_maps_from_txt(path):
with open(path, "r", encoding="utf8") as f:
return [line.strip() for line in f if line.strip()]
def main():
if len(sys.argv) > 1:
txt = sys.argv[1]
if os.path.exists(txt):
maps = load_maps_from_txt(txt)
else:
maps = sys.argv[1:]
else:
maps = input("Enter map_id through a space: ").split()
if not maps:
print("No maps to process")
return
log(f"Start of maps processing: {maps}")
already_processed = []
confirmation = ""
for map_id in maps:
possible_renderers = find_rendered_maps(map_id)
if not possible_renderers:
continue
if confirmation == "":
confirmation = input("Founded already rendered image, slice it? Write 'true' to confirm.")
if confirmation.lower() == "true":
already_processed.append(map_id)
for image_path in possible_renderers:
log(f"Render found: {image_path}")
slice_map(image_path)
for map_id in maps:
if map_id in already_processed:
continue
process_map(map_id)
log("All maps have been processed.")
if __name__ == "__main__":
main()