review.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import json
  3. import stat
  4. import shutil
  5. import argparse
  6. os.chdir(os.path.dirname(os.path.realpath(__file__)))
  7. os.chdir('..')
  8. review = os.path.join(os.getcwd(), 'review')
  9. MASK = stat.S_IRWXU|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH
  10. HTML = '''<!doctype html>
  11. <html lang='en'>
  12. <head>
  13. <title>%(title)s</title>
  14. <meta charset='utf-8'>
  15. <script src='../../../../../../build/three.min.js'></script>
  16. <script src='../../../../../../examples/js/controls/OrbitControls.js'></script>
  17. <script src='../../scripts/js/review.js'></script>
  18. <link href='../../scripts/css/style.css' rel='stylesheet' />
  19. </head>
  20. <body>
  21. <script>
  22. init('%(filename)s');
  23. </script>
  24. </body>
  25. </html>
  26. '''
  27. def parse_args():
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument('json')
  30. parser.add_argument('-t', '--tag', required=True)
  31. return vars(parser.parse_args())
  32. def copy_for_review(tmp_json, tag):
  33. tag_dir = os.path.join(review, tag)
  34. if not os.path.exists(tag_dir):
  35. print('making %s' % tag_dir)
  36. os.makedirs(tag_dir)
  37. dst_json = os.path.join(tag_dir, '%s.json' % tag)
  38. print('moving %s > %s' % (tmp_json, dst_json))
  39. shutil.move(tmp_json, dst_json)
  40. create_template(tag_dir, os.path.basename(dst_json))
  41. print('looking for maps...')
  42. with open(dst_json) as stream:
  43. data = json.load(stream)
  44. textures = []
  45. materials = data.get('materials')
  46. if data['metadata']['type'] == 'Geometry' and materials:
  47. textures.extend(_parse_geometry_materials(materials))
  48. images = data.get('images')
  49. if data['metadata']['type'] == 'Object' and images:
  50. for each in images:
  51. textures.append(each['url'])
  52. textures = list(set(textures))
  53. print('found %d maps' % len(textures))
  54. dir_tmp = os.path.dirname(tmp_json)
  55. for texture in textures:
  56. texture = os.path.join(dir_tmp, texture)
  57. dst = os.path.join(tag_dir, os.path.basename(texture))
  58. shutil.move(texture, dst)
  59. print('moving %s > %s' % (texture, dst))
  60. if data['metadata']['type'] == 'Object':
  61. print('looking for non-embedded geometry')
  62. for geometry in data['geometries']:
  63. url = geometry.get('url')
  64. if not url: continue
  65. src = os.path.join(dir_tmp, url)
  66. dst = os.path.join(tag_dir, url)
  67. print('moving %s > %s' % (src, dst))
  68. shutil.move(src, dst)
  69. elif data['metadata']['type'] == 'Geometry':
  70. print('looking for external animation files')
  71. for key in ('animation', 'morphTargets'):
  72. try:
  73. value = data[key]
  74. except KeyError:
  75. continue
  76. if not isinstance(value, str):
  77. continue
  78. src = os.path.join(dir_tmp, value)
  79. dst = os.path.join(tag_dir, value)
  80. print('moving %s > %s' % (src, dst))
  81. shutil.move(src, dst)
  82. def _parse_geometry_materials(materials):
  83. maps = ('mapDiffuse', 'mapSpecular', 'mapBump',
  84. 'mapLight', 'mapNormal')
  85. textures = []
  86. for material in materials:
  87. for key in material.keys():
  88. if key in maps:
  89. textures.append(material[key])
  90. return textures
  91. def create_template(tag_dir, filename):
  92. html = HTML % {
  93. 'title': filename[:-5].title(),
  94. 'filename': filename
  95. }
  96. html_path = os.path.join(tag_dir, 'index.html')
  97. with open(html_path, 'w') as stream:
  98. stream.write(html)
  99. os.chmod(html_path, MASK)
  100. def main():
  101. args = parse_args()
  102. copy_for_review(args['json'], args['tag'])
  103. if __name__ == '__main__':
  104. main()