import sys import re import json import os import subprocess import pathlib import time from xml.dom import minidom # an affront to god def combine(base_path, dark_path, medium_path, light_path, bright_path, tmp_output_path, output_path): base = open(base_path, "r", encoding="utf-8").read() dark = open(dark_path, "r", encoding="utf-8").read() medium = open(medium_path, "r", encoding="utf-8").read() light = open(light_path, "r", encoding="utf-8").read() bright = open(bright_path, "r", encoding="utf-8").read() base_data = re.search("", "" + base_data + "\n" + dark_data + "\n" + medium_data + "\n" + light_data) with open(tmp_output_path, "w", encoding="utf-8") as f: f.write(bright_data) return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg", "--export-area-drawing", "--export-filename=" + output_path, tmp_output_path], shell=False) configdir = pathlib.Path(__file__).parent configpath = configdir.joinpath("config.json") config = json.load(open(configpath, encoding="utf-8")) workdir = pathlib.Path(config["work-directory"]) print(sys.argv) sourcedir = workdir.joinpath(sys.argv[1]) macrodir = pathlib.Path(config["macrovision-directory"]) print(sourcedir, macrodir) side_strings = [] POTRACE = config["potrace"] INKSCAPE = config["inkscape"] output = {} with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file: all_data = json.load(file) group_name = all_data["name"] category = all_data["kind"] outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name) os.makedirs(outputdir, exist_ok=True) for data in all_data["forms"]: name = data["name"] for view in data["views"]: view_name = view["name"] input = sourcedir.joinpath(name + "-" + view_name + ".png").__str__() input_noline_raw = sourcedir.joinpath(name + "-" + view_name + "-" + "noline.png").__str__() result = outputdir.joinpath(name + "-" + view_name + ".svg").__str__() print(result) if os.path.exists(result): if os.path.getmtime(input) < os.path.getmtime(result): print("Skipping ", input) continue else: os.unlink(result) input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp").__str__() input_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.bmp").__str__() input_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.bmp").__str__() input_light = sourcedir.joinpath(name + "-" + view_name + "-light.bmp").__str__() input_bright = sourcedir.joinpath(name + "-" + view_name + "-bright.bmp").__str__() input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__() procs = [] procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RGB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-flatten", input_base], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "GB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_dark], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_medium], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RG", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_light], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-fill", "black", "-fuzz", "10%", "+opaque", "#FFFFFF", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_bright], shell=False)) # to correct for extra height from lines procs.append(subprocess.Popen(["magick", "convert", input_noline_raw, "-channel", "RGB", "-evaluate", "set", "0", "-background", "#FFFFFF", "-flatten", input_noline], shell=False)) [proc.wait() for proc in procs] output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__() output_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.svg").__str__() output_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.svg").__str__() output_light = sourcedir.joinpath(name + "-" + view_name + "-light.svg").__str__() output_bright = sourcedir.joinpath(name + "-" + view_name + "-bright.svg").__str__() output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__() procs = [] alpha = str(all_data["trace_alpha"]) procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-a", alpha, "-o", output_base], shell=False)) procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-a", alpha, "-C", "#1a1a1a", "-o", output_dark], shell=False)) procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_medium], shell=False)) procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-a", alpha, "-C", "#4d4d4d", "-o", output_light], shell=False)) procs.append(subprocess.Popen([POTRACE, input_bright, "-b", "svg", "-a", alpha, "-C", "#666666", "-o", output_bright], shell=False)) procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_noline], shell=False)) [proc.wait() for proc in procs] procs = [] noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__() result_tmp = outputdir.joinpath(name + "-" + view_name + "-tmp.svg").__str__() noline_result_tmp = sourcedir.joinpath(name + "-" + view_name + "-noline_processed-tmp.svg").__str__() procs.append(combine(output_base, output_dark, output_medium, output_light, output_bright, result_tmp, result)) procs.append(combine(output_noline, output_noline, output_noline, output_noline, output_noline, noline_result_tmp, noline_result)) [proc.wait() for proc in procs] # we now learn how much height was added by the lineart! # for some reason, the files aren't appearing for a moment after Inkscape quits while True: if os.path.exists(result) and os.path.exists(noline_result): break time.sleep(0.1) os.unlink(result_tmp) os.unlink(noline_result_tmp) original_xml = minidom.parse(open(result)) noline_xml = minidom.parse(open(noline_result)) original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2]) noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2]) delta = original_height - noline_height height = original_height bottom = height - (height - delta / 2) top = height - delta / 2 view["extra"] = (height - bottom) / (top - bottom) view["bottom"] = bottom / height os.unlink(noline_result) # now we add the data file_path = macrodir.joinpath("presets").joinpath(category + ".js") with open(file_path, "r", encoding="utf-8") as file: lines = file.readlines() found = False with open(file_path, "w", encoding="utf-8") as file: for line in lines: if f"/* ***{group_name}*** */" in line: found = True file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n") elif "/* ***INSERT HERE*** */" in line and not found: file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n") file.write(line) else: file.write(line)