import sys import re import json import os import subprocess import pathlib from xml.dom import minidom # an affront to god def combine(base_path, dark_path, medium_path, light_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() base_data = re.search("", "" + base_data + "\n" + dark_data + "\n" + medium_data) with open(output_path, "w", encoding="utf-8") as f: f.write(light_data) return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg=" + output_path, "--export-area-drawing", 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) base_lut = configdir.joinpath("luts").joinpath("base-lut.png").__str__() dark_lut = configdir.joinpath("luts").joinpath("dark-lut.png").__str__() medium_lut = configdir.joinpath("luts").joinpath("medium-lut.png").__str__() light_lut = configdir.joinpath("luts").joinpath("light-lut.png").__str__() 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) and os.path.getmtime(input) < os.path.getmtime(result): print("Skipping ", input) continue 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_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", "1000,0", "-background", "#FFFFFF", "-flatten", input_base], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "GB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_dark], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_medium], shell=False)) procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RG", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_light], shell=False)) # to correct for extra height from lines procs.append(subprocess.Popen(["magick", "convert", input_noline_raw, base_lut, "-channel", "RGB", "-clut", "-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_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__() procs = [] procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False)) procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-C", "#1a1a1a", "-o", output_dark], shell=False)) procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-C", "#333333", "-o", output_medium], shell=False)) procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-C", "#4d4d4d", "-o", output_light], shell=False)) procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-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__() procs.append(combine(output_base, output_dark, output_medium, output_light, result)) procs.append(combine(output_noline, output_noline, output_noline, output_noline, noline_result)) [proc.wait() for proc in procs] # we now learn how much height was added by the lineart! 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 # 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)