blender_icons_geom_update.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. # This script updates icons from the BLEND file
  3. import os
  4. import subprocess
  5. import sys
  6. def run(cmd):
  7. print(" ", " ".join(cmd))
  8. # Don't use check_call because asan causes nonzero exitcode :S
  9. subprocess.call(cmd)
  10. def edit_text_file(filename, marker_begin, marker_end, content):
  11. with open(filename, 'r', encoding='utf-8') as f:
  12. data = f.read()
  13. marker_begin_index = data.find(marker_begin)
  14. marker_end_index = data.find(marker_end, marker_begin_index)
  15. # include indentation of marker
  16. while data[marker_end_index - 1] in {'\t', ' '}:
  17. marker_end_index -= 1
  18. if marker_begin_index == -1:
  19. print('Error: {!r} not found'.format(marker_begin))
  20. return
  21. if marker_end_index == -1:
  22. print('Error: {!r} not found'.format(marker_end))
  23. return
  24. marker_begin_index += len(marker_begin) + 1
  25. data_update = data[:marker_begin_index] + content + data[marker_end_index:]
  26. if data != data_update:
  27. with open(filename, 'w', encoding='utf-8') as f:
  28. f.write(data_update)
  29. BASEDIR = os.path.abspath(os.path.dirname(__file__))
  30. ROOTDIR = os.path.normpath(os.path.join(BASEDIR, "..", ".."))
  31. blender_bin = os.environ.get("BLENDER_BIN", "blender")
  32. if not os.path.exists(blender_bin):
  33. blender_bin = os.path.join(ROOTDIR, "blender.bin")
  34. if not os.path.exists(blender_bin):
  35. if sys.platform == 'darwin':
  36. blender_app_path = '/Applications/Blender.app/Contents/MacOS/Blender'
  37. if os.path.exists(blender_app_path):
  38. blender_bin = blender_app_path
  39. icons_blend = (
  40. os.path.join(ROOTDIR, "..", "lib", "resources", "icon_geom.blend"),
  41. )
  42. def names_and_time_from_path(path):
  43. for entry in os.scandir(path):
  44. name = entry.name
  45. if name.endswith(".dat"):
  46. yield (name, entry.stat().st_mtime)
  47. # Collect icons files and update CMake.
  48. icon_files = []
  49. # create .dat geometry (which are stored in git)
  50. for blend in icons_blend:
  51. output_dir = os.path.join(BASEDIR, "icons")
  52. files_old = set(names_and_time_from_path(output_dir))
  53. cmd = (
  54. blender_bin, "--background", "--factory-startup", "-noaudio",
  55. blend,
  56. "--python", os.path.join(BASEDIR, "blender_icons_geom.py"),
  57. "--",
  58. "--group", "Export",
  59. "--output-dir", output_dir,
  60. )
  61. run(cmd)
  62. files_new = set(names_and_time_from_path(output_dir))
  63. icon_files.extend([
  64. name[:-4] # no .dat
  65. for (name, _) in sorted((files_new - files_old))
  66. ])
  67. edit_text_file(
  68. os.path.join(ROOTDIR, "source", "blender", "editors", "datafiles", "CMakeLists.txt"),
  69. "# BEGIN ICON_GEOM_NAMES",
  70. "# END ICON_GEOM_NAMES",
  71. "\t" + "\n\t".join(icon_files) + "\n",
  72. )