Last active
September 29, 2021 16:16
-
-
Save StoneLabs/93cf508943fa4b508b41d2a99c8f564d to your computer and use it in GitHub Desktop.
Minecraft world renderer using Mineways (snapshot compatible)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import cv2 | |
from jinja2 import Template | |
from rich.progress import Progress, BarColumn, TimeRemainingColumn, TextColumn, SpinnerColumn | |
from rich.traceback import install as install_traceback | |
from rich.console import Console | |
install_traceback() | |
print = Console().print | |
### config | |
output_path = "./render/" | |
output_final = "rendered.png" | |
tmp_cfg_path = "./render/script.mwscript" | |
mineways_exe = "./mineways/Mineways.exe" | |
world_path = "./world" | |
template = "Minecraft world: {{worldPath}}\n\ | |
Selection location min to max: {{fromBlocks.x}}, -64, {{fromBlocks.z}} to {{toBlocks.x}}, 255, {{toBlocks.z}}\n\ | |
Export map: {{outputFile}}\n\ | |
Close" | |
regionsRX = 10 | |
regionsRZ = 10 | |
region_size = 512 | |
print("[red]Please confirm:") | |
print(f" Rendering {regionsRX*2}x{regionsRZ*2} ({regionsRX*2*regionsRZ*2}) regions") | |
print(f" Covering blocks {-regionsRX * region_size},{-regionsRZ * region_size} to {(regionsRX) * region_size - 1},{(regionsRZ) * region_size - 1}") | |
input() | |
print("[red]\nStarting Render\n[white]===============\n") | |
if not os.path.exists(output_path): | |
os.mkdir(output_path) | |
if not os.path.isdir(output_path): | |
exit(1) | |
template = Template(template) | |
images_paths = [[0 for x in range(-regionsRX, regionsRX)] for z in range(-regionsRZ, regionsRZ)] | |
with Progress( | |
SpinnerColumn(), | |
"[progress.description]{task.description}", | |
TextColumn("[bold blue]{task.fields[coordinate]}", justify="right"), | |
BarColumn(), | |
"[progress.percentage]{task.percentage:>3.0f}%", | |
TimeRemainingColumn(), | |
) as progress: | |
region_task = progress.add_task("[red]Rendering regions...", coordinate=(None, None), total=regionsRX*2*regionsRZ*2) | |
for regionX in range(-regionsRX, regionsRX): | |
for regionZ in range(-regionsRZ, regionsRZ): | |
progress.update(region_task, coordinate=(regionX, regionZ), advance=1) | |
from_blocks = {"x": regionX * region_size, "z": regionZ * region_size} | |
to_blocks = {"x": (regionX + 1) * region_size - 1, "z": (regionZ + 1) * region_size - 1} | |
output_file = os.path.join(os.path.abspath(output_path), f"c_{regionX}_{regionZ}.png") | |
with open(tmp_cfg_path, "w") as cfg_file: | |
cfg_file.write(template.render(fromBlocks=from_blocks, toBlocks=to_blocks, outputFile=output_file, worldPath=os.path.abspath(world_path))) | |
subprocess.run([mineways_exe, "-w", "1", "1", "-s", "none", os.path.abspath(tmp_cfg_path)]) | |
images_paths[regionsRZ + regionZ][regionsRZ + regionX] = output_file | |
with Progress(SpinnerColumn(), "[progress.description]{task.description}") as progress: | |
region_task = progress.add_task("[red]Stitching images...", total=2) | |
images_loaded = [[cv2.imread(image) for image in imagesRow] for imagesRow in images_paths] | |
progress.update(region_task, advance=1) | |
map = cv2.vconcat([cv2.hconcat(imagesRow) for imagesRow in images_loaded]) | |
cv2.imwrite(output_final, map) | |
progress.update(region_task, advance=1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment