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 #print( "color spaces : %s" % [x.name for x in sorted(config_data['colorSpaces'])])
355 # Creating the remaining colorspaces.
356 for colorspace in sorted(config_data['colorSpaces']):
357 print('Creating new color space : %s' % colorspace.name)
359 ocio_colorspace = ocio.ColorSpace(
360 name=colorspace.name,
361 bitDepth=colorspace.bit_depth,
362 description=colorspace.description,
363 equalityGroup=colorspace.equality_group,
364 family=colorspace.family,
365 isData=colorspace.is_data,
366 allocation=colorspace.allocation_type,
367 allocationVars=colorspace.allocation_vars)
369 if colorspace.to_reference_transforms:
370 print('Generating To-Reference transforms')
371 ocio_transform = generate_OCIO_transform(
372 colorspace.to_reference_transforms)
373 ocio_colorspace.setTransform(
375 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
377 if colorspace.from_reference_transforms:
378 print('Generating From-Reference transforms')
379 ocio_transform = generate_OCIO_transform(
380 colorspace.from_reference_transforms)
381 ocio_colorspace.setTransform(
383 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
385 config.addColorSpace(ocio_colorspace)
388 # Add alias to normal colorspace, using compact name
391 if colorspace.aliases != []:
392 #print('Adding alias color spaces : %s' % colorspace.aliases)
393 add_colorspace_alias(config, reference_data,
394 colorspace, colorspace.aliases)
398 # Defining the *views* and *displays*.
402 # Defining a *generic* *display* and *view* setup.
404 for display, view_list in config_data['displays'].iteritems():
405 for view_name, colorspace in view_list.iteritems():
406 config.addDisplay(display, view_name, colorspace.name)
407 if not (view_name in views):
408 views.append(view_name)
409 displays.append(display)
411 # Defining the set of *views* and *displays* useful in a *GUI* context.
413 display_name = 'ACES'
414 displays.append(display_name)
416 display_names = sorted(config_data['displays'])
418 # Make sure the default display is first
419 default_display = config_data['defaultDisplay']
420 display_names.insert(0, display_names.pop(display_names.index(default_display)))
422 for display in display_names:
423 view_list = config_data['displays'][display]
424 for view_name, colorspace in view_list.iteritems():
425 if view_name == 'Output Transform':
426 config.addDisplay(display_name, display, colorspace.name)
427 if not (display in views):
428 views.append(display)
430 # Works with Nuke Studio and Mari, but not Nuke
431 # display_name = 'Utility'
432 # displays.append(display_name)
434 linear_display_space_name = config_data['roles']['scene_linear']
435 log_display_space_name = config_data['roles']['compositing_log']
437 config.addDisplay(display_name, 'Linear', linear_display_space_name)
438 views.append('Linear')
439 config.addDisplay(display_name, 'Log', log_display_space_name)
442 # Setting the active *displays* and *views*.
443 config.setActiveDisplays(','.join(sorted(displays)))
444 config.setActiveViews(','.join(views))
446 set_config_default_roles(
448 color_picking=config_data['roles']['color_picking'],
449 color_timing=config_data['roles']['color_timing'],
450 compositing_log=config_data['roles']['compositing_log'],
451 data=config_data['roles']['data'],
452 default=config_data['roles']['default'],
453 matte_paint=config_data['roles']['matte_paint'],
454 reference=config_data['roles']['reference'],
455 scene_linear=config_data['roles']['scene_linear'],
456 texture_paint=config_data['roles']['texture_paint'])
463 def generate_LUTs(odt_info,
468 lut_resolution_1d=4096,
469 lut_resolution_3d=64,
477 Parameter description.
482 Colorspaces and transforms converting between those colorspaces and
483 the reference colorspace, *ACES*.
486 print('generateLUTs - begin')
489 # Initialize a few variables
490 config_data['displays'] = {}
491 config_data['colorSpaces'] = []
493 # -------------------------------------------------------------------------
494 # *ACES Color Spaces*
495 # -------------------------------------------------------------------------
501 aces_log_display_space,
503 aces_default_display) = aces.create_colorspaces(aces_ctl_directory,
512 config_data['referenceColorSpace'] = aces_reference
513 config_data['roles'] = aces_roles
515 for cs in aces_colorspaces:
516 config_data['colorSpaces'].append(cs)
518 for name, data in aces_displays.iteritems():
519 config_data['displays'][name] = data
521 config_data['defaultDisplay'] = aces_default_display
522 config_data['linearDisplaySpace'] = aces_reference
523 config_data['logDisplaySpace'] = aces_log_display_space
525 # -------------------------------------------------------------------------
526 # *Camera Input Transforms*
527 # -------------------------------------------------------------------------
529 # *ARRI Log-C* to *ACES*.
530 arri_colorSpaces = arri.create_colorspaces(lut_directory,
532 for cs in arri_colorSpaces:
533 config_data['colorSpaces'].append(cs)
535 # *Canon-Log* to *ACES*.
536 canon_colorspaces = canon.create_colorspaces(lut_directory,
538 for cs in canon_colorspaces:
539 config_data['colorSpaces'].append(cs)
541 # *GoPro Protune* to *ACES*.
542 gopro_colorspaces = gopro.create_colorspaces(lut_directory,
544 for cs in gopro_colorspaces:
545 config_data['colorSpaces'].append(cs)
547 # *Panasonic V-Log* to *ACES*.
548 panasonic_colorSpaces = panasonic.create_colorspaces(lut_directory,
550 for cs in panasonic_colorSpaces:
551 config_data['colorSpaces'].append(cs)
553 # *RED* colorspaces to *ACES*.
554 red_colorspaces = red.create_colorspaces(lut_directory,
556 for cs in red_colorspaces:
557 config_data['colorSpaces'].append(cs)
560 sony_colorSpaces = sony.create_colorspaces(lut_directory,
562 for cs in sony_colorSpaces:
563 config_data['colorSpaces'].append(cs)
565 # -------------------------------------------------------------------------
566 # General Color Spaces
567 # -------------------------------------------------------------------------
568 general_colorSpaces = general.create_colorspaces(lut_directory,
571 for cs in general_colorSpaces:
572 config_data['colorSpaces'].append(cs)
574 # The *Raw* color space
575 raw = general.create_raw()
576 config_data['colorSpaces'].append(raw)
578 # Override certain roles, for now
579 config_data['roles']['data'] = raw.name
580 config_data['roles']['reference'] = raw.name
581 config_data['roles']['texture_paint'] = raw.name
583 print('generateLUTs - end')
587 def generate_baked_LUTs(odt_info,
593 lut_resolution_shaper=1024):
600 Parameter description.
605 Return value description.
608 # Create two entries for ODTs that have full and legal range support
609 odt_info_C = dict(odt_info)
610 for odt_ctl_name, odt_values in odt_info.iteritems():
611 if odt_values['transformHasFullLegalSwitch']:
612 odt_name = odt_values['transformUserName']
614 odt_values_legal = dict(odt_values)
615 odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
616 odt_info_C['%s - Legal' % odt_ctl_name] = odt_values_legal
618 odt_values_full = dict(odt_values)
619 odt_values_full['transformUserName'] = '%s - Full' % odt_name
620 odt_info_C['%s - Full' % odt_ctl_name] = odt_values_full
622 del (odt_info_C[odt_ctl_name])
624 # Generate appropriate LUTs for each ODT
625 for odt_ctl_name, odt_values in odt_info_C.iteritems():
626 odt_prefix = odt_values['transformUserNamePrefix']
627 odt_name = odt_values['transformUserName']
630 for input_space in ['ACEScc', 'ACESproxy']:
631 args = ['--iconfig', config_path,
633 '--inputspace', input_space]
634 args += ['--outputspace', '%s' % odt_name]
635 args += ['--description',
636 '%s - %s for %s data' % (odt_prefix,
639 args += ['--shaperspace', shaper_name,
640 '--shapersize', str(lut_resolution_shaper)]
641 args += ['--cubesize', str(lut_resolution_3d)]
644 os.path.join(baked_directory,
646 '%s for %s.icc' % (odt_name, input_space))]
648 bake_lut = Process(description='bake a LUT',
654 for input_space in ['ACEScc', 'ACESproxy']:
655 args = ['--iconfig', config_path,
657 '--inputspace', input_space]
658 args += ['--outputspace', '%s' % odt_name]
659 args += ['--description',
660 '%s - %s for %s data' % (
661 odt_prefix, odt_name, input_space)]
662 args += ['--shaperspace', shaper_name,
663 '--shapersize', str(lut_resolution_shaper)]
664 args += ['--cubesize', str(lut_resolution_3d)]
671 '%s for %s Flame.3dl' % (odt_name, input_space))]
672 bake_lut = Process(description='bake a LUT',
682 '%s for %s Lustre.3dl' % (odt_name, input_space))]
683 bake_lut = Process(description='bake a LUT',
689 for input_space in ['ACEScg', 'ACES2065-1']:
690 args = ['--iconfig', config_path,
692 '--inputspace', input_space]
693 args += ['--outputspace', '%s' % odt_name]
694 args += ['--description',
695 '%s - %s for %s data' % (
696 odt_prefix, odt_name, input_space)]
697 if input_space == 'ACEScg':
698 lin_shaper_name = '%s - AP1' % shaper_name
700 lin_shaper_name = shaper_name
701 args += ['--shaperspace', lin_shaper_name,
702 '--shapersize', str(lut_resolution_shaper)]
704 args += ['--cubesize', str(lut_resolution_3d)]
711 '%s for %s Maya.csp' % (odt_name, input_space))]
712 bake_lut = Process(description='bake a LUT',
722 '%s for %s Houdini.lut' % (odt_name, input_space))]
723 bake_lut = Process(description='bake a LUT',
729 def create_config_dir(config_directory, bake_secondary_LUTs):
736 Parameter description.
741 Return value description.
744 lut_directory = os.path.join(config_directory, 'luts')
745 dirs = [config_directory, lut_directory]
746 if bake_secondary_LUTs:
747 dirs.extend([os.path.join(config_directory, 'baked'),
748 os.path.join(config_directory, 'baked', 'flame'),
749 os.path.join(config_directory, 'baked', 'photoshop'),
750 os.path.join(config_directory, 'baked', 'houdini'),
751 os.path.join(config_directory, 'baked', 'lustre'),
752 os.path.join(config_directory, 'baked', 'maya')])
755 not os.path.exists(d) and os.mkdir(d)
760 def create_ACES_config(aces_ctl_directory,
762 lut_resolution_1d=4096,
763 lut_resolution_3d=64,
764 bake_secondary_LUTs=True,
767 Creates the ACES configuration.
772 Parameter description.
777 Return value description.
780 lut_directory = create_config_dir(config_directory, bake_secondary_LUTs)
782 odt_info = aces.get_ODTs_info(aces_ctl_directory)
783 lmt_info = aces.get_LMTs_info(aces_ctl_directory)
785 shaper_name = 'Output Shaper'
786 config_data = generate_LUTs(odt_info,
796 print('Creating "generic" config')
797 config = create_config(config_data)
801 os.path.join(config_directory, 'config.ocio'))
804 print('Creating "GUI" config')
805 gui_config = create_config(config_data, gui=True)
808 write_config(gui_config,
809 os.path.join(config_directory, 'config.ocio'))
811 if bake_secondary_LUTs:
812 generate_baked_LUTs(odt_info,
814 os.path.join(config_directory, 'baked'),
815 os.path.join(config_directory, 'config.ocio'),
830 Parameter description.
835 Return value description.
840 p = optparse.OptionParser(description='An OCIO config generation script',
841 prog='createACESConfig',
842 version='createACESConfig 0.1',
843 usage='%prog [options]')
844 p.add_option('--acesCTLDir', '-a', default=os.environ.get(
845 ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
846 p.add_option('--configDir', '-c', default=os.environ.get(
847 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
848 p.add_option('--lutResolution1d', default=4096)
849 p.add_option('--lutResolution3d', default=64)
850 p.add_option('--dontBakeSecondaryLUTs', action='store_true')
851 p.add_option('--keepTempImages', action='store_true')
853 options, arguments = p.parse_args()
855 aces_ctl_directory = options.acesCTLDir
856 config_directory = options.configDir
857 lut_resolution_1d = int(options.lutResolution1d)
858 lut_resolution_3d = int(options.lutResolution3d)
859 bake_secondary_luts = not options.dontBakeSecondaryLUTs
860 cleanup_temp_images = not options.keepTempImages
862 # TODO: Investigate the following statements.
864 args_start = sys.argv.index('--') + 1
865 args = sys.argv[args_start:]
867 args_start = len(sys.argv) + 1
870 print('command line : \n%s\n' % ' '.join(sys.argv))
872 assert aces_ctl_directory is not None, (
873 'process: No "{0}" environment variable defined or no "ACES CTL" '
874 'directory specified'.format(
875 ACES_OCIO_CTL_DIRECTORY_ENVIRON))
877 assert config_directory is not None, (
878 'process: No "{0}" environment variable defined or no configuration '
879 'directory specified'.format(
880 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
882 return create_ACES_config(aces_ctl_directory,
890 if __name__ == '__main__':