PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

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