2 # -*- coding: utf-8 -*-
5 Defines objects creating the *ACES* configuration.
16 # TODO: This restores the capability of running the script without having
17 # added the package to PYTHONPATH, this is ugly and should ideally replaced by
18 # dedicated executable in a /bin directory.
19 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
21 import PyOpenColorIO as ocio
23 import aces_ocio.create_arri_colorspaces as arri
24 import aces_ocio.create_canon_colorspaces as canon
25 import aces_ocio.create_red_colorspaces as red
26 import aces_ocio.create_sony_colorspaces as sony
27 from aces_ocio.generate_lut import (
28 generate_1d_LUT_from_CTL,
29 generate_3d_LUT_from_CTL,
31 from aces_ocio.process import Process
32 from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize_path
34 __author__ = 'ACES Developers'
35 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
37 __maintainer__ = 'ACES Developers'
38 __email__ = 'aces@oscars.org'
39 __status__ = 'Production'
41 __all__ = ['ACES_OCIO_CTL_DIRECTORY_ENVIRON',
42 'ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON',
43 'set_config_default_roles',
45 'generate_OCIO_transform',
48 'generate_baked_LUTs',
56 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
57 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
60 def set_config_default_roles(config,
71 Sets given *OCIO* configuration default roles.
77 color_picking : str or unicode
78 Color picking role title.
79 color_timing : str or unicode
80 Color timing role title.
81 compositing_log : str or unicode
82 Compositing log role title.
85 default : str or unicode
87 matte_paint : str or unicode
88 Matte painting role title.
89 reference : str or unicode
91 scene_linear : str or unicode
92 Scene linear role title.
93 texture_paint : str or unicode
94 Texture painting role title.
103 config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
105 config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
107 config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
109 config.setRole(ocio.Constants.ROLE_DATA, data)
111 config.setRole(ocio.Constants.ROLE_DEFAULT, default)
113 config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
115 config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
117 config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
119 config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
124 def write_config(config, config_path, sanity_check=True):
126 Writes the configuration to given path.
131 Parameter description.
136 Return value description.
144 print 'Configuration was not written due to a failed Sanity Check'
148 file_handle = open(config_path, mode='w')
149 file_handle.write(config.serialize())
153 def generate_OCIO_transform(transforms):
160 Parameter description.
165 Return value description.
168 # print('Generating transforms')
170 interpolation_options = {
171 'linear': ocio.Constants.INTERP_LINEAR,
172 'nearest': ocio.Constants.INTERP_NEAREST,
173 'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL
175 direction_options = {
176 'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
177 'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE
182 for transform in transforms:
183 if transform['type'] == 'lutFile':
184 ocio_transform = ocio.FileTransform(
185 src=transform['path'],
186 interpolation=interpolation_options[
187 transform['interpolation']],
188 direction=direction_options[transform['direction']])
189 ocio_transforms.append(ocio_transform)
190 elif transform['type'] == 'matrix':
191 ocio_transform = ocio.MatrixTransform()
192 # MatrixTransform member variables can't be initialized directly.
193 # Each must be set individually.
194 ocio_transform.setMatrix(transform['matrix'])
196 if 'offset' in transform:
197 ocio_transform.setOffset(transform['offset'])
199 if 'direction' in transform:
200 ocio_transform.setDirection(
201 direction_options[transform['direction']])
203 ocio_transforms.append(ocio_transform)
204 elif transform['type'] == 'exponent':
205 ocio_transform = ocio.ExponentTransform()
206 ocio_transform.setValue(transform['value'])
207 ocio_transforms.append(ocio_transform)
208 elif transform['type'] == 'log':
209 ocio_transform = ocio.LogTransform(
210 base=transform['base'],
211 direction=direction_options[transform['direction']])
213 ocio_transforms.append(ocio_transform)
215 print('Ignoring unknown transform type : %s' % transform['type'])
217 # Build a group transform if necessary
218 if len(ocio_transforms) > 1:
219 transform_G = ocio.GroupTransform()
220 for transform in ocio_transforms:
221 transform_G.push_back(transform)
222 transform = transform_G
224 # Or take the first transform from the list
226 transform = ocio_transforms[0]
231 def create_config(config_data, nuke=False):
238 Parameter description.
243 Return value description.
247 config = ocio.Config()
250 # Set config wide values
252 config.setDescription('An ACES config generated from python')
253 config.setSearchPath('luts')
256 # Define the reference color space
258 reference_data = config_data['referenceColorSpace']
259 print('Adding the reference color space : %s' % reference_data.name)
261 # Create a color space
262 reference = ocio.ColorSpace(
263 name=reference_data.name,
264 bitDepth=reference_data.bit_depth,
265 description=reference_data.description,
266 equalityGroup=reference_data.equality_group,
267 family=reference_data.family,
268 isData=reference_data.is_data,
269 allocation=reference_data.allocation_type,
270 allocationVars=reference_data.allocation_vars)
273 config.addColorSpace(reference)
276 # Create the rest of the color spaces
278 for colorspace in sorted(config_data['colorSpaces']):
279 print('Creating new color space : %s' % colorspace.name)
281 ocio_colorspace = ocio.ColorSpace(
282 name=colorspace.name,
283 bitDepth=colorspace.bit_depth,
284 description=colorspace.description,
285 equalityGroup=colorspace.equality_group,
286 family=colorspace.family,
287 isData=colorspace.is_data,
288 allocation=colorspace.allocation_type,
289 allocationVars=colorspace.allocation_vars)
291 if colorspace.to_reference_transforms != []:
292 print('Generating To-Reference transforms')
293 ocio_transform = generate_OCIO_transform(
294 colorspace.to_reference_transforms)
295 ocio_colorspace.setTransform(
297 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
299 if colorspace.from_reference_transforms != []:
300 print('Generating From-Reference transforms')
301 ocio_transform = generate_OCIO_transform(
302 colorspace.from_reference_transforms)
303 ocio_colorspace.setTransform(
305 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
307 config.addColorSpace(ocio_colorspace)
312 # Define the views and displays
317 # Generic display and view setup
319 for display, view_list in config_data['displays'].iteritems():
320 for view_name, colorspace in view_list.iteritems():
321 config.addDisplay(display, view_name, colorspace.name)
322 if not (view_name in views):
323 views.append(view_name)
324 displays.append(display)
325 # A Nuke specific set of views and displays
328 # A few names: Output Transform, ACES, ACEScc, are hard-coded here.
329 # Would be better to automate.
332 for display, view_list in config_data['displays'].iteritems():
333 for view_name, colorspace in view_list.iteritems():
334 if (view_name == 'Output Transform'):
336 config.addDisplay(display, view_name, colorspace.name)
337 if not (view_name in views):
338 views.append(view_name)
339 displays.append(display)
341 config.addDisplay('linear', 'View', 'ACES2065-1')
342 displays.append('linear')
343 config.addDisplay('log', 'View', 'ACEScc')
344 displays.append('log')
346 # Set active displays and views
347 config.setActiveDisplays(','.join(sorted(displays)))
348 config.setActiveViews(','.join(views))
351 # Need to generalize this at some point
355 set_config_default_roles(
357 color_picking=reference.getName(),
358 color_timing=reference.getName(),
359 compositing_log=reference.getName(),
360 data=reference.getName(),
361 default=reference.getName(),
362 matte_paint=reference.getName(),
363 reference=reference.getName(),
364 scene_linear=reference.getName(),
365 texture_paint=reference.getName())
367 # Check to make sure we didn't screw something up
373 def generate_LUTs(odt_info,
378 lut_resolution_1d=4096,
379 lut_resolution_3d=64,
387 Parameter description.
392 Colorspaces and transforms converting between those colorspaces and
393 the reference colorspace, *ACES*.
396 print('generateLUTs - begin')
400 # Define the reference color space
402 ACES = ColorSpace('ACES2065-1')
404 'The Academy Color Encoding System reference color space')
405 ACES.equality_group = ''
408 ACES.allocation_type = ocio.Constants.ALLOCATION_LG2
409 ACES.allocation_vars = [-15, 6]
411 config_data['referenceColorSpace'] = ACES
414 # Define the displays
416 config_data['displays'] = {}
419 # Define the other color spaces
421 config_data['colorSpaces'] = []
423 # Matrix converting ACES AP1 primaries to AP0
424 ACES_AP1_to_AP0 = [0.6954522414, 0.1406786965, 0.1638690622,
425 0.0447945634, 0.8596711185, 0.0955343182,
426 -0.0055258826, 0.0040252103, 1.0015006723]
428 # Matrix converting ACES AP0 primaries to XYZ
429 ACES_AP0_to_XYZ = [0.9525523959, 0.0000000000, 0.0000936786,
430 0.3439664498, 0.7281660966, -0.0721325464,
431 0.0000000000, 0.0000000000, 1.0088251844]
436 def create_ACEScc(name='ACEScc',
440 cs = ColorSpace(name)
441 cs.description = 'The %s color space' % name
442 cs.equality_group = ''
446 ctls = [os.path.join(aces_CTL_directory,
448 'ACEScsc.ACEScc_to_ACES.a1.0.0.ctl'),
449 # This transform gets back to the AP1 primaries
450 # Useful as the 1d LUT is only covering the transfer function
451 # The primaries switch is covered by the matrix below
452 os.path.join(aces_CTL_directory,
454 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
455 lut = '%s_to_ACES.spi1d' % name
457 lut = sanitize_path(lut)
459 generate_1d_LUT_from_CTL(
460 os.path.join(lut_directory, lut),
472 cs.to_reference_transforms = []
473 cs.to_reference_transforms.append({
476 'interpolation': 'linear',
477 'direction': 'forward'
480 # AP1 primaries to AP0 primaries
481 cs.to_reference_transforms.append({
483 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
484 'direction': 'forward'
487 cs.from_reference_transforms = []
490 ACEScc = create_ACEScc()
491 config_data['colorSpaces'].append(ACEScc)
496 def create_ACESproxy(name='ACESproxy'):
497 cs = ColorSpace(name)
498 cs.description = 'The %s color space' % name
499 cs.equality_group = ''
504 os.path.join(aces_CTL_directory,
506 'ACEScsc.ACESproxy10i_to_ACES.a1.0.0.ctl'),
507 # This transform gets back to the AP1 primaries
508 # Useful as the 1d LUT is only covering the transfer function
509 # The primaries switch is covered by the matrix below
510 os.path.join(aces_CTL_directory,
512 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
513 lut = '%s_to_aces.spi1d' % name
515 lut = sanitize_path(lut)
517 generate_1d_LUT_from_CTL(
518 os.path.join(lut_directory, lut),
528 cs.to_reference_transforms = []
529 cs.to_reference_transforms.append({
532 'interpolation': 'linear',
533 'direction': 'forward'
536 # AP1 primaries to AP0 primaries
537 cs.to_reference_transforms.append({
539 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
540 'direction': 'forward'
543 cs.from_reference_transforms = []
546 ACESproxy = create_ACESproxy()
547 config_data['colorSpaces'].append(ACESproxy)
552 def create_ACEScg(name='ACEScg'):
553 cs = ColorSpace(name)
554 cs.description = 'The %s color space' % name
555 cs.equality_group = ''
559 cs.to_reference_transforms = []
561 # AP1 primaries to AP0 primaries
562 cs.to_reference_transforms.append({
564 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
565 'direction': 'forward'
568 cs.from_reference_transforms = []
571 ACEScg = create_ACEScg()
572 config_data['colorSpaces'].append(ACEScg)
577 def create_ADX(bit_depth=10, name='ADX'):
578 name = '%s%s' % (name, bit_depth)
579 cs = ColorSpace(name)
580 cs.description = '%s color space - used for film scans' % name
581 cs.equality_group = ''
586 cs.bit_depth = bit_depth = ocio.Constants.BIT_DEPTH_UINT10
587 adx_to_cdd = [1023.0 / 500.0, 0.0, 0.0, 0.0,
588 0.0, 1023.0 / 500.0, 0.0, 0.0,
589 0.0, 0.0, 1023.0 / 500.0, 0.0,
591 offset = [-95.0 / 500.0, -95.0 / 500.0, -95.0 / 500.0, 0.0]
592 elif bit_depth == 16:
593 cs.bit_depth = bit_depth = ocio.Constants.BIT_DEPTH_UINT16
594 adx_to_cdd = [65535.0 / 8000.0, 0.0, 0.0, 0.0,
595 0.0, 65535.0 / 8000.0, 0.0, 0.0,
596 0.0, 0.0, 65535.0 / 8000.0, 0.0,
598 offset = [-1520.0 / 8000.0, -1520.0 / 8000.0, -1520.0 / 8000.0,
601 cs.to_reference_transforms = []
603 # Convert from ADX to Channel-Dependent Density
604 cs.to_reference_transforms.append({
606 'matrix': adx_to_cdd,
608 'direction': 'forward'
611 # Convert from Channel-Dependent Density to Channel-Independent Density
612 cs.to_reference_transforms.append({
614 'matrix': [0.75573, 0.22197, 0.02230, 0,
615 0.05901, 0.96928, -0.02829, 0,
616 0.16134, 0.07406, 0.76460, 0,
618 'direction': 'forward'
621 # Copied from Alex Fry's adx_cid_to_rle.py
622 def create_CID_to_RLE_LUT():
623 def interpolate_1D(x, xp, fp):
624 return numpy.interp(x, xp, fp)
626 LUT_1D_xp = [-0.190000000000000,
638 LUT_1D_fp = [-6.000000000000000,
650 REF_PT = ((7120.0 - 1520.0) / 8000.0 * (100.0 / 55.0) -
651 math.log(0.18, 10.0))
655 return interpolate_1D(x, LUT_1D_xp, LUT_1D_fp)
656 return (100.0 / 55.0) * x - REF_PT
658 def fit(value, from_min, from_max, to_min, to_max):
659 if from_min == from_max:
660 raise ValueError('from_min == from_max')
661 return (value - from_min) / (from_max - from_min) * (
662 to_max - to_min) + to_min
664 NUM_SAMPLES = 2 ** 12
667 for i in xrange(NUM_SAMPLES):
668 x = i / (NUM_SAMPLES - 1.0)
669 x = fit(x, 0.0, 1.0, RANGE[0], RANGE[1])
670 data.append(cid_to_rle(x))
672 lut = 'ADX_CID_to_RLE.spi1d'
673 write_SPI_1d(os.path.join(lut_directory, lut),
681 # Convert Channel Independent Density values to Relative Log Exposure
683 lut = create_CID_to_RLE_LUT()
684 cs.to_reference_transforms.append({
687 'interpolation': 'linear',
688 'direction': 'forward'
691 # Convert Relative Log Exposure values to Relative Exposure values
692 cs.to_reference_transforms.append({
695 'direction': 'inverse'
698 # Convert Relative Exposure values to ACES values
699 cs.to_reference_transforms.append({
701 'matrix': [0.72286, 0.12630, 0.15084, 0,
702 0.11923, 0.76418, 0.11659, 0,
703 0.01427, 0.08213, 0.90359, 0,
705 'direction': 'forward'
708 cs.from_reference_transforms = []
711 ADX10 = create_ADX(bit_depth=10)
712 config_data['colorSpaces'].append(ADX10)
714 ADX16 = create_ADX(bit_depth=16)
715 config_data['colorSpaces'].append(ADX16)
718 # Camera Input Transforms
721 # RED color spaces to ACES
722 red_colorspaces = red.create_colorspaces(lut_directory, lut_resolution_1d)
723 for cs in red_colorspaces:
724 config_data['colorSpaces'].append(cs)
727 canon_colorspaces = canon.create_colorspaces(lut_directory,
729 for cs in canon_colorspaces:
730 config_data['colorSpaces'].append(cs)
733 sony_colorSpaces = sony.create_colorspaces(lut_directory,
735 for cs in sony_colorSpaces:
736 config_data['colorSpaces'].append(cs)
739 arri_colorSpaces = arri.create_colorspaces(lut_directory,
741 for cs in arri_colorSpaces:
742 config_data['colorSpaces'].append(cs)
745 # Generic log transform
747 def create_generic_log(name='log',
754 lut_resolution_1d=lut_resolution_1d):
755 cs = ColorSpace(name)
756 cs.description = 'The %s color space' % name
757 cs.equality_group = name
758 cs.family = 'Utility'
762 os.path.join(aces_CTL_directory,
764 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl')]
765 lut = '%s_to_aces.spi1d' % name
767 lut = sanitize_path(lut)
769 generate_1d_LUT_from_CTL(
770 os.path.join(lut_directory, lut),
777 'middleGrey': middle_grey,
778 'minExposure': min_exposure,
779 'maxExposure': max_exposure
786 cs.to_reference_transforms = []
787 cs.to_reference_transforms.append({
790 'interpolation': 'linear',
791 'direction': 'forward'
794 cs.from_reference_transforms = []
800 def create_ACES_LMT(lmt_name,
803 lut_resolution_1d=1024,
804 lut_resolution_3d=64,
806 cs = ColorSpace('%s' % lmt_name)
807 cs.description = 'The ACES Look Transform: %s' % lmt_name
808 cs.equality_group = ''
812 pprint.pprint(lmt_values)
815 # Generate the shaper transform
819 shaper_from_ACES_CTL,
821 shaper_params) = shaper_info
823 shaper_lut = '%s_to_aces.spi1d' % shaper_name
824 if (not os.path.exists(os.path.join(lut_directory, shaper_lut))):
825 ctls = [shaper_to_ACES_CTL % aces_CTL_directory]
827 # Remove spaces and parentheses
828 shaper_lut = shaper_lut.replace(
829 ' ', '_').replace(')', '_').replace('(', '_')
831 generate_1d_LUT_from_CTL(
832 os.path.join(lut_directory, shaper_lut),
836 1.0 / shaper_input_scale,
842 shaper_OCIO_transform = {
845 'interpolation': 'linear',
846 'direction': 'inverse'
850 # Generate the forward transform
852 cs.from_reference_transforms = []
854 if 'transformCTL' in lmt_values:
856 shaper_to_ACES_CTL % aces_CTL_directory,
857 os.path.join(aces_CTL_directory, lmt_values['transformCTL'])]
858 lut = '%s.%s.spi3d' % (shaper_name, lmt_name)
860 lut = sanitize_path(lut)
862 generate_3d_LUT_from_CTL(
863 os.path.join(lut_directory, lut),
867 1.0 / shaper_input_scale,
873 cs.from_reference_transforms.append(shaper_OCIO_transform)
874 cs.from_reference_transforms.append({
877 'interpolation': 'tetrahedral',
878 'direction': 'forward'
882 # Generate the inverse transform
884 cs.to_reference_transforms = []
886 if 'transformCTLInverse' in lmt_values:
888 os.path.join(aces_CTL_directory,
889 odt_values['transformCTLInverse']),
890 shaper_from_ACES_CTL % aces_CTL_directory
892 lut = 'Inverse.%s.%s.spi3d' % (odt_name, shaper_name)
894 lut = sanitize_path(lut)
896 generate_3d_LUT_from_CTL(
897 os.path.join(lut_directory, lut),
907 cs.to_reference_transforms.append({
910 'interpolation': 'tetrahedral',
911 'direction': 'forward'
914 shaper_inverse = shaper_OCIO_transform.copy()
915 shaper_inverse['direction'] = 'forward'
916 cs.to_reference_transforms.append(shaper_inverse)
924 lmt_lut_resolution_1d = max(4096, lut_resolution_1d)
925 lmt_lut_resolution_3d = max(65, lut_resolution_3d)
928 lmt_shaper_name = 'LMT Shaper'
931 'minExposure': -10.0,
934 lmt_shaper = create_generic_log(name=lmt_shaper_name,
935 middle_grey=lmt_params['middleGrey'],
936 min_exposure=lmt_params['minExposure'],
937 max_exposure=lmt_params['maxExposure'],
938 lut_resolution_1d=lmt_lut_resolution_1d)
939 config_data['colorSpaces'].append(lmt_shaper)
941 shaper_input_scale_generic_log2 = 1.0
943 # Log 2 shaper name and CTL transforms bundled up
948 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl'),
951 'ACESlib.OCIO_shaper_lin_to_log2_param.a1.0.0.ctl'),
952 shaper_input_scale_generic_log2,
955 sorted_LMTs = sorted(lmt_info.iteritems(), key=lambda x: x[1])
957 for lmt in sorted_LMTs:
958 (lmt_name, lmt_values) = lmt
959 cs = create_ACES_LMT(
960 lmt_values['transformUserName'],
963 lmt_lut_resolution_1d,
964 lmt_lut_resolution_3d,
966 config_data['colorSpaces'].append(cs)
969 # ACES RRT with the supplied ODT
971 def create_ACES_RRT_plus_ODT(odt_name,
974 lut_resolution_1d=1024,
975 lut_resolution_3d=64,
977 cs = ColorSpace('%s' % odt_name)
978 cs.description = '%s - %s Output Transform' % (
979 odt_values['transformUserNamePrefix'], odt_name)
980 cs.equality_group = ''
984 pprint.pprint(odt_values)
987 # Generate the shaper transform
989 # if 'shaperCTL' in odtValues:
992 shaper_from_ACES_CTL,
994 shaper_params) = shaper_info
996 if 'legalRange' in odt_values:
997 shaper_params['legalRange'] = odt_values['legalRange']
999 shaper_params['legalRange'] = 0
1001 shaper_lut = '%s_to_aces.spi1d' % shaper_name
1002 if (not os.path.exists(os.path.join(lut_directory, shaper_lut))):
1003 ctls = [shaper_to_ACES_CTL % aces_CTL_directory]
1005 # Remove spaces and parentheses
1006 shaper_lut = shaper_lut.replace(
1007 ' ', '_').replace(')', '_').replace('(', '_')
1009 generate_1d_LUT_from_CTL(
1010 os.path.join(lut_directory, shaper_lut),
1014 1.0 / shaper_input_scale,
1020 shaper_OCIO_transform = {
1023 'interpolation': 'linear',
1024 'direction': 'inverse'
1028 # Generate the forward transform
1030 cs.from_reference_transforms = []
1032 if 'transformLUT' in odt_values:
1033 # Copy into the lut dir
1034 transform_LUT_file_name = os.path.basename(
1035 odt_values['transformLUT'])
1036 lut = os.path.join(lut_directory, transform_LUT_file_name)
1037 shutil.copy(odt_values['transformLUT'], lut)
1039 cs.from_reference_transforms.append(shaper_OCIO_transform)
1040 cs.from_reference_transforms.append({
1042 'path': transform_LUT_file_name,
1043 'interpolation': 'tetrahedral',
1044 'direction': 'forward'
1046 elif 'transformCTL' in odt_values:
1050 shaper_to_ACES_CTL % aces_CTL_directory,
1051 os.path.join(aces_CTL_directory,
1054 os.path.join(aces_CTL_directory,
1056 odt_values['transformCTL'])]
1057 lut = '%s.RRT.a1.0.0.%s.spi3d' % (shaper_name, odt_name)
1059 lut = sanitize_path(lut)
1061 generate_3d_LUT_from_CTL(
1062 os.path.join(lut_directory, lut),
1067 1.0 / shaper_input_scale,
1073 cs.from_reference_transforms.append(shaper_OCIO_transform)
1074 cs.from_reference_transforms.append({
1077 'interpolation': 'tetrahedral',
1078 'direction': 'forward'
1082 # Generate the inverse transform
1084 cs.to_reference_transforms = []
1086 if 'transformLUTInverse' in odt_values:
1087 # Copy into the lut dir
1088 transform_LUT_inverse_file_name = os.path.basename(
1089 odt_values['transformLUTInverse'])
1090 lut = os.path.join(lut_directory, transform_LUT_inverse_file_name)
1091 shutil.copy(odt_values['transformLUTInverse'], lut)
1093 cs.to_reference_transforms.append({
1095 'path': transform_LUT_inverse_file_name,
1096 'interpolation': 'tetrahedral',
1097 'direction': 'forward'
1100 shaper_inverse = shaper_OCIO_transform.copy()
1101 shaper_inverse['direction'] = 'forward'
1102 cs.to_reference_transforms.append(shaper_inverse)
1103 elif 'transformCTLInverse' in odt_values:
1105 os.path.join(aces_CTL_directory,
1107 odt_values['transformCTLInverse']),
1108 os.path.join(aces_CTL_directory,
1110 'InvRRT.a1.0.0.ctl'),
1111 shaper_from_ACES_CTL % aces_CTL_directory
1113 lut = 'InvRRT.a1.0.0.%s.%s.spi3d' % (odt_name, shaper_name)
1115 lut = sanitize_path(lut)
1117 generate_3d_LUT_from_CTL(
1118 os.path.join(lut_directory, lut),
1129 cs.to_reference_transforms.append({
1132 'interpolation': 'tetrahedral',
1133 'direction': 'forward'
1136 shaper_inverse = shaper_OCIO_transform.copy()
1137 shaper_inverse['direction'] = 'forward'
1138 cs.to_reference_transforms.append(shaper_inverse)
1143 # RRT/ODT shaper options
1148 log2_shaper_name = shaper_name
1151 'minExposure': -6.0,
1154 log2_shaper = create_generic_log(
1155 name=log2_shaper_name,
1156 middle_grey=log2_params['middleGrey'],
1157 min_exposure=log2_params['minExposure'],
1158 max_exposure=log2_params['maxExposure'])
1159 config_data['colorSpaces'].append(log2_shaper)
1161 shaper_input_scale_generic_log2 = 1.0
1163 # Log 2 shaper name and CTL transforms bundled up
1164 log2_shaper_data = [
1168 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl'),
1171 'ACESlib.OCIO_shaper_lin_to_log2_param.a1.0.0.ctl'),
1172 shaper_input_scale_generic_log2,
1175 shaper_data[log2_shaper_name] = log2_shaper_data
1178 # Shaper that also includes the AP1 primaries
1179 # - Needed for some LUT baking steps
1181 log2_shaper_AP1 = create_generic_log(
1182 name=log2_shaper_name,
1183 middle_grey=log2_params['middleGrey'],
1184 min_exposure=log2_params['minExposure'],
1185 max_exposure=log2_params['maxExposure'])
1186 log2_shaper_AP1.name = '%s - AP1' % log2_shaper_AP1.name
1187 # AP1 primaries to AP0 primaries
1188 log2_shaper_AP1.to_reference_transforms.append({
1190 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
1191 'direction': 'forward'
1193 config_data['colorSpaces'].append(log2_shaper_AP1)
1196 # Choose your shaper
1198 rrt_shaper_name = log2_shaper_name
1199 rrt_shaper = log2_shaper_data
1202 # RRT + ODT Combinations
1204 sorted_odts = sorted(odt_info.iteritems(), key=lambda x: x[1])
1206 for odt in sorted_odts:
1207 (odt_name, odt_values) = odt
1209 # Have to handle ODTs that can generate either legal or full output
1210 if odt_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1211 'Academy.Rec709_100nits_dim.a1.0.0',
1212 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1213 odt_name_legal = '%s - Legal' % odt_values['transformUserName']
1215 odt_name_legal = odt_values['transformUserName']
1217 odt_legal = odt_values.copy()
1218 odt_legal['legalRange'] = 1
1220 cs = create_ACES_RRT_plus_ODT(
1227 config_data['colorSpaces'].append(cs)
1229 # Create a display entry using this color space
1230 config_data['displays'][odt_name_legal] = {
1233 'Output Transform': cs}
1235 if odt_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1236 'Academy.Rec709_100nits_dim.a1.0.0',
1237 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1238 print('Generating full range ODT for %s' % odt_name)
1240 odt_name_full = '%s - Full' % odt_values['transformUserName']
1241 odt_full = odt_values.copy()
1242 odt_full['legalRange'] = 0
1244 cs_full = create_ACES_RRT_plus_ODT(
1251 config_data['colorSpaces'].append(cs_full)
1253 # Create a display entry using this color space
1254 config_data['displays'][odt_name_full] = {
1257 'Output Transform': cs_full}
1260 # Generic Matrix transform
1262 def create_generic_matrix(name='matrix',
1263 from_reference_values=[],
1264 to_reference_values=[]):
1265 cs = ColorSpace(name)
1266 cs.description = 'The %s color space' % name
1267 cs.equality_group = name
1268 cs.family = 'Utility'
1271 cs.to_reference_transforms = []
1272 if to_reference_values != []:
1273 for matrix in to_reference_values:
1274 cs.to_reference_transforms.append({
1276 'matrix': mat44_from_mat33(matrix),
1277 'direction': 'forward'
1280 cs.from_reference_transforms = []
1281 if from_reference_values != []:
1282 for matrix in from_reference_values:
1283 cs.from_reference_transforms.append({
1285 'matrix': mat44_from_mat33(matrix),
1286 'direction': 'forward'
1291 cs = create_generic_matrix('XYZ', from_reference_values=[ACES_AP0_to_XYZ])
1292 config_data['colorSpaces'].append(cs)
1294 cs = create_generic_matrix(
1295 'Linear - AP1', to_reference_values=[ACES_AP1_to_AP0])
1296 config_data['colorSpaces'].append(cs)
1298 # ACES to Linear, P3D60 primaries
1299 XYZ_to_P3D60 = [2.4027414142, -0.8974841639, -0.3880533700,
1300 -0.8325796487, 1.7692317536, 0.0237127115,
1301 0.0388233815, -0.0824996856, 1.0363685997]
1303 cs = create_generic_matrix(
1305 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_P3D60])
1306 config_data['colorSpaces'].append(cs)
1308 # ACES to Linear, P3D60 primaries
1309 XYZ_to_P3DCI = [2.7253940305, -1.0180030062, -0.4401631952,
1310 -0.7951680258, 1.6897320548, 0.0226471906,
1311 0.0412418914, -0.0876390192, 1.1009293786]
1313 cs = create_generic_matrix(
1315 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_P3DCI])
1316 config_data['colorSpaces'].append(cs)
1318 # ACES to Linear, Rec 709 primaries
1319 XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
1320 -0.9692436363, 1.8759675015, 0.0415550574,
1321 0.0556300797, -0.2039769589, 1.0569715142]
1323 cs = create_generic_matrix(
1325 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_Rec709])
1326 config_data['colorSpaces'].append(cs)
1328 # ACES to Linear, Rec 2020 primaries
1329 XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
1330 -0.6666843518, 1.6164812366, 0.0157685458,
1331 0.0176398574, -0.0427706133, 0.9421031212]
1333 cs = create_generic_matrix(
1334 'Linear - Rec.2020',
1335 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_Rec2020])
1336 config_data['colorSpaces'].append(cs)
1338 print('generateLUTs - end')
1342 def generate_baked_LUTs(odt_info,
1348 lut_resolution_shaper=1024):
1355 Parameter description.
1360 Return value description.
1363 # Add the legal and full variations into this list
1364 odt_info_C = dict(odt_info)
1365 for odt_CTL_name, odt_values in odt_info.iteritems():
1366 if odt_CTL_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1367 'Academy.Rec709_100nits_dim.a1.0.0',
1368 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1369 odt_name = odt_values['transformUserName']
1371 odt_values_legal = dict(odt_values)
1372 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
1373 odt_info_C['%s - Legal' % odt_CTL_name] = odt_values_legal
1375 odt_values_full = dict(odt_values)
1376 odt_values_full['transformUserName'] = '%s - Full' % odt_name
1377 odt_info_C['%s - Full' % odt_CTL_name] = odt_values_full
1379 del (odt_info_C[odt_CTL_name])
1381 for odt_CTL_name, odt_values in odt_info_C.iteritems():
1382 odt_prefix = odt_values['transformUserNamePrefix']
1383 odt_name = odt_values['transformUserName']
1386 for input_space in ['ACEScc', 'ACESproxy']:
1387 args = ['--iconfig', config_path,
1389 '--inputspace', input_space]
1390 args += ['--outputspace', '%s' % odt_name]
1391 args += ['--description',
1392 '%s - %s for %s data' % (odt_prefix,
1395 args += ['--shaperspace', shaper_name,
1396 '--shapersize', str(lut_resolution_shaper)]
1397 args += ['--cubesize', str(lut_resolution_3d)]
1398 args += ['--format',
1400 os.path.join(baked_directory,
1402 '%s for %s.icc' % (odt_name, input_space))]
1404 bake_LUT = Process(description='bake a LUT',
1410 for input_space in ['ACEScc', 'ACESproxy']:
1411 args = ['--iconfig', config_path,
1413 '--inputspace', input_space]
1414 args += ['--outputspace', '%s' % odt_name]
1415 args += ['--description',
1416 '%s - %s for %s data' % (
1417 odt_prefix, odt_name, input_space)]
1418 args += ['--shaperspace', shaper_name,
1419 '--shapersize', str(lut_resolution_shaper)]
1420 args += ['--cubesize', str(lut_resolution_3d)]
1422 fargs = ['--format',
1427 '%s for %s Flame.3dl' % (odt_name, input_space))]
1428 bake_LUT = Process(description='bake a LUT',
1430 args=(args + fargs))
1433 largs = ['--format',
1438 '%s for %s Lustre.3dl' % (odt_name, input_space))]
1439 bake_LUT = Process(description='bake a LUT',
1441 args=(args + largs))
1445 for input_space in ['ACEScg', 'ACES2065-1']:
1446 args = ['--iconfig', config_path,
1448 '--inputspace', input_space]
1449 args += ['--outputspace', '%s' % odt_name]
1450 args += ['--description',
1451 '%s - %s for %s data' % (
1452 odt_prefix, odt_name, input_space)]
1453 if input_space == 'ACEScg':
1454 lin_shaper_name = '%s - AP1' % shaper_name
1456 lin_shaper_name = shaper_name
1457 args += ['--shaperspace', lin_shaper_name,
1458 '--shapersize', str(lut_resolution_shaper)]
1460 args += ['--cubesize', str(lut_resolution_3d)]
1462 margs = ['--format',
1467 '%s for %s Maya.csp' % (odt_name, input_space))]
1468 bake_LUT = Process(description='bake a LUT',
1470 args=(args + margs))
1473 hargs = ['--format',
1478 '%s for %s Houdini.lut' % (odt_name, input_space))]
1479 bake_LUT = Process(description='bake a LUT',
1481 args=(args + hargs))
1485 def create_config_dir(config_directory, bake_secondary_LUTs):
1492 Parameter description.
1497 Return value description.
1500 dirs = [config_directory, os.path.join(config_directory, 'luts')]
1501 if bake_secondary_LUTs:
1502 dirs.extend([os.path.join(config_directory, 'baked'),
1503 os.path.join(config_directory, 'baked', 'flame'),
1504 os.path.join(config_directory, 'baked', 'photoshop'),
1505 os.path.join(config_directory, 'baked', 'houdini'),
1506 os.path.join(config_directory, 'baked', 'lustre'),
1507 os.path.join(config_directory, 'baked', 'maya')])
1510 not os.path.exists(d) and os.mkdir(d)
1513 def get_transform_info(ctl_transform):
1520 Parameter description.
1525 Return value description.
1528 # TODO: Use *with* statement.
1529 fp = open(ctl_transform, 'rb')
1532 lines = fp.readlines()
1534 # Grab transform ID and User Name
1535 transform_ID = lines[1][3:].split('<')[1].split('>')[1].strip()
1536 # print(transformID)
1537 transform_user_name = '-'.join(
1538 lines[2][3:].split('<')[1].split('>')[1].split('-')[1:]).strip()
1539 transform_user_name_prefix = (
1540 lines[2][3:].split('<')[1].split('>')[1].split('-')[0].strip())
1541 # print(transformUserName)
1544 return transform_ID, transform_user_name, transform_user_name_prefix
1547 def get_ODT_info(aces_CTL_directory):
1551 For versions after WGR9.
1556 Parameter description.
1561 Return value description.
1564 # TODO: Investigate usage of *files_walker* definition here.
1565 # Credit to Alex Fry for the original approach here
1566 odt_dir = os.path.join(aces_CTL_directory, 'odt')
1568 for dir_name, subdir_list, file_list in os.walk(odt_dir):
1569 for fname in file_list:
1570 all_odt.append((os.path.join(dir_name, fname)))
1572 odt_CTLs = [x for x in all_odt if
1573 ('InvODT' not in x) and (os.path.split(x)[-1][0] != '.')]
1579 for odt_CTL in odt_CTLs:
1580 odt_tokens = os.path.split(odt_CTL)
1583 # Handle nested directories
1584 odt_path_tokens = os.path.split(odt_tokens[-2])
1585 odt_dir = odt_path_tokens[-1]
1586 while odt_path_tokens[-2][-3:] != 'odt':
1587 odt_path_tokens = os.path.split(odt_path_tokens[-2])
1588 odt_dir = os.path.join(odt_path_tokens[-1], odt_dir)
1591 # print('odtDir : %s' % odtDir)
1592 transform_CTL = odt_tokens[-1]
1593 # print(transformCTL)
1594 odt_name = string.join(transform_CTL.split('.')[1:-1], '.')
1597 # Find id, user name and user name prefix
1599 transform_user_name,
1600 transform_user_name_prefix) = get_transform_info(
1601 os.path.join(aces_CTL_directory, 'odt', odt_dir, transform_CTL))
1604 transform_CTL_inverse = 'InvODT.%s.ctl' % odt_name
1605 if not os.path.exists(
1606 os.path.join(odt_tokens[-2], transform_CTL_inverse)):
1607 transform_CTL_inverse = None
1608 # print(transformCTLInverse)
1610 # Add to list of ODTs
1612 odts[odt_name]['transformCTL'] = os.path.join(odt_dir, transform_CTL)
1613 if transform_CTL_inverse != None:
1614 odts[odt_name]['transformCTLInverse'] = os.path.join(
1615 odt_dir, transform_CTL_inverse)
1617 odts[odt_name]['transformID'] = transform_ID
1618 odts[odt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1619 odts[odt_name]['transformUserName'] = transform_user_name
1621 print('ODT : %s' % odt_name)
1622 print('\tTransform ID : %s' % transform_ID)
1623 print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1624 print('\tTransform User Name : %s' % transform_user_name)
1625 print('\tForward ctl : %s' % (
1626 odts[odt_name]['transformCTL']))
1627 if 'transformCTLInverse' in odts[odt_name]:
1628 print('\tInverse ctl : %s' % (
1629 odts[odt_name]['transformCTLInverse']))
1631 print('\tInverse ctl : %s' % 'None')
1638 def get_LMT_info(aces_CTL_directory):
1642 For versions after WGR9.
1647 Parameter description.
1652 Return value description.
1655 # TODO: Investigate refactoring with previous definition.
1657 # Credit to Alex Fry for the original approach here
1658 lmt_dir = os.path.join(aces_CTL_directory, 'lmt')
1660 for dir_name, subdir_list, file_list in os.walk(lmt_dir):
1661 for fname in file_list:
1662 all_lmt.append((os.path.join(dir_name, fname)))
1664 lmt_CTLs = [x for x in all_lmt if
1665 ('InvLMT' not in x) and ('README' not in x) and (
1666 os.path.split(x)[-1][0] != '.')]
1672 for lmt_CTL in lmt_CTLs:
1673 lmt_tokens = os.path.split(lmt_CTL)
1676 # Handle nested directories
1677 lmt_path_tokens = os.path.split(lmt_tokens[-2])
1678 lmt_dir = lmt_path_tokens[-1]
1679 while lmt_path_tokens[-2][-3:] != 'ctl':
1680 lmt_path_tokens = os.path.split(lmt_path_tokens[-2])
1681 lmt_dir = os.path.join(lmt_path_tokens[-1], lmt_dir)
1684 # print('lmtDir : %s' % lmtDir)
1685 transform_CTL = lmt_tokens[-1]
1686 # print(transformCTL)
1687 lmt_name = string.join(transform_CTL.split('.')[1:-1], '.')
1690 # Find id, user name and user name prefix
1692 transform_user_name,
1693 transform_user_name_prefix) = get_transform_info(
1694 os.path.join(aces_CTL_directory, lmt_dir, transform_CTL))
1697 transform_CTL_inverse = 'InvLMT.%s.ctl' % lmt_name
1698 if not os.path.exists(
1699 os.path.join(lmt_tokens[-2], transform_CTL_inverse)):
1700 transform_CTL_inverse = None
1701 # print(transformCTLInverse)
1703 # Add to list of LMTs
1705 lmts[lmt_name]['transformCTL'] = os.path.join(lmt_dir, transform_CTL)
1706 if transform_CTL_inverse != None:
1707 # TODO: Check unresolved *odt_name* referemce.
1708 lmts[odt_name]['transformCTLInverse'] = os.path.join(
1709 lmt_dir, transform_CTL_inverse)
1711 lmts[lmt_name]['transformID'] = transform_ID
1712 lmts[lmt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1713 lmts[lmt_name]['transformUserName'] = transform_user_name
1715 print('LMT : %s' % lmt_name)
1716 print('\tTransform ID : %s' % transform_ID)
1717 print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1718 print('\tTransform User Name : %s' % transform_user_name)
1719 print('\t Forward ctl : %s' % lmts[lmt_name]['transformCTL'])
1720 if 'transformCTLInverse' in lmts[lmt_name]:
1721 print('\t Inverse ctl : %s' % (
1722 lmts[lmt_name]['transformCTLInverse']))
1724 print('\t Inverse ctl : %s' % 'None')
1731 def create_ACES_config(aces_CTL_directory,
1733 lut_resolution_1d=4096,
1734 lut_resolution_3d=64,
1735 bake_secondary_LUTs=True,
1738 Creates the ACES configuration.
1743 Parameter description.
1748 Return value description.
1751 # Get ODT names and CTL paths
1752 odt_info = get_ODT_info(aces_CTL_directory)
1754 # Get ODT names and CTL paths
1755 lmt_info = get_LMT_info(aces_CTL_directory)
1758 create_config_dir(config_directory, bake_secondary_LUTs)
1760 # Generate config data and LUTs for different transforms
1761 lut_directory = os.path.join(config_directory, 'luts')
1762 shaper_name = 'Output Shaper'
1763 config_data = generate_LUTs(odt_info,
1772 # Create the config using the generated LUTs
1773 print('Creating generic config')
1774 config = create_config(config_data)
1777 # Write the config to disk
1778 write_config(config,
1779 os.path.join(config_directory, 'config.ocio'))
1781 # Create a config that will work well with Nuke using the previously
1783 print('Creating Nuke-specific config')
1784 nuke_config = create_config(config_data, nuke=True)
1787 # Write the config to disk
1788 write_config(nuke_config,
1789 os.path.join(config_directory, 'nuke_config.ocio'))
1791 # Bake secondary LUTs using the config
1792 if bake_secondary_LUTs:
1793 generate_baked_LUTs(odt_info,
1795 os.path.join(config_directory, 'baked'),
1796 os.path.join(config_directory, 'config.ocio'),
1811 Parameter description.
1816 Return value description.
1821 p = optparse.OptionParser(description='An OCIO config generation script',
1822 prog='createACESConfig',
1823 version='createACESConfig 0.1',
1824 usage='%prog [options]')
1825 p.add_option('--acesCTLDir', '-a', default=os.environ.get(
1826 'ACES_OCIO_CTL_DIRECTORY', None))
1827 p.add_option('--configDir', '-c', default=os.environ.get(
1828 'ACES_OCIO_CONFIGURATION_DIRECTORY', None))
1829 p.add_option('--lutResolution1d', default=4096)
1830 p.add_option('--lutResolution3d', default=64)
1831 p.add_option('--dontBakeSecondaryLUTs', action='store_true')
1832 p.add_option('--keepTempImages', action='store_true')
1834 options, arguments = p.parse_args()
1839 aces_CTL_directory = options.acesCTLDir
1840 config_directory = options.configDir
1841 lut_resolution_1d = int(options.lutResolution1d)
1842 lut_resolution_3d = int(options.lutResolution3d)
1843 bake_secondary_LUTs = not (options.dontBakeSecondaryLUTs)
1844 cleanup_temp_images = not (options.keepTempImages)
1847 args_start = sys.argv.index('--') + 1
1848 args = sys.argv[args_start:]
1850 args_start = len(sys.argv) + 1
1853 print('command line : \n%s\n' % ' '.join(sys.argv))
1855 # TODO: Use assertion and mention environment variables.
1856 if not aces_CTL_directory:
1857 print('process: No ACES CTL directory specified')
1859 if not config_directory:
1860 print('process: No configuration directory specified')
1863 # Generate the configuration
1865 return create_ACES_config(aces_CTL_directory,
1869 bake_secondary_LUTs,
1870 cleanup_temp_images)
1873 if __name__ == '__main__':