2 # -*- coding: utf-8 -*-
5 Defines objects creating the *ACES* configuration.
8 from __future__ import division
13 import PyOpenColorIO as ocio
14 from aces_ocio.colorspaces import aces
15 from aces_ocio.colorspaces import arri
16 from aces_ocio.colorspaces import canon
17 from aces_ocio.colorspaces import general
18 from aces_ocio.colorspaces import gopro
19 from aces_ocio.colorspaces import panasonic
20 from aces_ocio.colorspaces import red
21 from aces_ocio.colorspaces import sony
22 from aces_ocio.process import Process
25 __author__ = 'ACES Developers'
26 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
28 __maintainer__ = 'ACES Developers'
29 __email__ = 'aces@oscars.org'
30 __status__ = 'Production'
32 __all__ = ['ACES_OCIO_CTL_DIRECTORY_ENVIRON',
33 'ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON',
34 'set_config_default_roles',
36 'generate_OCIO_transform',
37 'add_colorspace_alias',
40 'generate_baked_LUTs',
45 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
46 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
49 def set_config_default_roles(config,
60 compositing_linear=''):
62 Sets given *OCIO* configuration default roles.
68 color_picking : str or unicode
69 Color picking role title.
70 color_timing : str or unicode
71 Color timing role title.
72 compositing_log : str or unicode
73 Compositing log role title.
76 default : str or unicode
78 matte_paint : str or unicode
79 Matte painting role title.
80 reference : str or unicode
82 scene_linear : str or unicode
83 Scene linear role title.
84 texture_paint : str or unicode
85 Texture painting role title.
94 config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
96 config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
98 config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
100 config.setRole(ocio.Constants.ROLE_DATA, data)
102 config.setRole(ocio.Constants.ROLE_DEFAULT, default)
104 config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
106 config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
108 config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
110 # 'rendering' and 'compositing_linear' roles default to the 'scene_linear'
111 # value if not set explicitly
113 config.setRole("rendering", rendering)
114 if compositing_linear:
115 config.setRole("compositing_linear", compositing_linear)
117 config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
119 config.setRole("rendering", scene_linear)
120 if not compositing_linear:
121 config.setRole("compositing_linear", scene_linear)
126 def write_config(config, config_path, sanity_check=True):
128 Writes the configuration to given path.
133 Parameter description.
138 Return value description.
146 print 'Configuration was not written due to a failed Sanity Check'
149 with open(config_path, mode='w') as fp:
150 fp.write(config.serialize())
153 def generate_OCIO_transform(transforms):
160 Parameter description.
165 Return value description.
168 interpolation_options = {
169 'linear': ocio.Constants.INTERP_LINEAR,
170 'nearest': ocio.Constants.INTERP_NEAREST,
171 'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL}
173 direction_options = {
174 'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
175 'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE}
179 for transform in transforms:
182 if transform['type'] == 'lutFile':
183 ocio_transform = ocio.FileTransform(
184 src=transform['path'],
185 interpolation=interpolation_options[
186 transform['interpolation']],
187 direction=direction_options[transform['direction']])
188 ocio_transforms.append(ocio_transform)
191 elif transform['type'] == 'matrix':
192 ocio_transform = ocio.MatrixTransform()
193 # MatrixTransform member variables can't be initialized directly.
194 # Each must be set individually.
195 ocio_transform.setMatrix(transform['matrix'])
197 if 'offset' in transform:
198 ocio_transform.setOffset(transform['offset'])
200 if 'direction' in transform:
201 ocio_transform.setDirection(
202 direction_options[transform['direction']])
204 ocio_transforms.append(ocio_transform)
207 elif transform['type'] == 'exponent':
208 ocio_transform = ocio.ExponentTransform()
209 ocio_transform.setValue(transform['value'])
210 ocio_transforms.append(ocio_transform)
213 elif transform['type'] == 'log':
214 ocio_transform = ocio.LogTransform(
215 base=transform['base'],
216 direction=direction_options[transform['direction']])
218 ocio_transforms.append(ocio_transform)
220 # color space transform
221 elif transform['type'] == 'colorspace':
222 ocio_transform = ocio.ColorSpaceTransform(src=transform['src'],
223 dst=transform['dst'],
227 ocio_transforms.append(ocio_transform)
230 print("Ignoring unknown transform type : %s" % transform['type'])
232 if len(ocio_transforms) > 1:
233 group_transform = ocio.GroupTransform()
234 for transform in ocio_transforms:
235 group_transform.push_back(transform)
236 transform = group_transform
238 transform = ocio_transforms[0]
243 def add_colorspace_alias(config,
244 reference_colorspace,
246 colorspace_alias_names):
253 Parameter description.
258 Return value description.
261 for alias_name in colorspace_alias_names:
262 if alias_name.lower() == colorspace.name.lower():
263 print('Skipping alias creation for %s, alias %s, because lower cased names match' % (
264 colorspace.name, alias_name) )
267 print('Adding alias colorspace space %s, alias to %s' % (
268 alias_name, colorspace.name))
270 compact_family_name = 'Aliases'
272 ocio_colorspace_alias = ocio.ColorSpace(
274 bitDepth=colorspace.bit_depth,
275 description=colorspace.description,
276 equalityGroup=colorspace.equality_group,
277 family=compact_family_name,
278 isData=colorspace.is_data,
279 allocation=colorspace.allocation_type,
280 allocationVars=colorspace.allocation_vars)
282 if colorspace.to_reference_transforms:
283 print('Generating To-Reference transforms')
284 ocio_transform = generate_OCIO_transform(
285 [{'type': 'colorspace',
286 'src': colorspace.name,
287 'dst': reference_colorspace.name,
288 'direction': 'forward'}])
289 ocio_colorspace_alias.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 [{'type': 'colorspace',
297 'src': reference_colorspace.name,
298 'dst': colorspace.name,
299 'direction': 'forward'}])
300 ocio_colorspace_alias.setTransform(
302 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
304 config.addColorSpace(ocio_colorspace_alias)
307 def create_config(config_data, gui=False):
314 Parameter description.
319 Return value description.
322 # Creating the *OCIO* configuration.
323 config = ocio.Config()
325 # Setting configuration overall values.
326 config.setDescription('An ACES config generated from python')
327 config.setSearchPath('luts')
329 # Defining the reference colorspace.
330 reference_data = config_data['referenceColorSpace']
331 print('Adding the reference color space : %s' % reference_data.name)
333 reference = ocio.ColorSpace(
334 name=reference_data.name,
335 bitDepth=reference_data.bit_depth,
336 description=reference_data.description,
337 equalityGroup=reference_data.equality_group,
338 family=reference_data.family,
339 isData=reference_data.is_data,
340 allocation=reference_data.allocation_type,
341 allocationVars=reference_data.allocation_vars)
343 config.addColorSpace(reference)
347 if reference_data.aliases != []:
348 add_colorspace_alias(config, reference_data,
349 reference_data, reference_data.aliases)
353 # Creating the remaining colorspaces.
354 for colorspace in sorted(config_data['colorSpaces']):
355 print('Creating new color space : %s' % colorspace.name)
357 ocio_colorspace = ocio.ColorSpace(
358 name=colorspace.name,
359 bitDepth=colorspace.bit_depth,
360 description=colorspace.description,
361 equalityGroup=colorspace.equality_group,
362 family=colorspace.family,
363 isData=colorspace.is_data,
364 allocation=colorspace.allocation_type,
365 allocationVars=colorspace.allocation_vars)
367 if colorspace.to_reference_transforms:
368 print('Generating To-Reference transforms')
369 ocio_transform = generate_OCIO_transform(
370 colorspace.to_reference_transforms)
371 ocio_colorspace.setTransform(
373 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
375 if colorspace.from_reference_transforms:
376 print('Generating From-Reference transforms')
377 ocio_transform = generate_OCIO_transform(
378 colorspace.from_reference_transforms)
379 ocio_colorspace.setTransform(
381 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
383 config.addColorSpace(ocio_colorspace)
386 # Add alias to normal colorspace, using compact name
389 if colorspace.aliases != []:
390 #print('Adding alias color spaces : %s' % colorspace.aliases)
391 add_colorspace_alias(config, reference_data,
392 colorspace, colorspace.aliases)
396 # Defining the *views* and *displays*.
400 # Defining a *generic* *display* and *view* setup.
402 for display, view_list in config_data['displays'].iteritems():
403 for view_name, colorspace in view_list.iteritems():
404 config.addDisplay(display, view_name, colorspace.name)
405 if not (view_name in views):
406 views.append(view_name)
407 displays.append(display)
409 # Defining the set of *views* and *displays* useful in a *GUI* context.
411 display_name = 'ACES'
412 displays.append(display_name)
414 display_names = sorted(config_data['displays'])
416 # Make sure the default display is first
417 default_display = config_data['defaultDisplay']
418 display_names.insert(0, display_names.pop(display_names.index(default_display)))
420 for display in display_names:
421 view_list = config_data['displays'][display]
422 for view_name, colorspace in view_list.iteritems():
423 if view_name == 'Output Transform':
424 config.addDisplay(display_name, display, colorspace.name)
425 if not (display in views):
426 views.append(display)
428 # Works with Nuke Studio and Mari, but not Nuke
429 # display_name = 'Utility'
430 # displays.append(display_name)
432 linear_display_space_name = config_data['roles']['scene_linear']
433 log_display_space_name = config_data['roles']['compositing_log']
435 config.addDisplay(display_name, 'Linear', linear_display_space_name)
436 views.append('Linear')
437 config.addDisplay(display_name, 'Log', log_display_space_name)
440 # Setting the active *displays* and *views*.
441 config.setActiveDisplays(','.join(sorted(displays)))
442 config.setActiveViews(','.join(views))
444 set_config_default_roles(
446 color_picking=config_data['roles']['color_picking'],
447 color_timing=config_data['roles']['color_timing'],
448 compositing_log=config_data['roles']['compositing_log'],
449 data=config_data['roles']['data'],
450 default=config_data['roles']['default'],
451 matte_paint=config_data['roles']['matte_paint'],
452 reference=config_data['roles']['reference'],
453 scene_linear=config_data['roles']['scene_linear'],
454 texture_paint=config_data['roles']['texture_paint'])
461 def generate_LUTs(odt_info,
466 lut_resolution_1d=4096,
467 lut_resolution_3d=64,
475 Parameter description.
480 Colorspaces and transforms converting between those colorspaces and
481 the reference colorspace, *ACES*.
484 print('generateLUTs - begin')
487 # Initialize a few variables
488 config_data['displays'] = {}
489 config_data['colorSpaces'] = []
491 # -------------------------------------------------------------------------
492 # *ACES Color Spaces*
493 # -------------------------------------------------------------------------
499 aces_log_display_space,
501 aces_default_display) = aces.create_colorspaces(aces_ctl_directory,
510 config_data['referenceColorSpace'] = aces_reference
511 config_data['roles'] = aces_roles
513 for cs in aces_colorspaces:
514 config_data['colorSpaces'].append(cs)
516 for name, data in aces_displays.iteritems():
517 config_data['displays'][name] = data
519 config_data['defaultDisplay'] = aces_default_display
520 config_data['linearDisplaySpace'] = aces_reference
521 config_data['logDisplaySpace'] = aces_log_display_space
523 # -------------------------------------------------------------------------
524 # *Camera Input Transforms*
525 # -------------------------------------------------------------------------
527 # *ARRI Log-C* to *ACES*.
528 arri_colorSpaces = arri.create_colorspaces(lut_directory,
530 for cs in arri_colorSpaces:
531 config_data['colorSpaces'].append(cs)
533 # *Canon-Log* to *ACES*.
534 canon_colorspaces = canon.create_colorspaces(lut_directory,
536 for cs in canon_colorspaces:
537 config_data['colorSpaces'].append(cs)
539 # *GoPro Protune* to *ACES*.
540 gopro_colorspaces = gopro.create_colorspaces(lut_directory,
542 for cs in gopro_colorspaces:
543 config_data['colorSpaces'].append(cs)
545 # *Panasonic V-Log* to *ACES*.
546 panasonic_colorSpaces = panasonic.create_colorspaces(lut_directory,
548 for cs in panasonic_colorSpaces:
549 config_data['colorSpaces'].append(cs)
551 # *RED* colorspaces to *ACES*.
552 red_colorspaces = red.create_colorspaces(lut_directory,
554 for cs in red_colorspaces:
555 config_data['colorSpaces'].append(cs)
558 sony_colorSpaces = sony.create_colorspaces(lut_directory,
560 for cs in sony_colorSpaces:
561 config_data['colorSpaces'].append(cs)
563 # -------------------------------------------------------------------------
564 # General Color Spaces
565 # -------------------------------------------------------------------------
566 general_colorSpaces = general.create_colorspaces(lut_directory,
569 for cs in general_colorSpaces:
570 config_data['colorSpaces'].append(cs)
572 # The *Raw* color space
573 raw = general.create_raw()
574 config_data['colorSpaces'].append(raw)
576 # Override certain roles, for now
577 config_data['roles']['data'] = raw.name
578 config_data['roles']['reference'] = raw.name
579 config_data['roles']['texture_paint'] = raw.name
581 print('generateLUTs - end')
585 def generate_baked_LUTs(odt_info,
591 lut_resolution_shaper=1024):
598 Parameter description.
603 Return value description.
606 # Create two entries for ODTs that have full and legal range support
607 odt_info_C = dict(odt_info)
608 for odt_ctl_name, odt_values in odt_info.iteritems():
609 if odt_values['transformHasFullLegalSwitch']:
610 odt_name = odt_values['transformUserName']
612 odt_values_legal = dict(odt_values)
613 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
614 odt_info_C['%s - Legal' % odt_ctl_name] = odt_values_legal
616 odt_values_full = dict(odt_values)
617 odt_values_full['transformUserName'] = '%s - Full' % odt_name
618 odt_info_C['%s - Full' % odt_ctl_name] = odt_values_full
620 del (odt_info_C[odt_ctl_name])
622 # Generate appropriate LUTs for each ODT
623 for odt_ctl_name, odt_values in odt_info_C.iteritems():
624 odt_prefix = odt_values['transformUserNamePrefix']
625 odt_name = odt_values['transformUserName']
628 for input_space in ['ACEScc', 'ACESproxy']:
629 args = ['--iconfig', config_path,
631 '--inputspace', input_space]
632 args += ['--outputspace', '%s' % odt_name]
633 args += ['--description',
634 '%s - %s for %s data' % (odt_prefix,
637 args += ['--shaperspace', shaper_name,
638 '--shapersize', str(lut_resolution_shaper)]
639 args += ['--cubesize', str(lut_resolution_3d)]
642 os.path.join(baked_directory,
644 '%s for %s.icc' % (odt_name, input_space))]
646 bake_lut = Process(description='bake a LUT',
652 for input_space in ['ACEScc', 'ACESproxy']:
653 args = ['--iconfig', config_path,
655 '--inputspace', input_space]
656 args += ['--outputspace', '%s' % odt_name]
657 args += ['--description',
658 '%s - %s for %s data' % (
659 odt_prefix, odt_name, input_space)]
660 args += ['--shaperspace', shaper_name,
661 '--shapersize', str(lut_resolution_shaper)]
662 args += ['--cubesize', str(lut_resolution_3d)]
669 '%s for %s Flame.3dl' % (odt_name, input_space))]
670 bake_lut = Process(description='bake a LUT',
680 '%s for %s Lustre.3dl' % (odt_name, input_space))]
681 bake_lut = Process(description='bake a LUT',
687 for input_space in ['ACEScg', 'ACES2065-1']:
688 args = ['--iconfig', config_path,
690 '--inputspace', input_space]
691 args += ['--outputspace', '%s' % odt_name]
692 args += ['--description',
693 '%s - %s for %s data' % (
694 odt_prefix, odt_name, input_space)]
695 if input_space == 'ACEScg':
696 lin_shaper_name = '%s - AP1' % shaper_name
698 lin_shaper_name = shaper_name
699 args += ['--shaperspace', lin_shaper_name,
700 '--shapersize', str(lut_resolution_shaper)]
702 args += ['--cubesize', str(lut_resolution_3d)]
709 '%s for %s Maya.csp' % (odt_name, input_space))]
710 bake_lut = Process(description='bake a LUT',
720 '%s for %s Houdini.lut' % (odt_name, input_space))]
721 bake_lut = Process(description='bake a LUT',
727 def create_config_dir(config_directory, bake_secondary_LUTs):
734 Parameter description.
739 Return value description.
742 lut_directory = os.path.join(config_directory, 'luts')
743 dirs = [config_directory, lut_directory]
744 if bake_secondary_LUTs:
745 dirs.extend([os.path.join(config_directory, 'baked'),
746 os.path.join(config_directory, 'baked', 'flame'),
747 os.path.join(config_directory, 'baked', 'photoshop'),
748 os.path.join(config_directory, 'baked', 'houdini'),
749 os.path.join(config_directory, 'baked', 'lustre'),
750 os.path.join(config_directory, 'baked', 'maya')])
753 not os.path.exists(d) and os.mkdir(d)
758 def create_ACES_config(aces_ctl_directory,
760 lut_resolution_1d=4096,
761 lut_resolution_3d=64,
762 bake_secondary_LUTs=True,
765 Creates the ACES configuration.
770 Parameter description.
775 Return value description.
778 lut_directory = create_config_dir(config_directory, bake_secondary_LUTs)
780 odt_info = aces.get_ODTs_info(aces_ctl_directory)
781 lmt_info = aces.get_LMTs_info(aces_ctl_directory)
783 shaper_name = 'Output Shaper'
784 config_data = generate_LUTs(odt_info,
794 print('Creating "generic" config')
795 config = create_config(config_data)
799 os.path.join(config_directory, 'config.ocio'))
802 print('Creating "GUI" config')
803 gui_config = create_config(config_data, gui=True)
806 write_config(gui_config,
807 os.path.join(config_directory, 'config.ocio'))
809 if bake_secondary_LUTs:
810 generate_baked_LUTs(odt_info,
812 os.path.join(config_directory, 'baked'),
813 os.path.join(config_directory, 'config.ocio'),
828 Parameter description.
833 Return value description.
838 p = optparse.OptionParser(description='An OCIO config generation script',
839 prog='createACESConfig',
840 version='createACESConfig 0.1',
841 usage='%prog [options]')
842 p.add_option('--acesCTLDir', '-a', default=os.environ.get(
843 ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
844 p.add_option('--configDir', '-c', default=os.environ.get(
845 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
846 p.add_option('--lutResolution1d', default=4096)
847 p.add_option('--lutResolution3d', default=64)
848 p.add_option('--dontBakeSecondaryLUTs', action='store_true')
849 p.add_option('--keepTempImages', action='store_true')
851 options, arguments = p.parse_args()
853 aces_ctl_directory = options.acesCTLDir
854 config_directory = options.configDir
855 lut_resolution_1d = int(options.lutResolution1d)
856 lut_resolution_3d = int(options.lutResolution3d)
857 bake_secondary_luts = not options.dontBakeSecondaryLUTs
858 cleanup_temp_images = not options.keepTempImages
860 # TODO: Investigate the following statements.
862 args_start = sys.argv.index('--') + 1
863 args = sys.argv[args_start:]
865 args_start = len(sys.argv) + 1
868 print('command line : \n%s\n' % ' '.join(sys.argv))
870 assert aces_ctl_directory is not None, (
871 'process: No "{0}" environment variable defined or no "ACES CTL" '
872 'directory specified'.format(
873 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
875 assert config_directory is not None, (
876 'process: No "{0}" environment variable defined or no configuration '
877 'directory specified'.format(
878 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
880 return create_ACES_config(aces_ctl_directory,
888 if __name__ == '__main__':