export_test_scenes.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import bpy
  2. import os
  3. import sys
  4. import traceback
  5. import json
  6. sys.path = [os.getcwd()] + sys.path # Ensure exporter from this folder
  7. TEST_SCENE_DIR = os.path.join(os.getcwd(), "tests/test_scenes")
  8. EXPORTED_DIR = os.path.join(os.getcwd(), "tests/godot_project/exports")
  9. def export_escn(out_file, config):
  10. """Fake the export operator call"""
  11. import io_scene_godot
  12. io_scene_godot.export(out_file, config)
  13. def main():
  14. dir_queue = list()
  15. dir_queue.append('.')
  16. while dir_queue:
  17. dir_relpath = dir_queue.pop(0)
  18. # read config file if present, otherwise use default
  19. src_dir_path = os.path.join(TEST_SCENE_DIR, dir_relpath)
  20. if os.path.exists(os.path.join(src_dir_path, "config.json")):
  21. with open(os.path.join(src_dir_path, "config.json")) as config_file:
  22. config = json.load(config_file)
  23. else:
  24. config = {}
  25. # create exported to directory
  26. exported_dir_path = os.path.join(EXPORTED_DIR, dir_relpath)
  27. if not os.path.exists(exported_dir_path):
  28. os.makedirs(exported_dir_path)
  29. for item in os.listdir(os.path.join(TEST_SCENE_DIR, dir_relpath)):
  30. item_abspath = os.path.join(TEST_SCENE_DIR, dir_relpath, item)
  31. if os.path.isdir(item_abspath):
  32. # push dir into queue for later traversal
  33. dir_queue.append(os.path.join(dir_relpath, item))
  34. elif item_abspath.endswith('blend'):
  35. # export blend file
  36. print("---------")
  37. print("Exporting {}".format(os.path.abspath(item_abspath)))
  38. bpy.ops.wm.open_mainfile(filepath=item_abspath)
  39. out_path = os.path.join(
  40. EXPORTED_DIR,
  41. dir_relpath,
  42. item.replace('.blend', '.escn')
  43. )
  44. export_escn(out_path, config)
  45. print("Exported to {}".format(os.path.abspath(out_path)))
  46. def run_with_abort(function):
  47. """Runs a function such that an abort causes blender to quit with an error
  48. code. Otherwise, even a failed script will allow the Makefile to continue
  49. running"""
  50. try:
  51. function()
  52. except:
  53. traceback.print_exc()
  54. exit(1)
  55. if __name__ == "__main__":
  56. run_with_abort(main)