less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

103 line
4.2 KiB

  1. import sys
  2. import re
  3. import json
  4. import glob
  5. import os
  6. import subprocess
  7. import pathlib
  8. # an affront to god
  9. def combine(base_path, highlight_path, vivid_path, output_path):
  10. base = open(base_path, "r", encoding="utf-8").read()
  11. highlight = open(highlight_path, "r", encoding="utf-8").read()
  12. vivid = open(vivid_path, "r", encoding="utf-8").read()
  13. base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
  14. highlight_data = re.search("<g.*", highlight, flags=re.DOTALL)[0][:-7]
  15. vivid_data = vivid.replace("</metadata>", "</metadata>" + base_data + "\n" + highlight_data)
  16. with open(output_path, "w", encoding="utf-8") as f:
  17. f.write(vivid_data)
  18. subprocess.run([INKSCAPE, "--without-gui", "--export-plain-svg=" + output_path.resolve().__str__(), "--export-area-drawing", output_path], shell=False)
  19. configdir = pathlib.Path(__file__).parent
  20. configpath = configdir.joinpath("config.json")
  21. config = json.load(open(configpath, encoding="utf-8"))
  22. workdir = pathlib.Path(config["work-directory"])
  23. print(sys.argv)
  24. sourcedir = workdir.joinpath(sys.argv[1])
  25. macrodir = pathlib.Path(config["macrovision-directory"])
  26. print(sourcedir, macrodir)
  27. side_strings = []
  28. POTRACE = config["potrace"]
  29. INKSCAPE = config["inkscape"]
  30. output = {}
  31. with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file:
  32. all_data = json.load(file)
  33. group_name = all_data["name"]
  34. category = all_data["kind"]
  35. outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name)
  36. os.makedirs(outputdir, exist_ok=True)
  37. base_lut = configdir.joinpath("luts").joinpath("base-lut.png").__str__()
  38. highlight_lut = configdir.joinpath("luts").joinpath("highlight-lut.png").__str__()
  39. vivid_lut = configdir.joinpath("luts").joinpath("vivid-lut.png").__str__()
  40. for data in all_data["forms"]:
  41. name = data["name"]
  42. for view in data["views"]:
  43. view_name = view["name"]
  44. input = sourcedir.joinpath(name + "-" + view_name + ".png")
  45. result = outputdir.joinpath(name + "-" + view_name + ".svg")
  46. print(result)
  47. if os.path.exists(result) and os.path.getmtime(input) < os.path.getmtime(result):
  48. print("Skipping ", input)
  49. continue
  50. input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp")
  51. input_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.bmp")
  52. input_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.bmp")
  53. subprocess.run(["magick", "convert", input, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_base], shell=False)
  54. subprocess.run(["magick", "convert", input, highlight_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_highlight], shell=False)
  55. subprocess.run(["magick", "convert", input, vivid_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_vivid], shell=False)
  56. output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg")
  57. output_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.svg")
  58. output_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.svg")
  59. subprocess.run([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False)
  60. subprocess.run([POTRACE, input_highlight, "-b", "svg", "-C", "#1a1a1a", "-o", output_highlight], shell=False)
  61. subprocess.run([POTRACE, input_vivid, "-b", "svg", "-C", "#333333", "-o", output_vivid], shell=False)
  62. combine(output_base, output_highlight, output_vivid, result)
  63. # os.unlink(input_base)
  64. # os.unlink(input_highlight)
  65. # now we add the data
  66. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  67. with open(file_path, "r", encoding="utf-8") as file:
  68. lines = file.readlines()
  69. found = False
  70. with open(file_path, "w", encoding="utf-8") as file:
  71. for line in lines:
  72. if f"/* ***{group_name}*** */" in line:
  73. found = True
  74. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  75. elif "/* ***INSERT HERE*** */" in line and not found:
  76. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  77. file.write(line)
  78. else:
  79. file.write(line)