|  | import sys
import re
import json
import glob
import os
import subprocess
PREFIX = "PREFIX"
def combine(base_path, highlight_path, output_path):
    base = open(base_path, "r", encoding="utf-8").read()
    highlight = open(highlight_path, "r", encoding="utf-8").read()
    
    base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
    highlight_data = highlight.replace("</metadata>", "</metadata>" + base_data)
    with open(output_path, "w", encoding="utf-8") as f:
        f.write(highlight_data)
    subprocess.run([INKSCAPE, "--without-gui", "--export-plain-svg=" + output_path, "--export-area-drawing", output_path], shell=True)
if len(sys.argv) <= 1:
    print(f"Usage: {sys.argv[0]} [location name]")
    sys.exit(1)
template = """
        {{
            name: "{0}",
            sides: {{
                "Side": {{ height: math.unit({1}, "meters") }},
                "Front": {{ height: math.unit({2}, "meters") }},
                "Top": {{ height: math.unit({3}, "meters") }},
            }}
        }}"""
path = os.path.dirname(sys.argv[0])
config = json.load(open(os.path.join(path, "config.json"), "r", encoding="utf-8"))
output = []
os.makedirs("./combined/" + sys.argv[1], exist_ok=True)
target_file = "vehicles"
os.makedirs(f"{PREFIX}/furry/macrovision/media/" + target_file + "/" + sys.argv[1] + "/", exist_ok=True)
POTRACE = config["potrace"]
INKSCAPE = config["inkscape"]
METERS_PER_UNIT = 0.03048 * 0.97681253/ 1.9898
if sys.argv[1] == "Tanks":
    METERS_PER_UNIT /= 1.50811320755
for path in glob.glob("*.json"):
    base_name = os.path.splitext(os.path.basename(path))[0]
    data = json.load(open(path, "r", encoding="utf-8"))
    sizes = {}
    for view, scale in data.items():
        name = base_name + "-" + view
        result = f"{PREFIX}/furry/macrovision/media/vehicles/" + sys.argv[1] + "/" + name + ".svg"
        if view == "Top":
            height = int(subprocess.check_output(["convert", name + ".png", "-trim", "-rotate", "270", "-format", "%[h]", "info:"], shell=True).decode("utf-8"))
        else:
            height = int(subprocess.check_output(["convert", name + ".png", "-trim", "-format", "%[h]", "info:"], shell=True).decode("utf-8"))
        sizes[view] = height / scale * METERS_PER_UNIT
        if os.path.exists(result) and os.path.getmtime(name + ".png") < os.path.getmtime(result):
            print("Skipping " + name)
            continue
        input_base = "base.bmp"
        input_highlight = "highlight.bmp"
        subprocess.run(["convert", name + ".png", "xc:#000000", "-channel", "RGB", "-clut", "-background", "#ffffff", "-flatten", input_base], shell=True)
        subprocess.run(["convert", name + ".png", "-background", "#ffffff", "-flatten", input_highlight], shell=True)
        
        # rotate the top view, since I capture it horizontally
        if view == "Top":
            subprocess.run(["mogrify", "-rotate", "270", input_base], shell=True)
            subprocess.run(["mogrify", "-rotate", "270", input_highlight], shell=True)
        
        base = "base.svg"
        subprocess.run([POTRACE, input_base, "-b", "svg", "-o", base], shell=True)
        highlight = "highlight.svg"
        subprocess.run([POTRACE, input_highlight, "-b", "svg", "-C", "#1a1a1a", "-o", highlight], shell=True)
        combine(base, highlight, result)
        os.unlink(input_base)
        os.unlink(input_highlight)
    output.append(template.format(base_name, sizes["Side"], sizes["Front"], sizes["Top"]))
    
with open(f"{PREFIX}/furry/macrovision/presets/vehicles.js", "r", encoding="utf-8") as file:
    file_data = file.read()
    templated = "[" + ",".join(output) + "\n    ];"
    file_data = re.sub("const data" + sys.argv[1] + " =.*?];", "const data" + sys.argv[1] + " = " + templated, file_data, flags=re.DOTALL)
with open(f"{PREFIX}/furry/macrovision/presets/vehicles.js", "w", encoding="utf-8") as file:
    file.write(file_data)
 |