|
- 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, highlight_path, vivid_path, output_path):
- base = open(base_path, "r", encoding="utf-8").read()
- highlight = open(highlight_path, "r", encoding="utf-8").read()
- vivid = open(vivid_path, "r", encoding="utf-8").read()
-
- base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
- highlight_data = re.search("<g.*", highlight, flags=re.DOTALL)[0][:-7]
- vivid_data = vivid.replace("</metadata>", "</metadata>" + base_data + "\n" + highlight_data)
-
- with open(output_path, "w", encoding="utf-8") as f:
- f.write(vivid_data)
-
- subprocess.run([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__()
- highlight_lut = configdir.joinpath("luts").joinpath("highlight-lut.png").__str__()
- vivid_lut = configdir.joinpath("luts").joinpath("vivid-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_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.bmp").__str__()
- input_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.bmp").__str__()
- input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__()
-
- subprocess.run(["magick", "convert", input, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_base], shell=False)
- subprocess.run(["magick", "convert", input, highlight_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_highlight], shell=False)
- subprocess.run(["magick", "convert", input, vivid_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_vivid], shell=False)
-
- # to correct for extra height from lines
- subprocess.run(["magick", "convert", input_noline_raw, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_noline], shell=False)
-
- output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__()
- output_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.svg").__str__()
- output_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.svg").__str__()
- output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__()
-
- subprocess.run([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False)
- subprocess.run([POTRACE, input_highlight, "-b", "svg", "-C", "#1a1a1a", "-o", output_highlight], shell=False)
- subprocess.run([POTRACE, input_vivid, "-b", "svg", "-C", "#333333", "-o", output_vivid], shell=False)
- subprocess.run([POTRACE, input_noline, "-b", "svg", "-C", "#333333", "-o", output_noline], shell=False)
-
- combine(output_base, output_highlight, output_vivid, result)
-
- noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
-
- # 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
-
- combine(output_noline, output_noline, output_noline, noline_result)
- # os.unlink(input_base)
- # os.unlink(input_highlight)
-
- # 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)
|