bake.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 = 8192
  11. os.environ['DL_SHADERS_PATH'] = os.path.join(root, SHADERS)
  12. os.environ['DL_TEXTURES_PATH'] = os.path.join(root, TEXTURES)
  13. if platform == 'linux2':
  14. print 'running linux'
  15. DELIGHT = '/usr/local/3delight-12.0.19/Linux-x86_64'
  16. os.environ['DELIGHT'] = DELIGHT
  17. sys.path.append(os.path.join(DELIGHT, 'bin'))
  18. elif platform == 'darwin':
  19. os.environ['DELIGHT'] = '/Applications/3Delight'
  20. def cleanup():
  21. for bakefile in glob.glob(os.path.join(root,'*.bake')):
  22. os.remove(bakefile)
  23. for tdl in glob.glob(os.path.join(root,'*.tdl')):
  24. os.remove(tdl)
  25. def gen_includes(heightmap, disp_factor=1):
  26. includes = []
  27. disp_bound = disp_factor * 1.05
  28. for file in glob.glob('caster/*'):
  29. includes.append(file)
  30. for file in glob.glob('receiver/*'):
  31. basename = os.path.splitext(os.path.basename(file))[0]
  32. include = 'AttributeBegin\
  33. \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]\
  34. \n\tAttribute "displacementbound" "sphere" [{3}]\
  35. \n\tDisplacement "lm_disp" "float offset" [.01]\
  36. \n\t"float Km" [{2}] "string texname" ["textures/tweaked_heightmap.tdl"]\
  37. \n\tReadArchive "{1}"]\
  38. \nAttributeEnd'.format(basename, file, disp_factor, disp_bound)
  39. includes.append(include)
  40. return includes
  41. def bake_post_render(root, res=RES):
  42. for bakefile in glob.glob(os.path.join(root, '*.bake')):
  43. bake_target = os.path.splitext(bakefile)[0]
  44. # tdlmake converts bake files to tdl only
  45. cmd = ['tdlmake', '-nomipmap', '-progress', '-bakeres', str(res) + 'x' + str(res), bakefile, bake_target + '.tdl']
  46. subprocess.call(cmd)
  47. cmd = ['tdlmake', '-nomipmap', '-progress', bake_target + '.tdl', bake_target + '.tif']
  48. subprocess.call(cmd)
  49. for include in gen_includes('textures/tweaked_heightmap.tdl', 200):
  50. print include
  51. cleanup()
  52. #subprocess.call(['renderdl', '-res', str(RES), str(RES), '-id', '-progress', 'options.rib'])
  53. subprocess.call(['renderdl', '-res', str(RES), str(RES), '-id','-progress', 'options.rib'])
  54. bake_post_render(root)