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.
 
 
 

164 line
7.3 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, 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. base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
  16. dark_data = re.search("<g.*", dark, flags=re.DOTALL)[0][:-7]
  17. medium_data = re.search("<g.*", medium, flags=re.DOTALL)[0][:-7]
  18. light_data = light.replace("</metadata>", "</metadata>" + base_data + "\n" + dark_data + "\n" + medium_data)
  19. with open(tmp_output_path, "w", encoding="utf-8") as f:
  20. f.write(light_data)
  21. return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg", "--export-area-drawing", "--export-filename=" + output_path, tmp_output_path], shell=False)
  22. configdir = pathlib.Path(__file__).parent
  23. configpath = configdir.joinpath("config.json")
  24. config = json.load(open(configpath, encoding="utf-8"))
  25. workdir = pathlib.Path(config["work-directory"])
  26. print(sys.argv)
  27. sourcedir = workdir.joinpath(sys.argv[1])
  28. macrodir = pathlib.Path(config["macrovision-directory"])
  29. print(sourcedir, macrodir)
  30. side_strings = []
  31. POTRACE = config["potrace"]
  32. INKSCAPE = config["inkscape"]
  33. output = {}
  34. with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file:
  35. all_data = json.load(file)
  36. group_name = all_data["name"]
  37. category = all_data["kind"]
  38. outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name)
  39. os.makedirs(outputdir, exist_ok=True)
  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):
  49. if os.path.getmtime(input) < os.path.getmtime(result):
  50. print("Skipping ", input)
  51. continue
  52. else:
  53. os.unlink(result)
  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, "-channel", "RGB", "-evaluate", "set", "0", "-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. alpha = str(all_data["trace_alpha"])
  74. procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-a", alpha, "-o", output_base], shell=False))
  75. procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-a", alpha, "-C", "#1a1a1a", "-o", output_dark], shell=False))
  76. procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_medium], shell=False))
  77. procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-a", alpha, "-C", "#4d4d4d", "-o", output_light], shell=False))
  78. procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_noline], shell=False))
  79. [proc.wait() for proc in procs]
  80. procs = []
  81. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  82. result_tmp = outputdir.joinpath(name + "-" + view_name + "-tmp.svg").__str__()
  83. noline_result_tmp = sourcedir.joinpath(name + "-" + view_name + "-noline_processed-tmp.svg").__str__()
  84. procs.append(combine(output_base, output_dark, output_medium, output_light, result_tmp, result))
  85. procs.append(combine(output_noline, output_noline, output_noline, output_noline, noline_result_tmp, noline_result))
  86. [proc.wait() for proc in procs]
  87. # we now learn how much height was added by the lineart!
  88. # for some reason, the files aren't appearing for a moment after Inkscape quits
  89. while True:
  90. if os.path.exists(result) and os.path.exists(noline_result):
  91. break
  92. time.sleep(0.1)
  93. os.unlink(result_tmp)
  94. os.unlink(noline_result_tmp)
  95. original_xml = minidom.parse(open(result))
  96. noline_xml = minidom.parse(open(noline_result))
  97. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  98. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  99. delta = original_height - noline_height
  100. height = original_height
  101. bottom = height - (height - delta / 2)
  102. top = height - delta / 2
  103. view["extra"] = (height - bottom) / (top - bottom)
  104. view["bottom"] = bottom / height
  105. os.unlink(noline_result)
  106. # now we add the data
  107. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  108. with open(file_path, "r", encoding="utf-8") as file:
  109. lines = file.readlines()
  110. found = False
  111. with open(file_path, "w", encoding="utf-8") as file:
  112. for line in lines:
  113. if f"/* ***{group_name}*** */" in line:
  114. found = True
  115. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  116. elif "/* ***INSERT HERE*** */" in line and not found:
  117. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  118. file.write(line)
  119. else:
  120. file.write(line)