less copy protection, more size visualization
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

170 lines
8.1 KiB

  1. import sys
  2. import re
  3. import json
  4. import os
  5. import subprocess
  6. import pathlib
  7. import time
  8. from xml.dom import minidom
  9. # an affront to god
  10. def combine(base_path, dark_path, medium_path, light_path, bright_path, tmp_output_path, output_path):
  11. base = open(base_path, "r", encoding="utf-8").read()
  12. dark = open(dark_path, "r", encoding="utf-8").read()
  13. medium = open(medium_path, "r", encoding="utf-8").read()
  14. light = open(light_path, "r", encoding="utf-8").read()
  15. bright = open(bright_path, "r", encoding="utf-8").read()
  16. base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
  17. dark_data = re.search("<g.*", dark, flags=re.DOTALL)[0][:-7]
  18. medium_data = re.search("<g.*", medium, flags=re.DOTALL)[0][:-7]
  19. light_data = re.search("<g.*", light, flags=re.DOTALL)[0][:-7]
  20. bright_data = bright.replace("</metadata>", "</metadata>" + base_data + "\n" + dark_data + "\n" + medium_data + "\n" + light_data)
  21. with open(tmp_output_path, "w", encoding="utf-8") as f:
  22. f.write(bright_data)
  23. return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg", "--export-area-drawing", "--export-filename=" + output_path, tmp_output_path], shell=False)
  24. configdir = pathlib.Path(__file__).parent
  25. configpath = configdir.joinpath("config.json")
  26. config = json.load(open(configpath, encoding="utf-8"))
  27. workdir = pathlib.Path(config["work-directory"])
  28. print(sys.argv)
  29. sourcedir = workdir.joinpath(sys.argv[1])
  30. macrodir = pathlib.Path(config["macrovision-directory"])
  31. print(sourcedir, macrodir)
  32. side_strings = []
  33. POTRACE = config["potrace"]
  34. INKSCAPE = config["inkscape"]
  35. output = {}
  36. with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file:
  37. all_data = json.load(file)
  38. group_name = all_data["name"]
  39. category = all_data["kind"]
  40. outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name)
  41. os.makedirs(outputdir, exist_ok=True)
  42. for data in all_data["forms"]:
  43. name = data["name"]
  44. for view in data["views"]:
  45. view_name = view["name"]
  46. input = sourcedir.joinpath(name + "-" + view_name + ".png").__str__()
  47. input_noline_raw = sourcedir.joinpath(name + "-" + view_name + "-" + "noline.png").__str__()
  48. result = outputdir.joinpath(name + "-" + view_name + ".svg").__str__()
  49. print(result)
  50. if os.path.exists(result):
  51. if os.path.getmtime(input) < os.path.getmtime(result):
  52. print("Skipping ", input)
  53. continue
  54. else:
  55. os.unlink(result)
  56. input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp").__str__()
  57. input_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.bmp").__str__()
  58. input_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.bmp").__str__()
  59. input_light = sourcedir.joinpath(name + "-" + view_name + "-light.bmp").__str__()
  60. input_bright = sourcedir.joinpath(name + "-" + view_name + "-bright.bmp").__str__()
  61. input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__()
  62. procs = []
  63. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RGB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-flatten", input_base], shell=False))
  64. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "GB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_dark], shell=False))
  65. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_medium], shell=False))
  66. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RG", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_light], shell=False))
  67. procs.append(subprocess.Popen(["magick", "convert", input, "-fill", "black", "-fuzz", "10%", "+opaque", "#FFFFFF", "-channel", "RGB", "-modulate", "200,0", "-background", "#FFFFFF", "-negate", "-flatten", input_bright], shell=False))
  68. # to correct for extra height from lines
  69. procs.append(subprocess.Popen(["magick", "convert", input_noline_raw, "-channel", "RGB", "-evaluate", "set", "0", "-background", "#FFFFFF", "-flatten", input_noline], shell=False))
  70. [proc.wait() for proc in procs]
  71. output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__()
  72. output_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.svg").__str__()
  73. output_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.svg").__str__()
  74. output_light = sourcedir.joinpath(name + "-" + view_name + "-light.svg").__str__()
  75. output_bright = sourcedir.joinpath(name + "-" + view_name + "-bright.svg").__str__()
  76. output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__()
  77. procs = []
  78. alpha = str(all_data["trace_alpha"])
  79. procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-a", alpha, "-o", output_base], shell=False))
  80. procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-a", alpha, "-C", "#1a1a1a", "-o", output_dark], shell=False))
  81. procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_medium], shell=False))
  82. procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-a", alpha, "-C", "#4d4d4d", "-o", output_light], shell=False))
  83. procs.append(subprocess.Popen([POTRACE, input_bright, "-b", "svg", "-a", alpha, "-C", "#666666", "-o", output_bright], shell=False))
  84. procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_noline], shell=False))
  85. [proc.wait() for proc in procs]
  86. procs = []
  87. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  88. result_tmp = outputdir.joinpath(name + "-" + view_name + "-tmp.svg").__str__()
  89. noline_result_tmp = sourcedir.joinpath(name + "-" + view_name + "-noline_processed-tmp.svg").__str__()
  90. procs.append(combine(output_base, output_dark, output_medium, output_light, output_bright, result_tmp, result))
  91. procs.append(combine(output_noline, output_noline, output_noline, output_noline, output_noline, noline_result_tmp, noline_result))
  92. [proc.wait() for proc in procs]
  93. # we now learn how much height was added by the lineart!
  94. # for some reason, the files aren't appearing for a moment after Inkscape quits
  95. while True:
  96. if os.path.exists(result) and os.path.exists(noline_result):
  97. break
  98. time.sleep(0.1)
  99. os.unlink(result_tmp)
  100. os.unlink(noline_result_tmp)
  101. original_xml = minidom.parse(open(result))
  102. noline_xml = minidom.parse(open(noline_result))
  103. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  104. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  105. delta = original_height - noline_height
  106. height = original_height
  107. bottom = height - (height - delta / 2)
  108. top = height - delta / 2
  109. view["extra"] = (height - bottom) / (top - bottom)
  110. view["bottom"] = bottom / height
  111. os.unlink(noline_result)
  112. # now we add the data
  113. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  114. with open(file_path, "r", encoding="utf-8") as file:
  115. lines = file.readlines()
  116. found = False
  117. with open(file_path, "w", encoding="utf-8") as file:
  118. for line in lines:
  119. if f"/* ***{group_name}*** */" in line:
  120. found = True
  121. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  122. elif "/* ***INSERT HERE*** */" in line and not found:
  123. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  124. file.write(line)
  125. else:
  126. file.write(line)