2 # -*- coding: utf-8 -*-
5 Defines objects creating the *ACES* configuration.
16 import PyOpenColorIO as ocio
18 import aces_ocio.create_arri_colorspaces as arri
19 import aces_ocio.create_canon_colorspaces as canon
20 import aces_ocio.create_red_colorspaces as red
21 import aces_ocio.create_sony_colorspaces as sony
22 from aces_ocio.generate_lut import (
23 generate_1d_LUT_from_CTL,
24 generate_3d_LUT_from_CTL,
26 from aces_ocio.process import Process
27 from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize_path
29 __author__ = 'ACES Developers'
30 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
32 __maintainer__ = 'ACES Developers'
33 __email__ = 'aces@oscars.org'
34 __status__ = 'Production'
36 __all__ = ['ACES_OCIO_CTL_DIRECTORY_ENVIRON',
37 'ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON',
38 'set_config_default_roles',
40 'generate_OCIO_transform',
43 'generate_baked_LUTs',
51 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
52 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
55 def set_config_default_roles(config,
66 Sets given *OCIO* configuration default roles.
72 color_picking : str or unicode
73 Color picking role title.
74 color_timing : str or unicode
75 Color timing role title.
76 compositing_log : str or unicode
77 Compositing log role title.
80 default : str or unicode
82 matte_paint : str or unicode
83 Matte painting role title.
84 reference : str or unicode
86 scene_linear : str or unicode
87 Scene linear role title.
88 texture_paint : str or unicode
89 Texture painting role title.
98 config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
100 config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
102 config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
104 config.setRole(ocio.Constants.ROLE_DATA, data)
106 config.setRole(ocio.Constants.ROLE_DEFAULT, default)
108 config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
110 config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
112 config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
114 config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
119 def write_config(config, config_path, sanity_check=True):
121 Writes the configuration to given path.
126 Parameter description.
131 Return value description.
139 print 'Configuration was not written due to a failed Sanity Check'
143 with open(config_path, mode='w') as fp:
144 fp.write(config.serialize())
147 def generate_OCIO_transform(transforms):
154 Parameter description.
159 Return value description.
162 # print('Generating transforms')
164 interpolation_options = {
165 'linear': ocio.Constants.INTERP_LINEAR,
166 'nearest': ocio.Constants.INTERP_NEAREST,
167 'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL
169 direction_options = {
170 'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
171 'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE
176 for transform in transforms:
177 if transform['type'] == 'lutFile':
178 ocio_transform = ocio.FileTransform(
179 src=transform['path'],
180 interpolation=interpolation_options[
181 transform['interpolation']],
182 direction=direction_options[transform['direction']])
183 ocio_transforms.append(ocio_transform)
184 elif transform['type'] == 'matrix':
185 ocio_transform = ocio.MatrixTransform()
186 # MatrixTransform member variables can't be initialized directly.
187 # Each must be set individually.
188 ocio_transform.setMatrix(transform['matrix'])
190 if 'offset' in transform:
191 ocio_transform.setOffset(transform['offset'])
193 if 'direction' in transform:
194 ocio_transform.setDirection(
195 direction_options[transform['direction']])
197 ocio_transforms.append(ocio_transform)
198 elif transform['type'] == 'exponent':
199 ocio_transform = ocio.ExponentTransform()
200 ocio_transform.setValue(transform['value'])
201 ocio_transforms.append(ocio_transform)
202 elif transform['type'] == 'log':
203 ocio_transform = ocio.LogTransform(
204 base=transform['base'],
205 direction=direction_options[transform['direction']])
207 ocio_transforms.append(ocio_transform)
209 print('Ignoring unknown transform type : %s' % transform['type'])
211 # Build a group transform if necessary
212 if len(ocio_transforms) > 1:
213 transform_G = ocio.GroupTransform()
214 for transform in ocio_transforms:
215 transform_G.push_back(transform)
216 transform = transform_G
218 # Or take the first transform from the list
220 transform = ocio_transforms[0]
225 def create_config(config_data, nuke=False):
232 Parameter description.
237 Return value description.
241 config = ocio.Config()
244 # Set config wide values
246 config.setDescription('An ACES config generated from python')
247 config.setSearchPath('luts')
250 # Define the reference color space
252 reference_data = config_data['referenceColorSpace']
253 print('Adding the reference color space : %s' % reference_data.name)
255 # Create a color space
256 reference = ocio.ColorSpace(
257 name=reference_data.name,
258 bitDepth=reference_data.bit_depth,
259 description=reference_data.description,
260 equalityGroup=reference_data.equality_group,
261 family=reference_data.family,
262 isData=reference_data.is_data,
263 allocation=reference_data.allocation_type,
264 allocationVars=reference_data.allocation_vars)
267 config.addColorSpace(reference)
270 # Create the rest of the color spaces
272 for colorspace in sorted(config_data['colorSpaces']):
273 print('Creating new color space : %s' % colorspace.name)
275 ocio_colorspace = ocio.ColorSpace(
276 name=colorspace.name,
277 bitDepth=colorspace.bit_depth,
278 description=colorspace.description,
279 equalityGroup=colorspace.equality_group,
280 family=colorspace.family,
281 isData=colorspace.is_data,
282 allocation=colorspace.allocation_type,
283 allocationVars=colorspace.allocation_vars)
285 if colorspace.to_reference_transforms != []:
286 print('Generating To-Reference transforms')
287 ocio_transform = generate_OCIO_transform(
288 colorspace.to_reference_transforms)
289 ocio_colorspace.setTransform(
291 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
293 if colorspace.from_reference_transforms != []:
294 print('Generating From-Reference transforms')
295 ocio_transform = generate_OCIO_transform(
296 colorspace.from_reference_transforms)
297 ocio_colorspace.setTransform(
299 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
301 config.addColorSpace(ocio_colorspace)
306 # Define the views and displays
311 # Generic display and view setup
313 for display, view_list in config_data['displays'].iteritems():
314 for view_name, colorspace in view_list.iteritems():
315 config.addDisplay(display, view_name, colorspace.name)
316 if not (view_name in views):
317 views.append(view_name)
318 displays.append(display)
319 # A Nuke specific set of views and displays
322 # A few names: Output Transform, ACES, ACEScc, are hard-coded here.
323 # Would be better to automate.
326 for display, view_list in config_data['displays'].iteritems():
327 for view_name, colorspace in view_list.iteritems():
328 if (view_name == 'Output Transform'):
330 config.addDisplay(display, view_name, colorspace.name)
331 if not (view_name in views):
332 views.append(view_name)
333 displays.append(display)
335 config.addDisplay('linear', 'View', 'ACES2065-1')
336 displays.append('linear')
337 config.addDisplay('log', 'View', 'ACEScc')
338 displays.append('log')
340 # Set active displays and views
341 config.setActiveDisplays(','.join(sorted(displays)))
342 config.setActiveViews(','.join(views))
345 # Need to generalize this at some point
349 set_config_default_roles(
351 color_picking=reference.getName(),
352 color_timing=reference.getName(),
353 compositing_log=reference.getName(),
354 data=reference.getName(),
355 default=reference.getName(),
356 matte_paint=reference.getName(),
357 reference=reference.getName(),
358 scene_linear=reference.getName(),
359 texture_paint=reference.getName())
361 # Check to make sure we didn't screw something up
367 def generate_LUTs(odt_info,
372 lut_resolution_1d=4096,
373 lut_resolution_3d=64,
381 Parameter description.
386 Colorspaces and transforms converting between those colorspaces and
387 the reference colorspace, *ACES*.
390 print('generateLUTs - begin')
394 # Define the reference color space
396 ACES = ColorSpace('ACES2065-1')
398 'The Academy Color Encoding System reference color space')
399 ACES.equality_group = ''
402 ACES.allocation_type = ocio.Constants.ALLOCATION_LG2
403 ACES.allocation_vars = [-15, 6]
405 config_data['referenceColorSpace'] = ACES
408 # Define the displays
410 config_data['displays'] = {}
413 # Define the other color spaces
415 config_data['colorSpaces'] = []
417 # Matrix converting ACES AP1 primaries to AP0
418 ACES_AP1_to_AP0 = [0.6954522414, 0.1406786965, 0.1638690622,
419 0.0447945634, 0.8596711185, 0.0955343182,
420 -0.0055258826, 0.0040252103, 1.0015006723]
422 # Matrix converting ACES AP0 primaries to XYZ
423 ACES_AP0_to_XYZ = [0.9525523959, 0.0000000000, 0.0000936786,
424 0.3439664498, 0.7281660966, -0.0721325464,
425 0.0000000000, 0.0000000000, 1.0088251844]
430 def create_ACEScc(name='ACEScc',
434 cs = ColorSpace(name)
435 cs.description = 'The %s color space' % name
436 cs.equality_group = ''
440 ctls = [os.path.join(aces_CTL_directory,
442 'ACEScsc.ACEScc_to_ACES.a1.0.0.ctl'),
443 # This transform gets back to the AP1 primaries
444 # Useful as the 1d LUT is only covering the transfer function
445 # The primaries switch is covered by the matrix below
446 os.path.join(aces_CTL_directory,
448 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
449 lut = '%s_to_ACES.spi1d' % name
451 lut = sanitize_path(lut)
453 generate_1d_LUT_from_CTL(
454 os.path.join(lut_directory, lut),
466 cs.to_reference_transforms = []
467 cs.to_reference_transforms.append({
470 'interpolation': 'linear',
471 'direction': 'forward'
474 # AP1 primaries to AP0 primaries
475 cs.to_reference_transforms.append({
477 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
478 'direction': 'forward'
481 cs.from_reference_transforms = []
484 ACEScc = create_ACEScc()
485 config_data['colorSpaces'].append(ACEScc)
490 def create_ACESproxy(name='ACESproxy'):
491 cs = ColorSpace(name)
492 cs.description = 'The %s color space' % name
493 cs.equality_group = ''
498 os.path.join(aces_CTL_directory,
500 'ACEScsc.ACESproxy10i_to_ACES.a1.0.0.ctl'),
501 # This transform gets back to the AP1 primaries
502 # Useful as the 1d LUT is only covering the transfer function
503 # The primaries switch is covered by the matrix below
504 os.path.join(aces_CTL_directory,
506 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
507 lut = '%s_to_aces.spi1d' % name
509 lut = sanitize_path(lut)
511 generate_1d_LUT_from_CTL(
512 os.path.join(lut_directory, lut),
522 cs.to_reference_transforms = []
523 cs.to_reference_transforms.append({
526 'interpolation': 'linear',
527 'direction': 'forward'
530 # AP1 primaries to AP0 primaries
531 cs.to_reference_transforms.append({
533 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
534 'direction': 'forward'
537 cs.from_reference_transforms = []
540 ACESproxy = create_ACESproxy()
541 config_data['colorSpaces'].append(ACESproxy)
546 def create_ACEScg(name='ACEScg'):
547 cs = ColorSpace(name)
548 cs.description = 'The %s color space' % name
549 cs.equality_group = ''
553 cs.to_reference_transforms = []
555 # AP1 primaries to AP0 primaries
556 cs.to_reference_transforms.append({
558 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
559 'direction': 'forward'
562 cs.from_reference_transforms = []
565 ACEScg = create_ACEScg()
566 config_data['colorSpaces'].append(ACEScg)
571 def create_ADX(bit_depth=10, name='ADX'):
572 name = '%s%s' % (name, bit_depth)
573 cs = ColorSpace(name)
574 cs.description = '%s color space - used for film scans' % name
575 cs.equality_group = ''
580 cs.bit_depth = bit_depth = ocio.Constants.BIT_DEPTH_UINT10
581 adx_to_cdd = [1023.0 / 500.0, 0.0, 0.0, 0.0,
582 0.0, 1023.0 / 500.0, 0.0, 0.0,
583 0.0, 0.0, 1023.0 / 500.0, 0.0,
585 offset = [-95.0 / 500.0, -95.0 / 500.0, -95.0 / 500.0, 0.0]
586 elif bit_depth == 16:
587 cs.bit_depth = bit_depth = ocio.Constants.BIT_DEPTH_UINT16
588 adx_to_cdd = [65535.0 / 8000.0, 0.0, 0.0, 0.0,
589 0.0, 65535.0 / 8000.0, 0.0, 0.0,
590 0.0, 0.0, 65535.0 / 8000.0, 0.0,
592 offset = [-1520.0 / 8000.0, -1520.0 / 8000.0, -1520.0 / 8000.0,
595 cs.to_reference_transforms = []
597 # Convert from ADX to Channel-Dependent Density
598 cs.to_reference_transforms.append({
600 'matrix': adx_to_cdd,
602 'direction': 'forward'
605 # Convert from Channel-Dependent Density to Channel-Independent Density
606 cs.to_reference_transforms.append({
608 'matrix': [0.75573, 0.22197, 0.02230, 0,
609 0.05901, 0.96928, -0.02829, 0,
610 0.16134, 0.07406, 0.76460, 0,
612 'direction': 'forward'
615 # Copied from Alex Fry's adx_cid_to_rle.py
616 def create_CID_to_RLE_LUT():
617 def interpolate_1D(x, xp, fp):
618 return numpy.interp(x, xp, fp)
620 LUT_1D_xp = [-0.190000000000000,
632 LUT_1D_fp = [-6.000000000000000,
644 REF_PT = ((7120.0 - 1520.0) / 8000.0 * (100.0 / 55.0) -
645 math.log(0.18, 10.0))
649 return interpolate_1D(x, LUT_1D_xp, LUT_1D_fp)
650 return (100.0 / 55.0) * x - REF_PT
652 def fit(value, from_min, from_max, to_min, to_max):
653 if from_min == from_max:
654 raise ValueError('from_min == from_max')
655 return (value - from_min) / (from_max - from_min) * (
656 to_max - to_min) + to_min
658 NUM_SAMPLES = 2 ** 12
661 for i in xrange(NUM_SAMPLES):
662 x = i / (NUM_SAMPLES - 1.0)
663 x = fit(x, 0.0, 1.0, RANGE[0], RANGE[1])
664 data.append(cid_to_rle(x))
666 lut = 'ADX_CID_to_RLE.spi1d'
667 write_SPI_1d(os.path.join(lut_directory, lut),
675 # Convert Channel Independent Density values to Relative Log Exposure
677 lut = create_CID_to_RLE_LUT()
678 cs.to_reference_transforms.append({
681 'interpolation': 'linear',
682 'direction': 'forward'
685 # Convert Relative Log Exposure values to Relative Exposure values
686 cs.to_reference_transforms.append({
689 'direction': 'inverse'
692 # Convert Relative Exposure values to ACES values
693 cs.to_reference_transforms.append({
695 'matrix': [0.72286, 0.12630, 0.15084, 0,
696 0.11923, 0.76418, 0.11659, 0,
697 0.01427, 0.08213, 0.90359, 0,
699 'direction': 'forward'
702 cs.from_reference_transforms = []
705 ADX10 = create_ADX(bit_depth=10)
706 config_data['colorSpaces'].append(ADX10)
708 ADX16 = create_ADX(bit_depth=16)
709 config_data['colorSpaces'].append(ADX16)
712 # Camera Input Transforms
715 # RED color spaces to ACES
716 red_colorspaces = red.create_colorspaces(lut_directory, lut_resolution_1d)
717 for cs in red_colorspaces:
718 config_data['colorSpaces'].append(cs)
721 canon_colorspaces = canon.create_colorspaces(lut_directory,
723 for cs in canon_colorspaces:
724 config_data['colorSpaces'].append(cs)
727 sony_colorSpaces = sony.create_colorspaces(lut_directory,
729 for cs in sony_colorSpaces:
730 config_data['colorSpaces'].append(cs)
733 arri_colorSpaces = arri.create_colorspaces(lut_directory,
735 for cs in arri_colorSpaces:
736 config_data['colorSpaces'].append(cs)
739 # Generic log transform
741 def create_generic_log(name='log',
748 lut_resolution_1d=lut_resolution_1d):
749 cs = ColorSpace(name)
750 cs.description = 'The %s color space' % name
751 cs.equality_group = name
752 cs.family = 'Utility'
756 os.path.join(aces_CTL_directory,
758 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl')]
759 lut = '%s_to_aces.spi1d' % name
761 lut = sanitize_path(lut)
763 generate_1d_LUT_from_CTL(
764 os.path.join(lut_directory, lut),
771 'middleGrey': middle_grey,
772 'minExposure': min_exposure,
773 'maxExposure': max_exposure
780 cs.to_reference_transforms = []
781 cs.to_reference_transforms.append({
784 'interpolation': 'linear',
785 'direction': 'forward'
788 cs.from_reference_transforms = []
794 def create_ACES_LMT(lmt_name,
797 lut_resolution_1d=1024,
798 lut_resolution_3d=64,
800 cs = ColorSpace('%s' % lmt_name)
801 cs.description = 'The ACES Look Transform: %s' % lmt_name
802 cs.equality_group = ''
806 pprint.pprint(lmt_values)
809 # Generate the shaper transform
813 shaper_from_ACES_CTL,
815 shaper_params) = shaper_info
817 shaper_lut = '%s_to_aces.spi1d' % shaper_name
818 if (not os.path.exists(os.path.join(lut_directory, shaper_lut))):
819 ctls = [shaper_to_ACES_CTL % aces_CTL_directory]
821 # Remove spaces and parentheses
822 shaper_lut = shaper_lut.replace(
823 ' ', '_').replace(')', '_').replace('(', '_')
825 generate_1d_LUT_from_CTL(
826 os.path.join(lut_directory, shaper_lut),
830 1.0 / shaper_input_scale,
836 shaper_OCIO_transform = {
839 'interpolation': 'linear',
840 'direction': 'inverse'
844 # Generate the forward transform
846 cs.from_reference_transforms = []
848 if 'transformCTL' in lmt_values:
850 shaper_to_ACES_CTL % aces_CTL_directory,
851 os.path.join(aces_CTL_directory, lmt_values['transformCTL'])]
852 lut = '%s.%s.spi3d' % (shaper_name, lmt_name)
854 lut = sanitize_path(lut)
856 generate_3d_LUT_from_CTL(
857 os.path.join(lut_directory, lut),
861 1.0 / shaper_input_scale,
867 cs.from_reference_transforms.append(shaper_OCIO_transform)
868 cs.from_reference_transforms.append({
871 'interpolation': 'tetrahedral',
872 'direction': 'forward'
876 # Generate the inverse transform
878 cs.to_reference_transforms = []
880 if 'transformCTLInverse' in lmt_values:
882 os.path.join(aces_CTL_directory,
883 odt_values['transformCTLInverse']),
884 shaper_from_ACES_CTL % aces_CTL_directory
886 lut = 'Inverse.%s.%s.spi3d' % (odt_name, shaper_name)
888 lut = sanitize_path(lut)
890 generate_3d_LUT_from_CTL(
891 os.path.join(lut_directory, lut),
901 cs.to_reference_transforms.append({
904 'interpolation': 'tetrahedral',
905 'direction': 'forward'
908 shaper_inverse = shaper_OCIO_transform.copy()
909 shaper_inverse['direction'] = 'forward'
910 cs.to_reference_transforms.append(shaper_inverse)
918 lmt_lut_resolution_1d = max(4096, lut_resolution_1d)
919 lmt_lut_resolution_3d = max(65, lut_resolution_3d)
922 lmt_shaper_name = 'LMT Shaper'
925 'minExposure': -10.0,
928 lmt_shaper = create_generic_log(name=lmt_shaper_name,
929 middle_grey=lmt_params['middleGrey'],
930 min_exposure=lmt_params['minExposure'],
931 max_exposure=lmt_params['maxExposure'],
932 lut_resolution_1d=lmt_lut_resolution_1d)
933 config_data['colorSpaces'].append(lmt_shaper)
935 shaper_input_scale_generic_log2 = 1.0
937 # Log 2 shaper name and CTL transforms bundled up
942 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl'),
945 'ACESlib.OCIO_shaper_lin_to_log2_param.a1.0.0.ctl'),
946 shaper_input_scale_generic_log2,
949 sorted_LMTs = sorted(lmt_info.iteritems(), key=lambda x: x[1])
951 for lmt in sorted_LMTs:
952 (lmt_name, lmt_values) = lmt
953 cs = create_ACES_LMT(
954 lmt_values['transformUserName'],
957 lmt_lut_resolution_1d,
958 lmt_lut_resolution_3d,
960 config_data['colorSpaces'].append(cs)
963 # ACES RRT with the supplied ODT
965 def create_ACES_RRT_plus_ODT(odt_name,
968 lut_resolution_1d=1024,
969 lut_resolution_3d=64,
971 cs = ColorSpace('%s' % odt_name)
972 cs.description = '%s - %s Output Transform' % (
973 odt_values['transformUserNamePrefix'], odt_name)
974 cs.equality_group = ''
978 pprint.pprint(odt_values)
981 # Generate the shaper transform
983 # if 'shaperCTL' in odtValues:
986 shaper_from_ACES_CTL,
988 shaper_params) = shaper_info
990 if 'legalRange' in odt_values:
991 shaper_params['legalRange'] = odt_values['legalRange']
993 shaper_params['legalRange'] = 0
995 shaper_lut = '%s_to_aces.spi1d' % shaper_name
996 if (not os.path.exists(os.path.join(lut_directory, shaper_lut))):
997 ctls = [shaper_to_ACES_CTL % aces_CTL_directory]
999 # Remove spaces and parentheses
1000 shaper_lut = shaper_lut.replace(
1001 ' ', '_').replace(')', '_').replace('(', '_')
1003 generate_1d_LUT_from_CTL(
1004 os.path.join(lut_directory, shaper_lut),
1008 1.0 / shaper_input_scale,
1014 shaper_OCIO_transform = {
1017 'interpolation': 'linear',
1018 'direction': 'inverse'
1022 # Generate the forward transform
1024 cs.from_reference_transforms = []
1026 if 'transformLUT' in odt_values:
1027 # Copy into the lut dir
1028 transform_LUT_file_name = os.path.basename(
1029 odt_values['transformLUT'])
1030 lut = os.path.join(lut_directory, transform_LUT_file_name)
1031 shutil.copy(odt_values['transformLUT'], lut)
1033 cs.from_reference_transforms.append(shaper_OCIO_transform)
1034 cs.from_reference_transforms.append({
1036 'path': transform_LUT_file_name,
1037 'interpolation': 'tetrahedral',
1038 'direction': 'forward'
1040 elif 'transformCTL' in odt_values:
1044 shaper_to_ACES_CTL % aces_CTL_directory,
1045 os.path.join(aces_CTL_directory,
1048 os.path.join(aces_CTL_directory,
1050 odt_values['transformCTL'])]
1051 lut = '%s.RRT.a1.0.0.%s.spi3d' % (shaper_name, odt_name)
1053 lut = sanitize_path(lut)
1055 generate_3d_LUT_from_CTL(
1056 os.path.join(lut_directory, lut),
1061 1.0 / shaper_input_scale,
1067 cs.from_reference_transforms.append(shaper_OCIO_transform)
1068 cs.from_reference_transforms.append({
1071 'interpolation': 'tetrahedral',
1072 'direction': 'forward'
1076 # Generate the inverse transform
1078 cs.to_reference_transforms = []
1080 if 'transformLUTInverse' in odt_values:
1081 # Copy into the lut dir
1082 transform_LUT_inverse_file_name = os.path.basename(
1083 odt_values['transformLUTInverse'])
1084 lut = os.path.join(lut_directory, transform_LUT_inverse_file_name)
1085 shutil.copy(odt_values['transformLUTInverse'], lut)
1087 cs.to_reference_transforms.append({
1089 'path': transform_LUT_inverse_file_name,
1090 'interpolation': 'tetrahedral',
1091 'direction': 'forward'
1094 shaper_inverse = shaper_OCIO_transform.copy()
1095 shaper_inverse['direction'] = 'forward'
1096 cs.to_reference_transforms.append(shaper_inverse)
1097 elif 'transformCTLInverse' in odt_values:
1099 os.path.join(aces_CTL_directory,
1101 odt_values['transformCTLInverse']),
1102 os.path.join(aces_CTL_directory,
1104 'InvRRT.a1.0.0.ctl'),
1105 shaper_from_ACES_CTL % aces_CTL_directory
1107 lut = 'InvRRT.a1.0.0.%s.%s.spi3d' % (odt_name, shaper_name)
1109 lut = sanitize_path(lut)
1111 generate_3d_LUT_from_CTL(
1112 os.path.join(lut_directory, lut),
1123 cs.to_reference_transforms.append({
1126 'interpolation': 'tetrahedral',
1127 'direction': 'forward'
1130 shaper_inverse = shaper_OCIO_transform.copy()
1131 shaper_inverse['direction'] = 'forward'
1132 cs.to_reference_transforms.append(shaper_inverse)
1137 # RRT/ODT shaper options
1142 log2_shaper_name = shaper_name
1145 'minExposure': -6.0,
1148 log2_shaper = create_generic_log(
1149 name=log2_shaper_name,
1150 middle_grey=log2_params['middleGrey'],
1151 min_exposure=log2_params['minExposure'],
1152 max_exposure=log2_params['maxExposure'])
1153 config_data['colorSpaces'].append(log2_shaper)
1155 shaper_input_scale_generic_log2 = 1.0
1157 # Log 2 shaper name and CTL transforms bundled up
1158 log2_shaper_data = [
1162 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl'),
1165 'ACESlib.OCIO_shaper_lin_to_log2_param.a1.0.0.ctl'),
1166 shaper_input_scale_generic_log2,
1169 shaper_data[log2_shaper_name] = log2_shaper_data
1172 # Shaper that also includes the AP1 primaries
1173 # - Needed for some LUT baking steps
1175 log2_shaper_AP1 = create_generic_log(
1176 name=log2_shaper_name,
1177 middle_grey=log2_params['middleGrey'],
1178 min_exposure=log2_params['minExposure'],
1179 max_exposure=log2_params['maxExposure'])
1180 log2_shaper_AP1.name = '%s - AP1' % log2_shaper_AP1.name
1181 # AP1 primaries to AP0 primaries
1182 log2_shaper_AP1.to_reference_transforms.append({
1184 'matrix': mat44_from_mat33(ACES_AP1_to_AP0),
1185 'direction': 'forward'
1187 config_data['colorSpaces'].append(log2_shaper_AP1)
1190 # Choose your shaper
1192 rrt_shaper_name = log2_shaper_name
1193 rrt_shaper = log2_shaper_data
1196 # RRT + ODT Combinations
1198 sorted_odts = sorted(odt_info.iteritems(), key=lambda x: x[1])
1200 for odt in sorted_odts:
1201 (odt_name, odt_values) = odt
1203 # Have to handle ODTs that can generate either legal or full output
1204 if odt_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1205 'Academy.Rec709_100nits_dim.a1.0.0',
1206 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1207 odt_name_legal = '%s - Legal' % odt_values['transformUserName']
1209 odt_name_legal = odt_values['transformUserName']
1211 odt_legal = odt_values.copy()
1212 odt_legal['legalRange'] = 1
1214 cs = create_ACES_RRT_plus_ODT(
1221 config_data['colorSpaces'].append(cs)
1223 # Create a display entry using this color space
1224 config_data['displays'][odt_name_legal] = {
1227 'Output Transform': cs}
1229 if odt_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1230 'Academy.Rec709_100nits_dim.a1.0.0',
1231 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1232 print('Generating full range ODT for %s' % odt_name)
1234 odt_name_full = '%s - Full' % odt_values['transformUserName']
1235 odt_full = odt_values.copy()
1236 odt_full['legalRange'] = 0
1238 cs_full = create_ACES_RRT_plus_ODT(
1245 config_data['colorSpaces'].append(cs_full)
1247 # Create a display entry using this color space
1248 config_data['displays'][odt_name_full] = {
1251 'Output Transform': cs_full}
1254 # Generic Matrix transform
1256 def create_generic_matrix(name='matrix',
1257 from_reference_values=[],
1258 to_reference_values=[]):
1259 cs = ColorSpace(name)
1260 cs.description = 'The %s color space' % name
1261 cs.equality_group = name
1262 cs.family = 'Utility'
1265 cs.to_reference_transforms = []
1266 if to_reference_values != []:
1267 for matrix in to_reference_values:
1268 cs.to_reference_transforms.append({
1270 'matrix': mat44_from_mat33(matrix),
1271 'direction': 'forward'
1274 cs.from_reference_transforms = []
1275 if from_reference_values != []:
1276 for matrix in from_reference_values:
1277 cs.from_reference_transforms.append({
1279 'matrix': mat44_from_mat33(matrix),
1280 'direction': 'forward'
1285 cs = create_generic_matrix('XYZ', from_reference_values=[ACES_AP0_to_XYZ])
1286 config_data['colorSpaces'].append(cs)
1288 cs = create_generic_matrix(
1289 'Linear - AP1', to_reference_values=[ACES_AP1_to_AP0])
1290 config_data['colorSpaces'].append(cs)
1292 # ACES to Linear, P3D60 primaries
1293 XYZ_to_P3D60 = [2.4027414142, -0.8974841639, -0.3880533700,
1294 -0.8325796487, 1.7692317536, 0.0237127115,
1295 0.0388233815, -0.0824996856, 1.0363685997]
1297 cs = create_generic_matrix(
1299 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_P3D60])
1300 config_data['colorSpaces'].append(cs)
1302 # ACES to Linear, P3D60 primaries
1303 XYZ_to_P3DCI = [2.7253940305, -1.0180030062, -0.4401631952,
1304 -0.7951680258, 1.6897320548, 0.0226471906,
1305 0.0412418914, -0.0876390192, 1.1009293786]
1307 cs = create_generic_matrix(
1309 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_P3DCI])
1310 config_data['colorSpaces'].append(cs)
1312 # ACES to Linear, Rec 709 primaries
1313 XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
1314 -0.9692436363, 1.8759675015, 0.0415550574,
1315 0.0556300797, -0.2039769589, 1.0569715142]
1317 cs = create_generic_matrix(
1319 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_Rec709])
1320 config_data['colorSpaces'].append(cs)
1322 # ACES to Linear, Rec 2020 primaries
1323 XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
1324 -0.6666843518, 1.6164812366, 0.0157685458,
1325 0.0176398574, -0.0427706133, 0.9421031212]
1327 cs = create_generic_matrix(
1328 'Linear - Rec.2020',
1329 from_reference_values=[ACES_AP0_to_XYZ, XYZ_to_Rec2020])
1330 config_data['colorSpaces'].append(cs)
1332 print('generateLUTs - end')
1336 def generate_baked_LUTs(odt_info,
1342 lut_resolution_shaper=1024):
1349 Parameter description.
1354 Return value description.
1357 # Add the legal and full variations into this list
1358 odt_info_C = dict(odt_info)
1359 for odt_CTL_name, odt_values in odt_info.iteritems():
1360 if odt_CTL_name in ['Academy.Rec2020_100nits_dim.a1.0.0',
1361 'Academy.Rec709_100nits_dim.a1.0.0',
1362 'Academy.Rec709_D60sim_100nits_dim.a1.0.0']:
1363 odt_name = odt_values['transformUserName']
1365 odt_values_legal = dict(odt_values)
1366 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
1367 odt_info_C['%s - Legal' % odt_CTL_name] = odt_values_legal
1369 odt_values_full = dict(odt_values)
1370 odt_values_full['transformUserName'] = '%s - Full' % odt_name
1371 odt_info_C['%s - Full' % odt_CTL_name] = odt_values_full
1373 del (odt_info_C[odt_CTL_name])
1375 for odt_CTL_name, odt_values in odt_info_C.iteritems():
1376 odt_prefix = odt_values['transformUserNamePrefix']
1377 odt_name = odt_values['transformUserName']
1380 for input_space in ['ACEScc', 'ACESproxy']:
1381 args = ['--iconfig', config_path,
1383 '--inputspace', input_space]
1384 args += ['--outputspace', '%s' % odt_name]
1385 args += ['--description',
1386 '%s - %s for %s data' % (odt_prefix,
1389 args += ['--shaperspace', shaper_name,
1390 '--shapersize', str(lut_resolution_shaper)]
1391 args += ['--cubesize', str(lut_resolution_3d)]
1392 args += ['--format',
1394 os.path.join(baked_directory,
1396 '%s for %s.icc' % (odt_name, input_space))]
1398 bake_LUT = Process(description='bake a LUT',
1404 for input_space in ['ACEScc', 'ACESproxy']:
1405 args = ['--iconfig', config_path,
1407 '--inputspace', input_space]
1408 args += ['--outputspace', '%s' % odt_name]
1409 args += ['--description',
1410 '%s - %s for %s data' % (
1411 odt_prefix, odt_name, input_space)]
1412 args += ['--shaperspace', shaper_name,
1413 '--shapersize', str(lut_resolution_shaper)]
1414 args += ['--cubesize', str(lut_resolution_3d)]
1416 fargs = ['--format',
1421 '%s for %s Flame.3dl' % (odt_name, input_space))]
1422 bake_LUT = Process(description='bake a LUT',
1424 args=(args + fargs))
1427 largs = ['--format',
1432 '%s for %s Lustre.3dl' % (odt_name, input_space))]
1433 bake_LUT = Process(description='bake a LUT',
1435 args=(args + largs))
1439 for input_space in ['ACEScg', 'ACES2065-1']:
1440 args = ['--iconfig', config_path,
1442 '--inputspace', input_space]
1443 args += ['--outputspace', '%s' % odt_name]
1444 args += ['--description',
1445 '%s - %s for %s data' % (
1446 odt_prefix, odt_name, input_space)]
1447 if input_space == 'ACEScg':
1448 lin_shaper_name = '%s - AP1' % shaper_name
1450 lin_shaper_name = shaper_name
1451 args += ['--shaperspace', lin_shaper_name,
1452 '--shapersize', str(lut_resolution_shaper)]
1454 args += ['--cubesize', str(lut_resolution_3d)]
1456 margs = ['--format',
1461 '%s for %s Maya.csp' % (odt_name, input_space))]
1462 bake_LUT = Process(description='bake a LUT',
1464 args=(args + margs))
1467 hargs = ['--format',
1472 '%s for %s Houdini.lut' % (odt_name, input_space))]
1473 bake_LUT = Process(description='bake a LUT',
1475 args=(args + hargs))
1479 def create_config_dir(config_directory, bake_secondary_LUTs):
1486 Parameter description.
1491 Return value description.
1494 dirs = [config_directory, os.path.join(config_directory, 'luts')]
1495 if bake_secondary_LUTs:
1496 dirs.extend([os.path.join(config_directory, 'baked'),
1497 os.path.join(config_directory, 'baked', 'flame'),
1498 os.path.join(config_directory, 'baked', 'photoshop'),
1499 os.path.join(config_directory, 'baked', 'houdini'),
1500 os.path.join(config_directory, 'baked', 'lustre'),
1501 os.path.join(config_directory, 'baked', 'maya')])
1504 not os.path.exists(d) and os.mkdir(d)
1507 def get_transform_info(ctl_transform):
1514 Parameter description.
1519 Return value description.
1522 with open(ctl_transform, 'rb') as fp:
1523 lines = fp.readlines()
1525 # Grab transform ID and User Name
1526 transform_ID = lines[1][3:].split('<')[1].split('>')[1].strip()
1527 # print(transformID)
1528 transform_user_name = '-'.join(
1529 lines[2][3:].split('<')[1].split('>')[1].split('-')[1:]).strip()
1530 transform_user_name_prefix = (
1531 lines[2][3:].split('<')[1].split('>')[1].split('-')[0].strip())
1532 # print(transformUserName)
1534 return transform_ID, transform_user_name, transform_user_name_prefix
1537 def get_ODT_info(aces_CTL_directory):
1541 For versions after WGR9.
1546 Parameter description.
1551 Return value description.
1554 # TODO: Investigate usage of *files_walker* definition here.
1555 # Credit to Alex Fry for the original approach here
1556 odt_dir = os.path.join(aces_CTL_directory, 'odt')
1558 for dir_name, subdir_list, file_list in os.walk(odt_dir):
1559 for fname in file_list:
1560 all_odt.append((os.path.join(dir_name, fname)))
1562 odt_CTLs = [x for x in all_odt if
1563 ('InvODT' not in x) and (os.path.split(x)[-1][0] != '.')]
1569 for odt_CTL in odt_CTLs:
1570 odt_tokens = os.path.split(odt_CTL)
1573 # Handle nested directories
1574 odt_path_tokens = os.path.split(odt_tokens[-2])
1575 odt_dir = odt_path_tokens[-1]
1576 while odt_path_tokens[-2][-3:] != 'odt':
1577 odt_path_tokens = os.path.split(odt_path_tokens[-2])
1578 odt_dir = os.path.join(odt_path_tokens[-1], odt_dir)
1581 # print('odtDir : %s' % odtDir)
1582 transform_CTL = odt_tokens[-1]
1583 # print(transformCTL)
1584 odt_name = string.join(transform_CTL.split('.')[1:-1], '.')
1587 # Find id, user name and user name prefix
1589 transform_user_name,
1590 transform_user_name_prefix) = get_transform_info(
1591 os.path.join(aces_CTL_directory, 'odt', odt_dir, transform_CTL))
1594 transform_CTL_inverse = 'InvODT.%s.ctl' % odt_name
1595 if not os.path.exists(
1596 os.path.join(odt_tokens[-2], transform_CTL_inverse)):
1597 transform_CTL_inverse = None
1598 # print(transformCTLInverse)
1600 # Add to list of ODTs
1602 odts[odt_name]['transformCTL'] = os.path.join(odt_dir, transform_CTL)
1603 if transform_CTL_inverse != None:
1604 odts[odt_name]['transformCTLInverse'] = os.path.join(
1605 odt_dir, transform_CTL_inverse)
1607 odts[odt_name]['transformID'] = transform_ID
1608 odts[odt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1609 odts[odt_name]['transformUserName'] = transform_user_name
1611 print('ODT : %s' % odt_name)
1612 print('\tTransform ID : %s' % transform_ID)
1613 print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1614 print('\tTransform User Name : %s' % transform_user_name)
1615 print('\tForward ctl : %s' % (
1616 odts[odt_name]['transformCTL']))
1617 if 'transformCTLInverse' in odts[odt_name]:
1618 print('\tInverse ctl : %s' % (
1619 odts[odt_name]['transformCTLInverse']))
1621 print('\tInverse ctl : %s' % 'None')
1628 def get_LMT_info(aces_CTL_directory):
1632 For versions after WGR9.
1637 Parameter description.
1642 Return value description.
1645 # TODO: Investigate refactoring with previous definition.
1647 # Credit to Alex Fry for the original approach here
1648 lmt_dir = os.path.join(aces_CTL_directory, 'lmt')
1650 for dir_name, subdir_list, file_list in os.walk(lmt_dir):
1651 for fname in file_list:
1652 all_lmt.append((os.path.join(dir_name, fname)))
1654 lmt_CTLs = [x for x in all_lmt if
1655 ('InvLMT' not in x) and ('README' not in x) and (
1656 os.path.split(x)[-1][0] != '.')]
1662 for lmt_CTL in lmt_CTLs:
1663 lmt_tokens = os.path.split(lmt_CTL)
1666 # Handle nested directories
1667 lmt_path_tokens = os.path.split(lmt_tokens[-2])
1668 lmt_dir = lmt_path_tokens[-1]
1669 while lmt_path_tokens[-2][-3:] != 'ctl':
1670 lmt_path_tokens = os.path.split(lmt_path_tokens[-2])
1671 lmt_dir = os.path.join(lmt_path_tokens[-1], lmt_dir)
1674 # print('lmtDir : %s' % lmtDir)
1675 transform_CTL = lmt_tokens[-1]
1676 # print(transformCTL)
1677 lmt_name = string.join(transform_CTL.split('.')[1:-1], '.')
1680 # Find id, user name and user name prefix
1682 transform_user_name,
1683 transform_user_name_prefix) = get_transform_info(
1684 os.path.join(aces_CTL_directory, lmt_dir, transform_CTL))
1687 transform_CTL_inverse = 'InvLMT.%s.ctl' % lmt_name
1688 if not os.path.exists(
1689 os.path.join(lmt_tokens[-2], transform_CTL_inverse)):
1690 transform_CTL_inverse = None
1691 # print(transformCTLInverse)
1693 # Add to list of LMTs
1695 lmts[lmt_name]['transformCTL'] = os.path.join(lmt_dir, transform_CTL)
1696 if transform_CTL_inverse != None:
1697 # TODO: Check unresolved *odt_name* referemce.
1698 lmts[odt_name]['transformCTLInverse'] = os.path.join(
1699 lmt_dir, transform_CTL_inverse)
1701 lmts[lmt_name]['transformID'] = transform_ID
1702 lmts[lmt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1703 lmts[lmt_name]['transformUserName'] = transform_user_name
1705 print('LMT : %s' % lmt_name)
1706 print('\tTransform ID : %s' % transform_ID)
1707 print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1708 print('\tTransform User Name : %s' % transform_user_name)
1709 print('\t Forward ctl : %s' % lmts[lmt_name]['transformCTL'])
1710 if 'transformCTLInverse' in lmts[lmt_name]:
1711 print('\t Inverse ctl : %s' % (
1712 lmts[lmt_name]['transformCTLInverse']))
1714 print('\t Inverse ctl : %s' % 'None')
1721 def create_ACES_config(aces_CTL_directory,
1723 lut_resolution_1d=4096,
1724 lut_resolution_3d=64,
1725 bake_secondary_LUTs=True,
1728 Creates the ACES configuration.
1733 Parameter description.
1738 Return value description.
1741 # Get ODT names and CTL paths
1742 odt_info = get_ODT_info(aces_CTL_directory)
1744 # Get ODT names and CTL paths
1745 lmt_info = get_LMT_info(aces_CTL_directory)
1748 create_config_dir(config_directory, bake_secondary_LUTs)
1750 # Generate config data and LUTs for different transforms
1751 lut_directory = os.path.join(config_directory, 'luts')
1752 shaper_name = 'Output Shaper'
1753 config_data = generate_LUTs(odt_info,
1762 # Create the config using the generated LUTs
1763 print('Creating generic config')
1764 config = create_config(config_data)
1767 # Write the config to disk
1768 write_config(config,
1769 os.path.join(config_directory, 'config.ocio'))
1771 # Create a config that will work well with Nuke using the previously
1773 print('Creating Nuke-specific config')
1774 nuke_config = create_config(config_data, nuke=True)
1777 # Write the config to disk
1778 write_config(nuke_config,
1779 os.path.join(config_directory, 'nuke_config.ocio'))
1781 # Bake secondary LUTs using the config
1782 if bake_secondary_LUTs:
1783 generate_baked_LUTs(odt_info,
1785 os.path.join(config_directory, 'baked'),
1786 os.path.join(config_directory, 'config.ocio'),
1801 Parameter description.
1806 Return value description.
1811 p = optparse.OptionParser(description='An OCIO config generation script',
1812 prog='createACESConfig',
1813 version='createACESConfig 0.1',
1814 usage='%prog [options]')
1815 p.add_option('--acesCTLDir', '-a', default=os.environ.get(
1816 ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
1817 p.add_option('--configDir', '-c', default=os.environ.get(
1818 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
1819 p.add_option('--lutResolution1d', default=4096)
1820 p.add_option('--lutResolution3d', default=64)
1821 p.add_option('--dontBakeSecondaryLUTs', action='store_true')
1822 p.add_option('--keepTempImages', action='store_true')
1824 options, arguments = p.parse_args()
1829 aces_CTL_directory = options.acesCTLDir
1830 config_directory = options.configDir
1831 lut_resolution_1d = int(options.lutResolution1d)
1832 lut_resolution_3d = int(options.lutResolution3d)
1833 bake_secondary_LUTs = not options.dontBakeSecondaryLUTs
1834 cleanup_temp_images = not options.keepTempImages
1836 # TODO: Investigate the following statements.
1838 args_start = sys.argv.index('--') + 1
1839 args = sys.argv[args_start:]
1841 args_start = len(sys.argv) + 1
1844 print('command line : \n%s\n' % ' '.join(sys.argv))
1846 assert aces_CTL_directory is not None, (
1847 'process: No "{0}" environment variable defined or no "ACES CTL" '
1848 'directory specified'.format(
1849 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
1851 assert config_directory is not None, (
1852 'process: No "{0}" environment variable defined or no configuration '
1853 'directory specified'.format(
1854 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
1856 return create_ACES_config(aces_CTL_directory,
1860 bake_secondary_LUTs,
1861 cleanup_temp_images)
1864 if __name__ == '__main__':