reformat_pbrt.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # main imports
  2. import sys, os, argparse
  3. import json
  4. # default file
  5. PBRT_FILENAME = 'scene.pbrt'
  6. TUNGSTEN_FILENAME = 'scene.json'
  7. FILM_PBRT_LINE = 'Film "image" "integer xresolution" [ {0} ] "integer yresolution" [ {1} ] "string filename" [ "{2}" ]'
  8. CAMERA_PBRT_LINE = 'Camera "perspective" "float fov" [ {0} ]'
  9. SAMPLER_PBRT_LINE = 'Sampler "random" "integer pixelsamples" [ 64 ]'
  10. INTE_PBRT_LINE = 'Integrator "path" "integer maxdepth" [ 65 ]'
  11. def main():
  12. parser = argparse.ArgumentParser(description="Convert .pbrt scene as expected")
  13. parser.add_argument('--tungsten', type=str, help='tungsten scene name', required=True)
  14. parser.add_argument('--pbrt', type=str, help='pbrt scene name (this one to convert)', required=True)
  15. parser.add_argument('--outfile', type=str, help='outfile name and path', required=True)
  16. parser.add_argument('--resolution', type=str, help='image resolution expected', default='1000,1000', required=True)
  17. parser.add_argument('--image_name', type=str, help='image output name expected', required=True)
  18. args = parser.parse_args()
  19. p_tungsten = args.tungsten
  20. p_pbrt = args.pbrt
  21. p_outfile = args.outfile
  22. p_resolution = list(map(int, args.resolution.split(',')))
  23. p_image_name = args.image_name
  24. # Get expected information from tungsten file
  25. tungsten_file = open(p_tungsten, 'r')
  26. tungsten_data = json.load(tungsten_file)
  27. # read and keep information
  28. tungsten_scene_lookat = tungsten_data['camera']['transform']
  29. lookat_position = tungsten_scene_lookat['position']
  30. lookat_target = tungsten_scene_lookat['look_at']
  31. lookat_up = tungsten_scene_lookat['up']
  32. tungsten_scene_fov = tungsten_data['camera']['fov']
  33. tungsten_file.close()
  34. # create empty file and append necessary information about pbrt scene
  35. #if not os.path.exists(p_outfile):
  36. # os.makedirs(p_outfile)
  37. pbrt_outfile = open(p_outfile, 'w')
  38. # read existing pbrt file
  39. pbrt_file = open(p_pbrt, 'r')
  40. pbrt_lines = pbrt_file.readlines()
  41. for line in pbrt_lines:
  42. # default append line
  43. output_line = line
  44. # 1. Comment Transform and Camera command
  45. if line.startswith('Transform') or line.startswith('Camera'):
  46. output_line = '#' + output_line + '\n'
  47. # 2. Update image information (using resolution and extension)
  48. if line.startswith('Film'):
  49. x_resolution, y_resolution = p_resolution
  50. output_line = FILM_PBRT_LINE.format(x_resolution, y_resolution, p_image_name) + '\n'
  51. # 3. Replace expected Sampler to random
  52. if line.startswith('Sampler'):
  53. output_line = SAMPLER_PBRT_LINE + '\n'
  54. # 4. Replace expected Integrator to path
  55. if line.startswith('Integrator'):
  56. output_line = INTE_PBRT_LINE + '\n'
  57. # 5. Update LookAt and Camera information using specifc fov from tungsten
  58. if line.startswith('WorldBegin'):
  59. lookat_line = 'LookAt '
  60. lookat_line = lookat_line + str(lookat_position[0]) + ' ' + str(lookat_position[1]) + ' ' + str(lookat_position[2]) + '\n'
  61. lookat_line = lookat_line + '\t\t' + str(lookat_target[0]) + ' ' + str(lookat_target[1]) + ' ' + str(lookat_target[2]) + '\n'
  62. lookat_line = lookat_line + '\t\t' + str(lookat_up[0]) + ' ' + str(lookat_up[1]) + ' ' + str(lookat_up[2]) + '\n'
  63. camera_line = CAMERA_PBRT_LINE.format(tungsten_scene_fov) + '\n'
  64. output_line = lookat_line + camera_line + '\n' + 'WorldBegin\n'
  65. pbrt_outfile.write(output_line)
  66. # close all buffers
  67. pbrt_outfile.close()
  68. pbrt_file.close()
  69. if __name__== "__main__":
  70. main()