less copy protection, more size visualization
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

133 líneas
5.7 KiB

  1. import sys
  2. import re
  3. import json
  4. import os
  5. import subprocess
  6. import pathlib
  7. from xml.dom import minidom
  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, "--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").__str__()
  45. input_noline_raw = sourcedir.joinpath(name + "-" + view_name + "-" + "noline.png").__str__()
  46. result = outputdir.joinpath(name + "-" + view_name + ".svg").__str__()
  47. print(result)
  48. if os.path.exists(result) and os.path.getmtime(input) < os.path.getmtime(result):
  49. print("Skipping ", input)
  50. continue
  51. input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp").__str__()
  52. input_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.bmp").__str__()
  53. input_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.bmp").__str__()
  54. input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__()
  55. subprocess.run(["magick", "convert", input, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_base], shell=False)
  56. subprocess.run(["magick", "convert", input, highlight_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_highlight], shell=False)
  57. subprocess.run(["magick", "convert", input, vivid_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_vivid], shell=False)
  58. # to correct for extra height from lines
  59. subprocess.run(["magick", "convert", input_noline_raw, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_noline], shell=False)
  60. output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__()
  61. output_highlight = sourcedir.joinpath(name + "-" + view_name + "-highlight.svg").__str__()
  62. output_vivid = sourcedir.joinpath(name + "-" + view_name + "-vivid.svg").__str__()
  63. output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__()
  64. subprocess.run([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False)
  65. subprocess.run([POTRACE, input_highlight, "-b", "svg", "-C", "#1a1a1a", "-o", output_highlight], shell=False)
  66. subprocess.run([POTRACE, input_vivid, "-b", "svg", "-C", "#333333", "-o", output_vivid], shell=False)
  67. subprocess.run([POTRACE, input_noline, "-b", "svg", "-C", "#333333", "-o", output_noline], shell=False)
  68. combine(output_base, output_highlight, output_vivid, result)
  69. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  70. # we now learn how much height was added by the lineart!
  71. original_xml = minidom.parse(open(result))
  72. noline_xml = minidom.parse(open(noline_result))
  73. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  74. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  75. delta = original_height - noline_height
  76. height = original_height
  77. bottom = height - (height - delta / 2)
  78. top = height - delta / 2
  79. view["extra"] = (height - bottom) / (top - bottom)
  80. view["bottom"] = bottom / height
  81. combine(output_noline, output_noline, output_noline, noline_result)
  82. # os.unlink(input_base)
  83. # os.unlink(input_highlight)
  84. # now we add the data
  85. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  86. with open(file_path, "r", encoding="utf-8") as file:
  87. lines = file.readlines()
  88. found = False
  89. with open(file_path, "w", encoding="utf-8") as file:
  90. for line in lines:
  91. if f"/* ***{group_name}*** */" in line:
  92. found = True
  93. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  94. elif "/* ***INSERT HERE*** */" in line and not found:
  95. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  96. file.write(line)
  97. else:
  98. file.write(line)