bake.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. import subprocess
  3. import os
  4. import glob
  5. import sys
  6. root = os.path.dirname(sys.argv[0])
  7. platform = sys.platform
  8. SHADERS = 'shaders'
  9. TEXTURES = 'textures'
  10. RES = 2048
  11. # /// ENV SETUP
  12. os.environ['DL_SHADERS_PATH'] = os.path.join(root, SHADERS)
  13. os.environ['DL_TEXTURES_PATH'] = os.path.join(root, TEXTURES)
  14. if platform == 'linux2':
  15. print 'running linux'
  16. DELIGHT = '/usr/local/3delight-12.0.19/Linux-x86_64'
  17. #os.environ['DELIGHT'] = DELIGHT
  18. #sys.path.append(os.path.join(DELIGHT, 'bin'))
  19. elif platform == 'darwin':
  20. DELIGHT = '/Applications/3Delight'
  21. os.environ['DELIGHT'] = DELIGHT
  22. sys.path.append(os.path.join(DELIGHT, 'bin'))
  23. DELIGHT_BIN_FOLDER = os.path.join(DELIGHT, 'bin')
  24. DELIGHT_BIN = os.path.join(DELIGHT_BIN_FOLDER, 'renderdl')
  25. TDLMAKE_BIN = os.path.join(DELIGHT_BIN_FOLDER, 'tdlmake')
  26. def cleanup():
  27. for bakefile in glob.glob(os.path.join(root,'*.bake')):
  28. os.remove(bakefile)
  29. for tdl in glob.glob(os.path.join(root,'*.tdl')):
  30. os.remove(tdl)
  31. def gen_includes(heightmap, disp_factor=1):
  32. includes = []
  33. disp_bound = disp_factor * 1.05
  34. for file in glob.glob('caster/*'):
  35. includes.append(file)
  36. for file in glob.glob('receiver/*'):
  37. basename = os.path.splitext(os.path.basename(file))[0]
  38. dispsettings = '\n\tDisplacement "lm_disp" "float offset" [.01] \n\t"float Km" [{2}] "string texname" ["textures/tweaked_heightmap.tdl"]'
  39. include = 'AttributeBegin\
  40. \n\tSurface "lm_bake" "string bakefile" ["{0}"] "string texColName" [""] "string texSpecName" [""] "float Ka" [0.5] "float Kd" [0.4] "float Ks" [0.5] "float roughness" [0.1]\
  41. \n\tAttribute "displacementbound" "sphere" [{3}]\
  42. \n\tReadArchive "{1}"]\
  43. \nAttributeEnd'.format(basename, file, disp_factor, disp_bound)
  44. includes.append(include)
  45. return includes
  46. def bake_post_render(root, res=RES):
  47. for bakefile in glob.glob(os.path.join(root, '*.bake')):
  48. bake_target = os.path.splitext(bakefile)[0]
  49. # tdlmake converts bake files to tdl only
  50. cmd = [TDLMAKE_BIN, '-nomipmap', '-progress', '-bakeres', str(res) + 'x' + str(res), bakefile, bake_target + '.tdl']
  51. subprocess.call(cmd)
  52. cmd = [TDLMAKE_BIN, '-nomipmap', '-progress', bake_target + '.tdl', bake_target + '.tif']
  53. subprocess.call(cmd)
  54. for include in gen_includes('textures/tweaked_heightmap.tdl', 200):
  55. print include
  56. cleanup()
  57. subprocess.call([DELIGHT_BIN, '-res', str(RES), str(RES), '-id', '-progress', 'options.rib'])
  58. bake_post_render(root)