/src/mari/ociofiletransform.py

http://github.com/imageworks/OpenColorIO · Python · 93 lines · 65 code · 0 blank · 28 comment · 0 complexity · e2501002d34e398691035915784c2408 MD5 · raw file

  1. """
  2. This script allows the loading of a specied lut (1d/3d/mtx) in
  3. the Mari Viewer. Requires Mari 1.3v2+.
  4. This example is not represntative of the final Mari OCIO workflow, merely
  5. an API demonstration. This code is a work in progress, to demonstrate the
  6. integration of OpenColorIO and Mari. The APIs this code relies on are subject
  7. to change at any time, and as such should not be relied on for production use
  8. (yet).
  9. LINUX testing instructions:
  10. * Build OCIO
  11. mkdir -p dist_mari
  12. mkdir -p build_mari && cd build_mari
  13. cmake -D CMAKE_BUILD_TYPE=Release \
  14. -D CMAKE_INSTALL_PREFIX=../dist_mari \
  15. -D PYTHON=/usr/bin/python2.6 \
  16. -D OCIO_NAMESPACE=OpenColorIO_Mari \
  17. ../
  18. make install -j8
  19. * Edit this file to point to use viewer lut you want to use
  20. * Run Mari with OpenColorIO added to the LD_LIBRARY_PATH, and Python
  21. env LD_LIBRARY_PATH=<YOURDIR>/dist_mari/lib/ PYTHONPATH=<YOURDIR>/dist_mari/lib/python2.6 mari
  22. * Source this script in the python console.
  23. Also - IMPORTANT - you must enable 'Use Color Correction' in the Color Manager.
  24. """
  25. # YOU MUST CHANGE THIS TO MODIFY WHICH LUT TO USE
  26. LUT_FILENAME = "/shots/spi/home/lib/lut/dev/v29/luts/LC3DL_Kodak2383_Sony_GDM.3dl"
  27. LUT3D_SIZE = 32
  28. import mari, time, os
  29. try:
  30. import PyOpenColorIO as OCIO
  31. print OCIO.__file__
  32. except Exception,e:
  33. print "Error: Could not import OpenColorIO python bindings."
  34. print "Please confirm PYTHONPATH has dir containing PyOpenColorIO.so"
  35. def RegisterOCIOLut():
  36. if not hasattr(mari.gl_render,"createPostFilter"):
  37. print "Error: This version of Mari does not support the mari.gl_render.createPostFilter API"
  38. return
  39. config = OCIO.Config()
  40. transform = OCIO.FileTransform(src = os.path.realpath(LUT_FILENAME),
  41. interpolation = OCIO.Constants.INTERP_LINEAR,
  42. direction = OCIO.Constants.TRANSFORM_DIR_FORWARD)
  43. processor = config.getProcessor(transform)
  44. shaderDesc = dict( [('language', OCIO.Constants.GPU_LANGUAGE_GLSL_1_3),
  45. ('functionName', 'display_ocio_$ID_'),
  46. ('lut3DEdgeLen', LUT3D_SIZE)] )
  47. shaderText = processor.getGpuShaderText(shaderDesc)
  48. lut3d = processor.getGpuLut3D(shaderDesc)
  49. # Clear the existing color managment filter stack
  50. mari.gl_render.clearPostFilterStack()
  51. # Create variable pre-declarations
  52. desc = "sampler3D lut3d_ocio_$ID_;\n"
  53. desc += shaderText
  54. # Set the body of the filter
  55. body = "{ Out = display_ocio_$ID_(Out, lut3d_ocio_$ID_); }"
  56. # HACK: Increment a counter by 1 each time to force a refresh
  57. if not hasattr(mari, "ocio_lut_counter_hack"):
  58. mari.ocio_lut_counter_hack = 0
  59. else:
  60. mari.ocio_lut_counter_hack += 1
  61. ocio_lut_counter_hack = mari.ocio_lut_counter_hack
  62. # Create a new filter
  63. name = "OCIO %s v%d" % (os.path.basename(LUT_FILENAME), ocio_lut_counter_hack)
  64. postfilter = mari.gl_render.createPostFilter(name, desc, body)
  65. # Set the texture to use for the given sampler on this filter
  66. postfilter.setTexture3D("lut3d_ocio_$ID_",
  67. LUT3D_SIZE, LUT3D_SIZE, LUT3D_SIZE,
  68. postfilter.FORMAT_RGB, lut3d)
  69. # Append the filter to the end of the current list of filters
  70. mari.gl_render.appendPostFilter(postfilter)
  71. RegisterOCIOLut()