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.
 
 
 

149 líneas
6.8 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, dark_path, medium_path, light_path, output_path):
  10. base = open(base_path, "r", encoding="utf-8").read()
  11. dark = open(dark_path, "r", encoding="utf-8").read()
  12. medium = open(medium_path, "r", encoding="utf-8").read()
  13. light = open(light_path, "r", encoding="utf-8").read()
  14. base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
  15. dark_data = re.search("<g.*", dark, flags=re.DOTALL)[0][:-7]
  16. medium_data = re.search("<g.*", medium, flags=re.DOTALL)[0][:-7]
  17. light_data = light.replace("</metadata>", "</metadata>" + base_data + "\n" + dark_data + "\n" + medium_data)
  18. with open(output_path, "w", encoding="utf-8") as f:
  19. f.write(light_data)
  20. return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg=" + output_path, "--export-area-drawing", output_path], shell=False)
  21. configdir = pathlib.Path(__file__).parent
  22. configpath = configdir.joinpath("config.json")
  23. config = json.load(open(configpath, encoding="utf-8"))
  24. workdir = pathlib.Path(config["work-directory"])
  25. print(sys.argv)
  26. sourcedir = workdir.joinpath(sys.argv[1])
  27. macrodir = pathlib.Path(config["macrovision-directory"])
  28. print(sourcedir, macrodir)
  29. side_strings = []
  30. POTRACE = config["potrace"]
  31. INKSCAPE = config["inkscape"]
  32. output = {}
  33. with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file:
  34. all_data = json.load(file)
  35. group_name = all_data["name"]
  36. category = all_data["kind"]
  37. outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name)
  38. os.makedirs(outputdir, exist_ok=True)
  39. base_lut = configdir.joinpath("luts").joinpath("base-lut.png").__str__()
  40. dark_lut = configdir.joinpath("luts").joinpath("dark-lut.png").__str__()
  41. medium_lut = configdir.joinpath("luts").joinpath("medium-lut.png").__str__()
  42. light_lut = configdir.joinpath("luts").joinpath("light-lut.png").__str__()
  43. for data in all_data["forms"]:
  44. name = data["name"]
  45. for view in data["views"]:
  46. view_name = view["name"]
  47. input = sourcedir.joinpath(name + "-" + view_name + ".png").__str__()
  48. input_noline_raw = sourcedir.joinpath(name + "-" + view_name + "-" + "noline.png").__str__()
  49. result = outputdir.joinpath(name + "-" + view_name + ".svg").__str__()
  50. print(result)
  51. if os.path.exists(result) and os.path.getmtime(input) < os.path.getmtime(result):
  52. print("Skipping ", input)
  53. continue
  54. input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp").__str__()
  55. input_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.bmp").__str__()
  56. input_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.bmp").__str__()
  57. input_light = sourcedir.joinpath(name + "-" + view_name + "-light.bmp").__str__()
  58. input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__()
  59. procs = []
  60. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RGB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-flatten", input_base], shell=False))
  61. 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))
  62. 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))
  63. 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))
  64. # to correct for extra height from lines
  65. procs.append(subprocess.Popen(["magick", "convert", input_noline_raw, base_lut, "-channel", "RGB", "-clut", "-background", "#FFFFFF", "-flatten", input_noline], shell=False))
  66. [proc.wait() for proc in procs]
  67. output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__()
  68. output_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.svg").__str__()
  69. output_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.svg").__str__()
  70. output_light = sourcedir.joinpath(name + "-" + view_name + "-light.svg").__str__()
  71. output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__()
  72. procs = []
  73. procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False))
  74. procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-C", "#1a1a1a", "-o", output_dark], shell=False))
  75. procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-C", "#333333", "-o", output_medium], shell=False))
  76. procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-C", "#4d4d4d", "-o", output_light], shell=False))
  77. procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-C", "#333333", "-o", output_noline], shell=False))
  78. [proc.wait() for proc in procs]
  79. procs = []
  80. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  81. procs.append(combine(output_base, output_dark, output_medium, output_light, result))
  82. procs.append(combine(output_noline, output_noline, output_noline, output_noline, noline_result))
  83. [proc.wait() for proc in procs]
  84. # we now learn how much height was added by the lineart!
  85. original_xml = minidom.parse(open(result))
  86. noline_xml = minidom.parse(open(noline_result))
  87. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  88. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  89. delta = original_height - noline_height
  90. height = original_height
  91. bottom = height - (height - delta / 2)
  92. top = height - delta / 2
  93. view["extra"] = (height - bottom) / (top - bottom)
  94. view["bottom"] = bottom / height
  95. # now we add the data
  96. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  97. with open(file_path, "r", encoding="utf-8") as file:
  98. lines = file.readlines()
  99. found = False
  100. with open(file_path, "w", encoding="utf-8") as file:
  101. for line in lines:
  102. if f"/* ***{group_name}*** */" in line:
  103. found = True
  104. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  105. elif "/* ***INSERT HERE*** */" in line and not found:
  106. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  107. file.write(line)
  108. else:
  109. file.write(line)