2 # -*- coding: utf-8 -*-
5 Defines objects creating the *ACES* configuration.
8 from __future__ import division
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.process import Process
24 __author__ = 'ACES Developers'
25 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
27 __maintainer__ = 'ACES Developers'
28 __email__ = 'aces@oscars.org'
29 __status__ = 'Production'
31 __all__ = ['ACES_OCIO_CTL_DIRECTORY_ENVIRON',
32 'ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON',
33 'set_config_default_roles',
35 'generate_OCIO_transform',
36 'add_colorspace_alias',
39 'generate_baked_LUTs',
44 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
45 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
48 def set_config_default_roles(config,
59 Sets given *OCIO* configuration default roles.
65 color_picking : str or unicode
66 Color picking role title.
67 color_timing : str or unicode
68 Color timing role title.
69 compositing_log : str or unicode
70 Compositing log role title.
73 default : str or unicode
75 matte_paint : str or unicode
76 Matte painting role title.
77 reference : str or unicode
79 scene_linear : str or unicode
80 Scene linear role title.
81 texture_paint : str or unicode
82 Texture painting role title.
91 config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
93 config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
95 config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
97 config.setRole(ocio.Constants.ROLE_DATA, data)
99 config.setRole(ocio.Constants.ROLE_DEFAULT, default)
101 config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
103 config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
105 config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
107 config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
112 def write_config(config, config_path, sanity_check=True):
114 Writes the configuration to given path.
119 Parameter description.
124 Return value description.
132 print 'Configuration was not written due to a failed Sanity Check'
135 with open(config_path, mode='w') as fp:
136 fp.write(config.serialize())
139 def generate_OCIO_transform(transforms):
146 Parameter description.
151 Return value description.
154 interpolation_options = {
155 'linear': ocio.Constants.INTERP_LINEAR,
156 'nearest': ocio.Constants.INTERP_NEAREST,
157 'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL}
159 direction_options = {
160 'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
161 'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE}
165 for transform in transforms:
168 if transform['type'] == 'lutFile':
169 ocio_transform = ocio.FileTransform(
170 src=transform['path'],
171 interpolation=interpolation_options[
172 transform['interpolation']],
173 direction=direction_options[transform['direction']])
174 ocio_transforms.append(ocio_transform)
177 elif transform['type'] == 'matrix':
178 ocio_transform = ocio.MatrixTransform()
179 # MatrixTransform member variables can't be initialized directly.
180 # Each must be set individually.
181 ocio_transform.setMatrix(transform['matrix'])
183 if 'offset' in transform:
184 ocio_transform.setOffset(transform['offset'])
186 if 'direction' in transform:
187 ocio_transform.setDirection(
188 direction_options[transform['direction']])
190 ocio_transforms.append(ocio_transform)
193 elif transform['type'] == 'exponent':
194 ocio_transform = ocio.ExponentTransform()
195 ocio_transform.setValue(transform['value'])
196 ocio_transforms.append(ocio_transform)
199 elif transform['type'] == 'log':
200 ocio_transform = ocio.LogTransform(
201 base=transform['base'],
202 direction=direction_options[transform['direction']])
204 ocio_transforms.append(ocio_transform)
206 # color space transform
207 elif transform['type'] == 'colorspace':
208 ocio_transform = ocio.ColorSpaceTransform(src=transform['src'],
209 dst=transform['dst'],
213 ocio_transforms.append(ocio_transform)
216 print("Ignoring unknown transform type : %s" % transform['type'])
218 if len(ocio_transforms) > 1:
219 group_transform = ocio.GroupTransform()
220 for transform in ocio_transforms:
221 group_transform.push_back(transform)
222 transform = group_transform
224 transform = ocio_transforms[0]
229 def add_colorspace_alias(config,
230 reference_colorspace,
232 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 not 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'}])
273 ocio_colorspace_alias.setTransform(
275 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
277 if not colorspace.from_reference_transforms:
278 print('Generating From-Reference transforms')
279 ocio_transform = generate_OCIO_transform(
280 [{'type': 'colorspace',
281 'src': reference_colorspace.name,
282 'dst': colorspace.name,
283 'direction': 'forward'}])
284 ocio_colorspace_alias.setTransform(
286 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
288 config.addColorSpace(ocio_colorspace_alias)
291 def create_config(config_data, nuke=False):
298 Parameter description.
303 Return value description.
306 # Creating the *OCIO* configuration.
307 config = ocio.Config()
309 # Setting configuration overall values.
310 config.setDescription('An ACES config generated from python')
311 config.setSearchPath('luts')
313 # Defining the reference colorspace.
314 reference_data = config_data['referenceColorSpace']
315 print('Adding the reference color space : %s' % reference_data.name)
317 reference = ocio.ColorSpace(
318 name=reference_data.name,
319 bitDepth=reference_data.bit_depth,
320 description=reference_data.description,
321 equalityGroup=reference_data.equality_group,
322 family=reference_data.family,
323 isData=reference_data.is_data,
324 allocation=reference_data.allocation_type,
325 allocationVars=reference_data.allocation_vars)
327 config.addColorSpace(reference)
330 if reference_data.aliases != []:
331 add_colorspace_alias(config, reference_data,
332 reference_data, reference_data.aliases)
336 # Creating the remaining colorspaces.
337 for colorspace in sorted(config_data['colorSpaces']):
338 print('Creating new color space : %s' % colorspace.name)
340 ocio_colorspace = ocio.ColorSpace(
341 name=colorspace.name,
342 bitDepth=colorspace.bit_depth,
343 description=colorspace.description,
344 equalityGroup=colorspace.equality_group,
345 family=colorspace.family,
346 isData=colorspace.is_data,
347 allocation=colorspace.allocation_type,
348 allocationVars=colorspace.allocation_vars)
350 if colorspace.to_reference_transforms:
351 print('Generating To-Reference transforms')
352 ocio_transform = generate_OCIO_transform(
353 colorspace.to_reference_transforms)
354 ocio_colorspace.setTransform(
356 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
358 if colorspace.from_reference_transforms:
359 print('Generating From-Reference transforms')
360 ocio_transform = generate_OCIO_transform(
361 colorspace.from_reference_transforms)
362 ocio_colorspace.setTransform(
364 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
366 config.addColorSpace(ocio_colorspace)
369 # Add alias to normal colorspace, using compact name
371 if colorspace.aliases != []:
372 add_colorspace_alias(config, reference_data,
373 colorspace, colorspace.aliases)
377 # Defining the *views* and *displays*.
381 # Defining a *generic* *display* and *view* setup.
383 for display, view_list in config_data['displays'].iteritems():
384 for view_name, colorspace in view_list.iteritems():
385 config.addDisplay(display, view_name, colorspace.name)
386 if not (view_name in views):
387 views.append(view_name)
388 displays.append(display)
390 # Defining the *Nuke* specific set of *views* and *displays*.
392 for display, view_list in config_data['displays'].iteritems():
393 for view_name, colorspace in view_list.iteritems():
394 if view_name == 'Output Transform':
396 config.addDisplay(display, view_name, colorspace.name)
397 if not (view_name in views):
398 views.append(view_name)
399 displays.append(display)
401 linear_display_space_name = config_data['linearDisplaySpace'].name
402 log_display_space_name = config_data['logDisplaySpace'].name
404 config.addDisplay('linear', 'View', linear_display_space_name)
405 displays.append('linear')
406 config.addDisplay('log', 'View', log_display_space_name)
407 displays.append('log')
409 # Setting the active *displays* and *views*.
410 config.setActiveDisplays(','.join(sorted(displays)))
411 config.setActiveViews(','.join(views))
413 set_config_default_roles(
415 color_picking=reference.getName(),
416 color_timing=reference.getName(),
417 compositing_log=reference.getName(),
418 data=reference.getName(),
419 default=reference.getName(),
420 matte_paint=reference.getName(),
421 reference=reference.getName(),
422 scene_linear=reference.getName(),
423 texture_paint=reference.getName())
430 def generate_LUTs(odt_info,
435 lut_resolution_1d=4096,
436 lut_resolution_3d=64,
444 Parameter description.
449 Colorspaces and transforms converting between those colorspaces and
450 the reference colorspace, *ACES*.
453 print('generateLUTs - begin')
456 # Initialize a few variables
457 config_data['displays'] = {}
458 config_data['colorSpaces'] = []
460 # -------------------------------------------------------------------------
461 # *ACES Color Spaces*
462 # -------------------------------------------------------------------------
468 aces_log_display_space) = aces.create_colorspaces(aces_ctl_directory,
477 config_data['referenceColorSpace'] = aces_reference
479 for cs in aces_colorspaces:
480 config_data['colorSpaces'].append(cs)
482 for name, data in aces_displays.iteritems():
483 config_data['displays'][name] = data
485 config_data['linearDisplaySpace'] = aces_reference
486 config_data['logDisplaySpace'] = aces_log_display_space
488 # -------------------------------------------------------------------------
489 # *Camera Input Transforms*
490 # -------------------------------------------------------------------------
493 arri_colorSpaces = arri.create_colorspaces(lut_directory,
495 for cs in arri_colorSpaces:
496 config_data['colorSpaces'].append(cs)
498 # *Canon-Log* to *ACES*.
499 canon_colorspaces = canon.create_colorspaces(lut_directory,
501 for cs in canon_colorspaces:
502 config_data['colorSpaces'].append(cs)
504 # *RED* colorspaces to *ACES*.
505 red_colorspaces = red.create_colorspaces(lut_directory,
507 for cs in red_colorspaces:
508 config_data['colorSpaces'].append(cs)
511 sony_colorSpaces = sony.create_colorspaces(lut_directory,
513 for cs in sony_colorSpaces:
514 config_data['colorSpaces'].append(cs)
516 # -------------------------------------------------------------------------
517 # General Color Spaces
518 # -------------------------------------------------------------------------
519 general_colorSpaces = general.create_colorspaces(lut_directory,
522 for cs in general_colorSpaces:
523 config_data['colorSpaces'].append(cs)
525 print('generateLUTs - end')
529 def generate_baked_LUTs(odt_info,
535 lut_resolution_shaper=1024):
542 Parameter description.
547 Return value description.
550 # Create two entries for ODTs that have full and legal range support
551 odt_info_C = dict(odt_info)
552 for odt_ctl_name, odt_values in odt_info.iteritems():
553 if odt_values['transformHasFullLegalSwitch']:
554 odt_name = odt_values['transformUserName']
556 odt_values_legal = dict(odt_values)
557 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
558 odt_info_C['%s - Legal' % odt_ctl_name] = odt_values_legal
560 odt_values_full = dict(odt_values)
561 odt_values_full['transformUserName'] = '%s - Full' % odt_name
562 odt_info_C['%s - Full' % odt_ctl_name] = odt_values_full
564 del (odt_info_C[odt_ctl_name])
566 # Generate appropriate LUTs for each ODT
567 for odt_ctl_name, odt_values in odt_info_C.iteritems():
568 odt_prefix = odt_values['transformUserNamePrefix']
569 odt_name = odt_values['transformUserName']
572 for input_space in ['ACEScc', 'ACESproxy']:
573 args = ['--iconfig', config_path,
575 '--inputspace', input_space]
576 args += ['--outputspace', '%s' % odt_name]
577 args += ['--description',
578 '%s - %s for %s data' % (odt_prefix,
581 args += ['--shaperspace', shaper_name,
582 '--shapersize', str(lut_resolution_shaper)]
583 args += ['--cubesize', str(lut_resolution_3d)]
586 os.path.join(baked_directory,
588 '%s for %s.icc' % (odt_name, input_space))]
590 bake_lut = Process(description='bake a LUT',
596 for input_space in ['ACEScc', 'ACESproxy']:
597 args = ['--iconfig', config_path,
599 '--inputspace', input_space]
600 args += ['--outputspace', '%s' % odt_name]
601 args += ['--description',
602 '%s - %s for %s data' % (
603 odt_prefix, odt_name, input_space)]
604 args += ['--shaperspace', shaper_name,
605 '--shapersize', str(lut_resolution_shaper)]
606 args += ['--cubesize', str(lut_resolution_3d)]
613 '%s for %s Flame.3dl' % (odt_name, input_space))]
614 bake_lut = Process(description='bake a LUT',
624 '%s for %s Lustre.3dl' % (odt_name, input_space))]
625 bake_lut = Process(description='bake a LUT',
631 for input_space in ['ACEScg', 'ACES2065-1']:
632 args = ['--iconfig', config_path,
634 '--inputspace', input_space]
635 args += ['--outputspace', '%s' % odt_name]
636 args += ['--description',
637 '%s - %s for %s data' % (
638 odt_prefix, odt_name, input_space)]
639 if input_space == 'ACEScg':
640 lin_shaper_name = '%s - AP1' % shaper_name
642 lin_shaper_name = shaper_name
643 args += ['--shaperspace', lin_shaper_name,
644 '--shapersize', str(lut_resolution_shaper)]
646 args += ['--cubesize', str(lut_resolution_3d)]
653 '%s for %s Maya.csp' % (odt_name, input_space))]
654 bake_lut = Process(description='bake a LUT',
664 '%s for %s Houdini.lut' % (odt_name, input_space))]
665 bake_lut = Process(description='bake a LUT',
671 def create_config_dir(config_directory, bake_secondary_LUTs):
678 Parameter description.
683 Return value description.
686 lut_directory = os.path.join(config_directory, 'luts')
687 dirs = [config_directory, lut_directory]
688 if bake_secondary_LUTs:
689 dirs.extend([os.path.join(config_directory, 'baked'),
690 os.path.join(config_directory, 'baked', 'flame'),
691 os.path.join(config_directory, 'baked', 'photoshop'),
692 os.path.join(config_directory, 'baked', 'houdini'),
693 os.path.join(config_directory, 'baked', 'lustre'),
694 os.path.join(config_directory, 'baked', 'maya')])
697 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_ODTs_info(aces_ctl_directory)
725 lmt_info = aces.get_LMTs_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__':