2 # -*- coding: utf-8 -*-
5 Defines objects creating the *ACES* configuration.
13 import PyOpenColorIO as ocio
15 import aces_ocio.create_aces_colorspaces as aces
16 import aces_ocio.create_arri_colorspaces as arri
17 import aces_ocio.create_canon_colorspaces as canon
18 import aces_ocio.create_red_colorspaces as red
19 import aces_ocio.create_sony_colorspaces as sony
20 import aces_ocio.create_general_colorspaces as general
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, compact
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',
48 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
49 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
52 def set_config_default_roles(config,
63 Sets given *OCIO* configuration default roles.
69 color_picking : str or unicode
70 Color picking role title.
71 color_timing : str or unicode
72 Color timing role title.
73 compositing_log : str or unicode
74 Compositing log role title.
77 default : str or unicode
79 matte_paint : str or unicode
80 Matte painting role title.
81 reference : str or unicode
83 scene_linear : str or unicode
84 Scene linear role title.
85 texture_paint : str or unicode
86 Texture painting role title.
95 config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
97 config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
99 config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
101 config.setRole(ocio.Constants.ROLE_DATA, data)
103 config.setRole(ocio.Constants.ROLE_DEFAULT, default)
105 config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
107 config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
109 config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
111 config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
116 def write_config(config, config_path, sanity_check=True):
118 Writes the configuration to given path.
123 Parameter description.
128 Return value description.
136 print 'Configuration was not written due to a failed Sanity Check'
139 with open(config_path, mode='w') as fp:
140 fp.write(config.serialize())
143 def generate_OCIO_transform(transforms):
150 Parameter description.
155 Return value description.
158 interpolation_options = {
159 'linear': ocio.Constants.INTERP_LINEAR,
160 'nearest': ocio.Constants.INTERP_NEAREST,
161 'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL
163 direction_options = {
164 'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
165 'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE
170 for transform in transforms:
173 if transform['type'] == 'lutFile':
174 ocio_transform = ocio.FileTransform(
175 src=transform['path'],
176 interpolation=interpolation_options[
177 transform['interpolation']],
178 direction=direction_options[transform['direction']])
179 ocio_transforms.append(ocio_transform)
182 elif transform['type'] == 'matrix':
183 ocio_transform = ocio.MatrixTransform()
184 # MatrixTransform member variables can't be initialized directly.
185 # Each must be set individually.
186 ocio_transform.setMatrix(transform['matrix'])
188 if 'offset' in transform:
189 ocio_transform.setOffset(transform['offset'])
191 if 'direction' in transform:
192 ocio_transform.setDirection(
193 direction_options[transform['direction']])
195 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)
204 elif transform['type'] == 'log':
205 ocio_transform = ocio.LogTransform(
206 base=transform['base'],
207 direction=direction_options[transform['direction']])
209 ocio_transforms.append(ocio_transform)
211 # color space transform
212 elif transform['type'] == 'colorspace':
213 ocio_transform = ocio.ColorSpaceTransform( src=transform['src'],
214 dst=transform['dst'],
215 direction=direction_options['forward'] )
216 ocio_transforms.append(ocio_transform)
220 print("Ignoring unknown transform type : %s" % transform['type'])
222 if len(ocio_transforms) > 1:
223 group_transform = ocio.GroupTransform()
224 for transform in ocio_transforms:
225 group_transform.push_back(transform)
226 transform = group_transform
228 transform = ocio_transforms[0]
232 def add_colorspace_alias(config, reference_colorspace, colorspace, colorspace_alias_names):
239 Parameter description.
244 Return value description.
247 for alias_name in colorspace_alias_names:
248 if alias_name == colorspace.name.lower():
251 print( "Adding alias colorspace space %s, alias to %s" % (
252 alias_name, colorspace.name))
254 compact_family_name = "Aliases"
256 ocio_colorspace_alias = ocio.ColorSpace(
258 bitDepth=colorspace.bit_depth,
259 description=colorspace.description,
260 equalityGroup=colorspace.equality_group,
261 family=compact_family_name,
262 isData=colorspace.is_data,
263 allocation=colorspace.allocation_type,
264 allocationVars=colorspace.allocation_vars)
266 if colorspace.to_reference_transforms != []:
267 print("Generating To-Reference transforms")
268 ocio_transform = generate_OCIO_transform([{
269 'type': 'colorspace',
270 'src': colorspace.name,
271 'dst': reference_colorspace.name,
272 'direction': 'forward'
274 ocio_colorspace_alias.setTransform(
276 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
278 if colorspace.from_reference_transforms != []:
279 print("Generating From-Reference transforms")
280 ocio_transform = generate_OCIO_transform([{
281 'type': 'colorspace',
282 'src': reference_colorspace.name,
283 'dst': colorspace.name,
284 'direction': 'forward'
286 ocio_colorspace_alias.setTransform(
288 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
290 config.addColorSpace(ocio_colorspace_alias)
293 def create_config(config_data, nuke=False):
300 Parameter description.
305 Return value description.
308 # Creating the *OCIO* configuration.
309 config = ocio.Config()
311 # Setting configuration overall values.
312 config.setDescription('An ACES config generated from python')
313 config.setSearchPath('luts')
315 # Defining the reference colorspace.
316 reference_data = config_data['referenceColorSpace']
317 print('Adding the reference color space : %s' % reference_data.name)
319 reference = ocio.ColorSpace(
320 name=reference_data.name,
321 bitDepth=reference_data.bit_depth,
322 description=reference_data.description,
323 equalityGroup=reference_data.equality_group,
324 family=reference_data.family,
325 isData=reference_data.is_data,
326 allocation=reference_data.allocation_type,
327 allocationVars=reference_data.allocation_vars)
329 config.addColorSpace(reference)
332 if reference_data.aliases != []:
333 add_colorspace_alias(config, reference_data,
334 reference_data, reference_data.aliases)
338 # Creating the remaining colorspaces.
339 for colorspace in sorted(config_data['colorSpaces']):
340 print('Creating new color space : %s' % colorspace.name)
342 ocio_colorspace = ocio.ColorSpace(
343 name=colorspace.name,
344 bitDepth=colorspace.bit_depth,
345 description=colorspace.description,
346 equalityGroup=colorspace.equality_group,
347 family=colorspace.family,
348 isData=colorspace.is_data,
349 allocation=colorspace.allocation_type,
350 allocationVars=colorspace.allocation_vars)
352 if colorspace.to_reference_transforms:
353 print('Generating To-Reference transforms')
354 ocio_transform = generate_OCIO_transform(
355 colorspace.to_reference_transforms)
356 ocio_colorspace.setTransform(
358 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
360 if colorspace.from_reference_transforms:
361 print('Generating From-Reference transforms')
362 ocio_transform = generate_OCIO_transform(
363 colorspace.from_reference_transforms)
364 ocio_colorspace.setTransform(
366 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
368 config.addColorSpace(ocio_colorspace)
371 # Add alias to normal colorspace, using compact name
373 if colorspace.aliases != []:
374 add_colorspace_alias(config, reference_data,
375 colorspace, colorspace.aliases)
379 # Defining the *views* and *displays*.
383 # Defining a *generic* *display* and *view* setup.
385 for display, view_list in config_data['displays'].iteritems():
386 for view_name, colorspace in view_list.iteritems():
387 config.addDisplay(display, view_name, colorspace.name)
388 if not (view_name in views):
389 views.append(view_name)
390 displays.append(display)
392 # Defining the *Nuke* specific set of *views* and *displays*.
394 for display, view_list in config_data['displays'].iteritems():
395 for view_name, colorspace in view_list.iteritems():
396 if view_name == 'Output Transform':
398 config.addDisplay(display, view_name, colorspace.name)
399 if not (view_name in views):
400 views.append(view_name)
401 displays.append(display)
403 linear_display_space_name = config_data['linearDisplaySpace'].name
404 log_display_space_name = config_data['logDisplaySpace'].name
406 config.addDisplay('linear', 'View', linear_display_space_name)
407 displays.append('linear')
408 config.addDisplay('log', 'View', log_display_space_name)
409 displays.append('log')
411 # Setting the active *displays* and *views*.
412 config.setActiveDisplays(','.join(sorted(displays)))
413 config.setActiveViews(','.join(views))
415 set_config_default_roles(
417 color_picking=reference.getName(),
418 color_timing=reference.getName(),
419 compositing_log=reference.getName(),
420 data=reference.getName(),
421 default=reference.getName(),
422 matte_paint=reference.getName(),
423 reference=reference.getName(),
424 scene_linear=reference.getName(),
425 texture_paint=reference.getName())
431 def generate_LUTs(odt_info,
436 lut_resolution_1d=4096,
437 lut_resolution_3d=64,
445 Parameter description.
450 Colorspaces and transforms converting between those colorspaces and
451 the reference colorspace, *ACES*.
454 print('generateLUTs - begin')
457 # Initialize a few variables
458 config_data['displays'] = {}
459 config_data['colorSpaces'] = []
461 # -------------------------------------------------------------------------
462 # *ACES Color Spaces*
463 # -------------------------------------------------------------------------
469 aces_log_display_space) = aces.create_colorspaces(aces_CTL_directory,
478 config_data['referenceColorSpace'] = aces_reference
480 for cs in aces_colorspaces:
481 config_data['colorSpaces'].append(cs)
483 for name, data in aces_displays.iteritems():
484 config_data['displays'][name] = data
486 config_data['linearDisplaySpace'] = aces_reference
487 config_data['logDisplaySpace'] = aces_log_display_space
489 # -------------------------------------------------------------------------
490 # *Camera Input Transforms*
491 # -------------------------------------------------------------------------
494 arri_colorSpaces = arri.create_colorspaces(lut_directory,
496 for cs in arri_colorSpaces:
497 config_data['colorSpaces'].append(cs)
499 # *Canon-Log* to *ACES*.
500 canon_colorspaces = canon.create_colorspaces(lut_directory,
502 for cs in canon_colorspaces:
503 config_data['colorSpaces'].append(cs)
505 # *RED* colorspaces to *ACES*.
506 red_colorspaces = red.create_colorspaces(lut_directory,
508 for cs in red_colorspaces:
509 config_data['colorSpaces'].append(cs)
512 sony_colorSpaces = sony.create_colorspaces(lut_directory,
514 for cs in sony_colorSpaces:
515 config_data['colorSpaces'].append(cs)
517 # -------------------------------------------------------------------------
518 # General Color Spaces
519 # -------------------------------------------------------------------------
520 general_colorSpaces = general.create_colorspaces(lut_directory,
523 for cs in general_colorSpaces:
524 config_data['colorSpaces'].append(cs)
526 print('generateLUTs - end')
530 def generate_baked_LUTs(odt_info,
536 lut_resolution_shaper=1024):
543 Parameter description.
548 Return value description.
551 # Create two entries for ODTs that have full and legal range support
552 odt_info_C = dict(odt_info)
553 for odt_CTL_name, odt_values in odt_info.iteritems():
554 if odt_values['transformHasFullLegalSwitch']:
555 odt_name = odt_values['transformUserName']
557 odt_values_legal = dict(odt_values)
558 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
559 odt_info_C['%s - Legal' % odt_CTL_name] = odt_values_legal
561 odt_values_full = dict(odt_values)
562 odt_values_full['transformUserName'] = '%s - Full' % odt_name
563 odt_info_C['%s - Full' % odt_CTL_name] = odt_values_full
565 del (odt_info_C[odt_CTL_name])
567 # Generate appropriate LUTs for each ODT
568 for odt_CTL_name, odt_values in odt_info_C.iteritems():
569 odt_prefix = odt_values['transformUserNamePrefix']
570 odt_name = odt_values['transformUserName']
573 for input_space in ['ACEScc', 'ACESproxy']:
574 args = ['--iconfig', config_path,
576 '--inputspace', input_space]
577 args += ['--outputspace', '%s' % odt_name]
578 args += ['--description',
579 '%s - %s for %s data' % (odt_prefix,
582 args += ['--shaperspace', shaper_name,
583 '--shapersize', str(lut_resolution_shaper)]
584 args += ['--cubesize', str(lut_resolution_3d)]
587 os.path.join(baked_directory,
589 '%s for %s.icc' % (odt_name, input_space))]
591 bake_LUT = Process(description='bake a LUT',
597 for input_space in ['ACEScc', 'ACESproxy']:
598 args = ['--iconfig', config_path,
600 '--inputspace', input_space]
601 args += ['--outputspace', '%s' % odt_name]
602 args += ['--description',
603 '%s - %s for %s data' % (
604 odt_prefix, odt_name, input_space)]
605 args += ['--shaperspace', shaper_name,
606 '--shapersize', str(lut_resolution_shaper)]
607 args += ['--cubesize', str(lut_resolution_3d)]
614 '%s for %s Flame.3dl' % (odt_name, input_space))]
615 bake_LUT = Process(description='bake a LUT',
625 '%s for %s Lustre.3dl' % (odt_name, input_space))]
626 bake_LUT = Process(description='bake a LUT',
632 for input_space in ['ACEScg', 'ACES2065-1']:
633 args = ['--iconfig', config_path,
635 '--inputspace', input_space]
636 args += ['--outputspace', '%s' % odt_name]
637 args += ['--description',
638 '%s - %s for %s data' % (
639 odt_prefix, odt_name, input_space)]
640 if input_space == 'ACEScg':
641 lin_shaper_name = '%s - AP1' % shaper_name
643 lin_shaper_name = shaper_name
644 args += ['--shaperspace', lin_shaper_name,
645 '--shapersize', str(lut_resolution_shaper)]
647 args += ['--cubesize', str(lut_resolution_3d)]
654 '%s for %s Maya.csp' % (odt_name, input_space))]
655 bake_LUT = Process(description='bake a LUT',
665 '%s for %s Houdini.lut' % (odt_name, input_space))]
666 bake_LUT = Process(description='bake a LUT',
672 def create_config_dir(config_directory, bake_secondary_LUTs):
679 Parameter description.
684 Return value description.
687 lut_directory = os.path.join(config_directory, 'luts')
688 dirs = [config_directory, lut_directory]
689 if bake_secondary_LUTs:
690 dirs.extend([os.path.join(config_directory, 'baked'),
691 os.path.join(config_directory, 'baked', 'flame'),
692 os.path.join(config_directory, 'baked', 'photoshop'),
693 os.path.join(config_directory, 'baked', 'houdini'),
694 os.path.join(config_directory, 'baked', 'lustre'),
695 os.path.join(config_directory, 'baked', 'maya')])
698 not os.path.exists(d) and os.mkdir(d)
702 def create_ACES_config(aces_CTL_directory,
704 lut_resolution_1d=4096,
705 lut_resolution_3d=64,
706 bake_secondary_LUTs=True,
709 Creates the ACES configuration.
714 Parameter description.
719 Return value description.
722 lut_directory = create_config_dir(config_directory, bake_secondary_LUTs)
724 odt_info = aces.get_ODT_info(aces_CTL_directory)
725 lmt_info = aces.get_LMT_info(aces_CTL_directory)
727 shaper_name = 'Output Shaper'
728 config_data = generate_LUTs(odt_info,
737 print('Creating "generic" config')
738 config = create_config(config_data)
742 os.path.join(config_directory, 'config.ocio'))
744 print('Creating "Nuke" config')
745 nuke_config = create_config(config_data, nuke=True)
748 write_config(nuke_config,
749 os.path.join(config_directory, 'nuke_config.ocio'))
751 if bake_secondary_LUTs:
752 generate_baked_LUTs(odt_info,
754 os.path.join(config_directory, 'baked'),
755 os.path.join(config_directory, 'config.ocio'),
770 Parameter description.
775 Return value description.
780 p = optparse.OptionParser(description='An OCIO config generation script',
781 prog='createACESConfig',
782 version='createACESConfig 0.1',
783 usage='%prog [options]')
784 p.add_option('--acesCTLDir', '-a', default=os.environ.get(
785 ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
786 p.add_option('--configDir', '-c', default=os.environ.get(
787 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
788 p.add_option('--lutResolution1d', default=4096)
789 p.add_option('--lutResolution3d', default=64)
790 p.add_option('--dontBakeSecondaryLUTs', action='store_true')
791 p.add_option('--keepTempImages', action='store_true')
793 options, arguments = p.parse_args()
795 aces_CTL_directory = options.acesCTLDir
796 config_directory = options.configDir
797 lut_resolution_1d = int(options.lutResolution1d)
798 lut_resolution_3d = int(options.lutResolution3d)
799 bake_secondary_LUTs = not options.dontBakeSecondaryLUTs
800 cleanup_temp_images = not options.keepTempImages
802 # TODO: Investigate the following statements.
804 args_start = sys.argv.index('--') + 1
805 args = sys.argv[args_start:]
807 args_start = len(sys.argv) + 1
810 print('command line : \n%s\n' % ' '.join(sys.argv))
812 assert aces_CTL_directory is not None, (
813 'process: No "{0}" environment variable defined or no "ACES CTL" '
814 'directory specified'.format(
815 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
817 assert config_directory is not None, (
818 'process: No "{0}" environment variable defined or no configuration '
819 'directory specified'.format(
820 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
822 return create_ACES_config(aces_CTL_directory,
830 if __name__ == '__main__':