The working space is now ACEScg. The scene_linear, rendering and compositing_linear...
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / aces_config.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Defines objects creating the *ACES* configuration.
6 """
7
8 from __future__ import division
9
10 import os
11 import sys
12
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
23
24 from aces_ocio.utilities import replace
25
26 __author__ = 'ACES Developers'
27 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
28 __license__ = ''
29 __maintainer__ = 'ACES Developers'
30 __email__ = 'aces@oscars.org'
31 __status__ = 'Production'
32
33 __all__ = ['ACES_OCIO_CTL_DIRECTORY_ENVIRON',
34            'ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON',
35            'set_config_default_roles',
36            'write_config',
37            'generate_OCIO_transform',
38            'add_colorspace_alias',
39            'create_config',
40            'generate_LUTs',
41            'generate_baked_LUTs',
42            'create_config_dir',
43            'create_ACES_config',
44            'main']
45
46 ACES_OCIO_CTL_DIRECTORY_ENVIRON = 'ACES_OCIO_CTL_DIRECTORY'
47 ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON = 'ACES_OCIO_CONFIGURATION_DIRECTORY'
48
49
50 def set_config_default_roles(config,
51                              color_picking='',
52                              color_timing='',
53                              compositing_log='',
54                              data='',
55                              default='',
56                              matte_paint='',
57                              reference='',
58                              scene_linear='',
59                              texture_paint='',
60                              rendering='',
61                              compositing_linear=''):
62     """
63     Sets given *OCIO* configuration default roles.
64
65     Parameters
66     ----------
67     config : config
68         *OCIO* configuration.
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.
75     data : str or unicode
76         Data role title.
77     default : str or unicode
78         Default role title.
79     matte_paint : str or unicode
80         Matte painting role title.
81     reference : str or unicode
82         Reference role title.
83     scene_linear : str or unicode
84         Scene linear role title.
85     texture_paint : str or unicode
86         Texture painting role title.
87
88     Returns
89     -------
90     bool
91          Definition success.
92     """
93
94     if color_picking:
95         config.setRole(ocio.Constants.ROLE_COLOR_PICKING, color_picking)
96     if color_timing:
97         config.setRole(ocio.Constants.ROLE_COLOR_TIMING, color_timing)
98     if compositing_log:
99         config.setRole(ocio.Constants.ROLE_COMPOSITING_LOG, compositing_log)
100     if data:
101         config.setRole(ocio.Constants.ROLE_DATA, data)
102     if default:
103         config.setRole(ocio.Constants.ROLE_DEFAULT, default)
104     if matte_paint:
105         config.setRole(ocio.Constants.ROLE_MATTE_PAINT, matte_paint)
106     if reference:
107         config.setRole(ocio.Constants.ROLE_REFERENCE, reference)
108     if texture_paint:
109         config.setRole(ocio.Constants.ROLE_TEXTURE_PAINT, texture_paint)
110
111     # 'rendering' and 'compositing_linear' roles default to the 'scene_linear'
112     # value if not set explicitly
113     if rendering:
114         config.setRole("rendering", rendering)
115     if compositing_linear:
116         config.setRole("compositing_linear", compositing_linear)
117     if scene_linear:
118         config.setRole(ocio.Constants.ROLE_SCENE_LINEAR, scene_linear)
119         if not rendering:
120             config.setRole("rendering", scene_linear)
121         if not compositing_linear:
122             config.setRole("compositing_linear", scene_linear)
123
124     return True
125
126
127 def write_config(config, config_path, sanity_check=True):
128     """
129     Writes the configuration to given path.
130
131     Parameters
132     ----------
133     parameter : type
134         Parameter description.
135
136     Returns
137     -------
138     type
139          Return value description.
140     """
141
142     if sanity_check:
143         try:
144             config.sanityCheck()
145         except Exception, e:
146             print e
147             print 'Configuration was not written due to a failed Sanity Check'
148             return
149
150     with open(config_path, mode='w') as fp:
151         fp.write(config.serialize())
152
153
154 def generate_OCIO_transform(transforms):
155     """
156     Object description.
157
158     Parameters
159     ----------
160     parameter : type
161         Parameter description.
162
163     Returns
164     -------
165     type
166          Return value description.
167     """
168
169     interpolation_options = {
170         'linear': ocio.Constants.INTERP_LINEAR,
171         'nearest': ocio.Constants.INTERP_NEAREST,
172         'tetrahedral': ocio.Constants.INTERP_TETRAHEDRAL}
173
174     direction_options = {
175         'forward': ocio.Constants.TRANSFORM_DIR_FORWARD,
176         'inverse': ocio.Constants.TRANSFORM_DIR_INVERSE}
177
178     ocio_transforms = []
179
180     for transform in transforms:
181
182         # lutFile transform
183         if transform['type'] == 'lutFile':
184             ocio_transform = ocio.FileTransform(
185                 src=transform['path'],
186                 interpolation=interpolation_options[
187                     transform['interpolation']],
188                 direction=direction_options[transform['direction']])
189             ocio_transforms.append(ocio_transform)
190
191         # matrix transform
192         elif transform['type'] == 'matrix':
193             ocio_transform = ocio.MatrixTransform()
194             # MatrixTransform member variables can't be initialized directly.
195             # Each must be set individually.
196             ocio_transform.setMatrix(transform['matrix'])
197
198             if 'offset' in transform:
199                 ocio_transform.setOffset(transform['offset'])
200
201             if 'direction' in transform:
202                 ocio_transform.setDirection(
203                     direction_options[transform['direction']])
204
205             ocio_transforms.append(ocio_transform)
206
207         # exponent transform
208         elif transform['type'] == 'exponent':
209             ocio_transform = ocio.ExponentTransform()
210             ocio_transform.setValue(transform['value'])
211             ocio_transforms.append(ocio_transform)
212
213         # log transform
214         elif transform['type'] == 'log':
215             ocio_transform = ocio.LogTransform(
216                 base=transform['base'],
217                 direction=direction_options[transform['direction']])
218
219             ocio_transforms.append(ocio_transform)
220
221         # color space transform
222         elif transform['type'] == 'colorspace':
223             ocio_transform = ocio.ColorSpaceTransform(src=transform['src'],
224                                                       dst=transform['dst'],
225                                                       direction=
226                                                       direction_options[
227                                                           'forward'])
228             ocio_transforms.append(ocio_transform)
229         # unknown type
230         else:
231             print("Ignoring unknown transform type : %s" % transform['type'])
232
233     if len(ocio_transforms) > 1:
234         group_transform = ocio.GroupTransform()
235         for transform in ocio_transforms:
236             group_transform.push_back(transform)
237         transform = group_transform
238     else:
239         transform = ocio_transforms[0]
240
241     return transform
242
243
244 def add_colorspace_alias(config,
245                          reference_colorspace,
246                          colorspace,
247                          colorspace_alias_names):
248     """
249     Object description.
250
251     Parameters
252     ----------
253     parameter : type
254         Parameter description.
255
256     Returns
257     -------
258     type
259          Return value description.
260     """
261
262     for alias_name in colorspace_alias_names:
263         if alias_name.lower() == colorspace.name.lower():
264             print('Skipping alias creation for %s, alias %s, because lower cased names match' % (
265                 colorspace.name, alias_name) )
266             return
267
268         print('Adding alias colorspace space %s, alias to %s' % (
269             alias_name, colorspace.name))
270
271         compact_family_name = 'Aliases'
272
273         ocio_colorspace_alias = ocio.ColorSpace(
274             name=alias_name,
275             bitDepth=colorspace.bit_depth,
276             description=colorspace.description,
277             equalityGroup=colorspace.equality_group,
278             family=compact_family_name,
279             isData=colorspace.is_data,
280             allocation=colorspace.allocation_type,
281             allocationVars=colorspace.allocation_vars)
282
283         if colorspace.to_reference_transforms:
284             print('Generating To-Reference transforms')
285             ocio_transform = generate_OCIO_transform(
286                 [{'type': 'colorspace',
287                   'src': colorspace.name,
288                   'dst': reference_colorspace.name,
289                   'direction': 'forward'}])
290             ocio_colorspace_alias.setTransform(
291                 ocio_transform,
292                 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
293
294         if colorspace.from_reference_transforms:
295             print('Generating From-Reference transforms')
296             ocio_transform = generate_OCIO_transform(
297                 [{'type': 'colorspace',
298                   'src': reference_colorspace.name,
299                   'dst': colorspace.name,
300                   'direction': 'forward'}])
301             ocio_colorspace_alias.setTransform(
302                 ocio_transform,
303                 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
304
305         config.addColorSpace(ocio_colorspace_alias)
306
307 def colorspace_prefixed_name(colorspace):
308     prefix = colorspace.family.replace("/", " - ")
309     return "%s - %s" % (prefix, colorspace.name)
310
311 def create_config(config_data, aliases=False, prefix=False):
312     """
313     Object description.
314
315     Parameters
316     ----------
317     parameter : type
318         Parameter description.
319
320     Returns
321     -------
322     type
323          Return value description.
324     """
325
326     prefixed_names = {}
327     alias_colorspaces = []
328
329     # Creating the *OCIO* configuration.
330     config = ocio.Config()
331
332     # Setting configuration overall values.
333     config.setDescription('An ACES config generated from python')
334     config.setSearchPath('luts')
335
336     # Defining the reference colorspace.
337     reference_data = config_data['referenceColorSpace']
338
339     # Adding the color space Family into the name
340     # Helps with applications that present colorspaces as one long list
341     if prefix:
342         prefixed_name = colorspace_prefixed_name(reference_data)
343         prefixed_names[reference_data.name] = prefixed_name
344         reference_data.name = prefixed_name
345
346     print('Adding the reference color space : %s' % reference_data.name)
347
348     reference = ocio.ColorSpace(
349         name=reference_data.name,
350         bitDepth=reference_data.bit_depth,
351         description=reference_data.description,
352         equalityGroup=reference_data.equality_group,
353         family=reference_data.family,
354         isData=reference_data.is_data,
355         allocation=reference_data.allocation_type,
356         allocationVars=reference_data.allocation_vars)
357
358     config.addColorSpace(reference)
359
360     # Add alias
361     if aliases:
362         if reference_data.aliases != []:
363             #add_colorspace_alias(config, reference_data,
364             #                     reference_data, reference_data.aliases)
365             # defer adding alias colorspaces until end. Helps with some applications
366             alias_colorspaces.append([reference_data, reference_data, reference_data.aliases])
367
368
369     print("")
370
371     #print( "color spaces : %s" % [x.name for x in sorted(config_data['colorSpaces'])])
372
373     print('Adding the regular color spaces')
374
375     # Creating the remaining colorspaces.
376     for colorspace in sorted(config_data['colorSpaces']):
377         # Adding the color space Family into the name
378         # Helps with applications that present colorspaces as one long list
379         if prefix:
380             prefixed_name = colorspace_prefixed_name(colorspace)
381             prefixed_names[colorspace.name] = prefixed_name
382             colorspace.name = prefixed_name
383
384         print('Creating new color space : %s' % colorspace.name)
385
386         ocio_colorspace = ocio.ColorSpace(
387             name=colorspace.name,
388             bitDepth=colorspace.bit_depth,
389             description=colorspace.description,
390             equalityGroup=colorspace.equality_group,
391             family=colorspace.family,
392             isData=colorspace.is_data,
393             allocation=colorspace.allocation_type,
394             allocationVars=colorspace.allocation_vars)
395
396         if colorspace.to_reference_transforms:
397             print('Generating To-Reference transforms')
398             ocio_transform = generate_OCIO_transform(
399                 colorspace.to_reference_transforms)
400             ocio_colorspace.setTransform(
401                 ocio_transform,
402                 ocio.Constants.COLORSPACE_DIR_TO_REFERENCE)
403
404         if colorspace.from_reference_transforms:
405             print('Generating From-Reference transforms')
406             ocio_transform = generate_OCIO_transform(
407                 colorspace.from_reference_transforms)
408             ocio_colorspace.setTransform(
409                 ocio_transform,
410                 ocio.Constants.COLORSPACE_DIR_FROM_REFERENCE)
411
412         config.addColorSpace(ocio_colorspace)
413
414         #
415         # Add alias to normal colorspace, using compact name
416         #
417         if aliases:
418             if colorspace.aliases != []:
419                 #add_colorspace_alias(config, reference_data,
420                 #                     colorspace, colorspace.aliases)
421                 # defer adding alias colorspaces until end. Helps with some applications
422                 alias_colorspaces.append([reference_data, colorspace, colorspace.aliases])
423
424         print('')
425
426     print("")
427
428     # We add these at the end as some applications use the order of the colorspaces
429     # definitions in the config to order the colorspaces in their selection lists.
430     # Other go alphabetically. This should keep the alias colorspaces out of the way
431     # for the apps that use the order of definition in the config.
432     print('Adding the alias colorspaces')
433     for reference, colorspace, aliases in alias_colorspaces:
434         add_colorspace_alias(config, reference, colorspace, aliases)
435
436     print("")
437
438     print('Adding the diplays and views')
439
440     # Defining the *views* and *displays*.
441     displays = []
442     views = []
443
444     '''
445     # Defining a *generic* *display* and *view* setup.
446     if not gui:
447         for display, view_list in config_data['displays'].iteritems():
448             for view_name, colorspace in view_list.iteritems():
449                 config.addDisplay(display, view_name, colorspace.name)
450                 if not (view_name in views):
451                     views.append(view_name)
452             displays.append(display)
453
454     else:
455     '''
456     # Defining the set of *views* and *displays* useful in a *GUI* context.
457     #display_name = 'ACES'
458     display_name = config_data['roles']['scene_linear']
459     displays.append(display_name)
460
461     display_names = sorted(config_data['displays'])
462
463     # Make sure the default display is first
464     default_display = config_data['defaultDisplay']
465     display_names.insert(0, display_names.pop(display_names.index(default_display)))
466
467     for display in display_names:
468         view_list = config_data['displays'][display]
469         for view_name, colorspace in view_list.iteritems():
470             if view_name == 'Output Transform':
471                 display_cleaned = replace(display, {')': '', '(': ''})
472                 config.addDisplay(display_name, display_cleaned, colorspace.name)
473                 if not (display_cleaned in views):
474                     views.append(display_cleaned)
475
476     # Works with Nuke Studio and Mari, but not Nuke
477     # display_name = 'Utility'
478     # displays.append(display_name)
479
480     linear_display_space_name = config_data['roles']['scene_linear']
481     log_display_space_name = config_data['roles']['compositing_log']
482
483     # Find the newly-prefixed colorspace names
484     if prefix:
485         #print( prefixed_names )
486         linear_display_space_name = prefixed_names[linear_display_space_name]
487         log_display_space_name = prefixed_names[log_display_space_name]
488
489     config.addDisplay(display_name, 'Linear', linear_display_space_name)
490     views.append('Linear')
491     config.addDisplay(display_name, 'Log', log_display_space_name)
492     views.append('Log')
493
494     # Setting the active *displays* and *views*.
495     config.setActiveDisplays(','.join(sorted(displays)))
496     config.setActiveViews(','.join(views))
497
498     print("")
499
500     print('Setting the roles')
501
502     if prefix:
503         set_config_default_roles(
504             config,
505             color_picking=prefixed_names[config_data['roles']['color_picking']],
506             color_timing=prefixed_names[config_data['roles']['color_timing']],
507             compositing_log=prefixed_names[config_data['roles']['compositing_log']],
508             data=prefixed_names[config_data['roles']['data']],
509             default=prefixed_names[config_data['roles']['default']],
510             matte_paint=prefixed_names[config_data['roles']['matte_paint']],
511             reference=prefixed_names[config_data['roles']['reference']],
512             scene_linear=prefixed_names[config_data['roles']['scene_linear']],
513             texture_paint=prefixed_names[config_data['roles']['texture_paint']])
514     else:
515         set_config_default_roles(
516             config,
517             color_picking=config_data['roles']['color_picking'],
518             color_timing=config_data['roles']['color_timing'],
519             compositing_log=config_data['roles']['compositing_log'],
520             data=config_data['roles']['data'],
521             default=config_data['roles']['default'],
522             matte_paint=config_data['roles']['matte_paint'],
523             reference=config_data['roles']['reference'],
524             scene_linear=config_data['roles']['scene_linear'],
525             texture_paint=config_data['roles']['texture_paint'])
526
527     print("")
528
529     # Make sure we didn't create a bad config
530     config.sanityCheck()
531
532     # Reset the colorspace names back to their non-prefixed versions
533     if prefix:
534         # Build the reverse lookup
535         prefixed_names_inverse = {}
536         for original, prefixed in prefixed_names.iteritems():
537             prefixed_names_inverse[prefixed] = original
538
539         # Reet the reference colorspace name
540         reference_data.name = prefixed_names_inverse[reference_data.name]
541
542         # Reset the rest of the colorspace names
543         for colorspace in config_data['colorSpaces']:
544             colorspace.name = prefixed_names_inverse[colorspace.name]
545
546     return config
547
548
549 def generate_LUTs(odt_info,
550                   lmt_info,
551                   shaper_name,
552                   aces_ctl_directory,
553                   lut_directory,
554                   lut_resolution_1d=4096,
555                   lut_resolution_3d=64,
556                   cleanup=True):
557     """
558     Object description.
559
560     Parameters
561     ----------
562     parameter : type
563         Parameter description.
564
565     Returns
566     -------
567     dict
568          Colorspaces and transforms converting between those colorspaces and
569          the reference colorspace, *ACES*.
570     """
571
572     print('generateLUTs - begin')
573     config_data = {}
574
575     # Initialize a few variables
576     config_data['displays'] = {}
577     config_data['colorSpaces'] = []
578
579     # -------------------------------------------------------------------------
580     # *ACES Color Spaces*
581     # -------------------------------------------------------------------------
582
583     # *ACES* colorspaces
584     (aces_reference,
585      aces_colorspaces,
586      aces_displays,
587      aces_log_display_space,
588      aces_roles,
589      aces_default_display) = aces.create_colorspaces(aces_ctl_directory,
590                                                      lut_directory,
591                                                      lut_resolution_1d,
592                                                      lut_resolution_3d,
593                                                      lmt_info,
594                                                      odt_info,
595                                                      shaper_name,
596                                                      cleanup)
597
598     config_data['referenceColorSpace'] = aces_reference
599     config_data['roles'] = aces_roles
600
601     for cs in aces_colorspaces:
602         config_data['colorSpaces'].append(cs)
603
604     for name, data in aces_displays.iteritems():
605         config_data['displays'][name] = data
606
607     config_data['defaultDisplay'] = aces_default_display
608     config_data['linearDisplaySpace'] = aces_reference
609     config_data['logDisplaySpace'] = aces_log_display_space
610
611     # -------------------------------------------------------------------------
612     # *Camera Input Transforms*
613     # -------------------------------------------------------------------------
614
615     # *ARRI Log-C* to *ACES*.
616     arri_colorSpaces = arri.create_colorspaces(lut_directory,
617                                                lut_resolution_1d)
618     for cs in arri_colorSpaces:
619         config_data['colorSpaces'].append(cs)
620
621     # *Canon-Log* to *ACES*.
622     canon_colorspaces = canon.create_colorspaces(lut_directory,
623                                                  lut_resolution_1d)
624     for cs in canon_colorspaces:
625         config_data['colorSpaces'].append(cs)
626
627     # *GoPro Protune* to *ACES*.
628     gopro_colorspaces = gopro.create_colorspaces(lut_directory,
629                                                  lut_resolution_1d)
630     for cs in gopro_colorspaces:
631         config_data['colorSpaces'].append(cs)
632
633     # *Panasonic V-Log* to *ACES*.
634     panasonic_colorSpaces = panasonic.create_colorspaces(lut_directory,
635                                                          lut_resolution_1d)
636     for cs in panasonic_colorSpaces:
637         config_data['colorSpaces'].append(cs)
638
639     # *RED* colorspaces to *ACES*.
640     red_colorspaces = red.create_colorspaces(lut_directory,
641                                              lut_resolution_1d)
642     for cs in red_colorspaces:
643         config_data['colorSpaces'].append(cs)
644
645     # *S-Log* to *ACES*.
646     sony_colorSpaces = sony.create_colorspaces(lut_directory,
647                                                lut_resolution_1d)
648     for cs in sony_colorSpaces:
649         config_data['colorSpaces'].append(cs)
650
651     # -------------------------------------------------------------------------
652     # General Color Spaces
653     # -------------------------------------------------------------------------
654     general_colorSpaces = general.create_colorspaces(lut_directory,
655                                                      lut_resolution_1d,
656                                                      lut_resolution_3d)
657     for cs in general_colorSpaces:
658         config_data['colorSpaces'].append(cs)
659
660     # The *Raw* color space
661     raw = general.create_raw()
662     config_data['colorSpaces'].append(raw)
663
664     # Override certain roles, for now
665     config_data['roles']['data'] = raw.name
666     config_data['roles']['reference'] = raw.name
667     config_data['roles']['texture_paint'] = raw.name
668
669     print('generateLUTs - end')
670     return config_data
671
672
673 def generate_baked_LUTs(odt_info,
674                         shaper_name,
675                         baked_directory,
676                         config_path,
677                         lut_resolution_1d,
678                         lut_resolution_3d,
679                         lut_resolution_shaper=1024):
680     """
681     Object description.
682
683     Parameters
684     ----------
685     parameter : type
686         Parameter description.
687
688     Returns
689     -------
690     type
691          Return value description.
692     """
693
694     # Create two entries for ODTs that have full and legal range support
695     odt_info_C = dict(odt_info)
696     for odt_ctl_name, odt_values in odt_info.iteritems():
697         if odt_values['transformHasFullLegalSwitch']:
698             odt_name = odt_values['transformUserName']
699
700             odt_values_legal = dict(odt_values)
701             odt_values_legal['transformUserName'] = '%s - Legal' % odt_name
702             odt_info_C['%s - Legal' % odt_ctl_name] = odt_values_legal
703
704             odt_values_full = dict(odt_values)
705             odt_values_full['transformUserName'] = '%s - Full' % odt_name
706             odt_info_C['%s - Full' % odt_ctl_name] = odt_values_full
707
708             del (odt_info_C[odt_ctl_name])
709
710     # Generate appropriate LUTs for each ODT
711     for odt_ctl_name, odt_values in odt_info_C.iteritems():
712         odt_prefix = odt_values['transformUserNamePrefix']
713         odt_name = odt_values['transformUserName']
714
715         # *Photoshop*
716         for input_space in ['ACEScc', 'ACESproxy']:
717             args = ['--iconfig', config_path,
718                     '-v',
719                     '--inputspace', input_space]
720             args += ['--outputspace', '%s' % odt_name]
721             args += ['--description',
722                      '%s - %s for %s data' % (odt_prefix,
723                                               odt_name,
724                                               input_space)]
725             args += ['--shaperspace', shaper_name,
726                      '--shapersize', str(lut_resolution_shaper)]
727             args += ['--cubesize', str(lut_resolution_3d)]
728             args += ['--format',
729                      'icc',
730                      os.path.join(baked_directory,
731                                   'photoshop',
732                                   '%s for %s.icc' % (odt_name, input_space))]
733
734             bake_lut = Process(description='bake a LUT',
735                                cmd='ociobakelut',
736                                args=args)
737             bake_lut.execute()
738
739         # *Flame*, *Lustre*
740         for input_space in ['ACEScc', 'ACESproxy']:
741             args = ['--iconfig', config_path,
742                     '-v',
743                     '--inputspace', input_space]
744             args += ['--outputspace', '%s' % odt_name]
745             args += ['--description',
746                      '%s - %s for %s data' % (
747                          odt_prefix, odt_name, input_space)]
748             args += ['--shaperspace', shaper_name,
749                      '--shapersize', str(lut_resolution_shaper)]
750             args += ['--cubesize', str(lut_resolution_3d)]
751
752             fargs = ['--format',
753                      'flame',
754                      os.path.join(
755                          baked_directory,
756                          'flame',
757                          '%s for %s Flame.3dl' % (odt_name, input_space))]
758             bake_lut = Process(description='bake a LUT',
759                                cmd='ociobakelut',
760                                args=(args + fargs))
761             bake_lut.execute()
762
763             largs = ['--format',
764                      'lustre',
765                      os.path.join(
766                          baked_directory,
767                          'lustre',
768                          '%s for %s Lustre.3dl' % (odt_name, input_space))]
769             bake_lut = Process(description='bake a LUT',
770                                cmd='ociobakelut',
771                                args=(args + largs))
772             bake_lut.execute()
773
774         # *Maya*, *Houdini*
775         for input_space in ['ACEScg', 'ACES2065-1']:
776             args = ['--iconfig', config_path,
777                     '-v',
778                     '--inputspace', input_space]
779             args += ['--outputspace', '%s' % odt_name]
780             args += ['--description',
781                      '%s - %s for %s data' % (
782                          odt_prefix, odt_name, input_space)]
783             if input_space == 'ACEScg':
784                 lin_shaper_name = '%s - AP1' % shaper_name
785             else:
786                 lin_shaper_name = shaper_name
787             args += ['--shaperspace', lin_shaper_name,
788                      '--shapersize', str(lut_resolution_shaper)]
789
790             args += ['--cubesize', str(lut_resolution_3d)]
791
792             margs = ['--format',
793                      'cinespace',
794                      os.path.join(
795                          baked_directory,
796                          'maya',
797                          '%s for %s Maya.csp' % (odt_name, input_space))]
798             bake_lut = Process(description='bake a LUT',
799                                cmd='ociobakelut',
800                                args=(args + margs))
801             bake_lut.execute()
802
803             hargs = ['--format',
804                      'houdini',
805                      os.path.join(
806                          baked_directory,
807                          'houdini',
808                          '%s for %s Houdini.lut' % (odt_name, input_space))]
809             bake_lut = Process(description='bake a LUT',
810                                cmd='ociobakelut',
811                                args=(args + hargs))
812             bake_lut.execute()
813
814
815 def create_config_dir(config_directory, bake_secondary_LUTs):
816     """
817     Object description.
818
819     Parameters
820     ----------
821     parameter : type
822         Parameter description.
823
824     Returns
825     -------
826     type
827          Return value description.
828     """
829
830     lut_directory = os.path.join(config_directory, 'luts')
831     dirs = [config_directory, lut_directory]
832     if bake_secondary_LUTs:
833         dirs.extend([os.path.join(config_directory, 'baked'),
834                      os.path.join(config_directory, 'baked', 'flame'),
835                      os.path.join(config_directory, 'baked', 'photoshop'),
836                      os.path.join(config_directory, 'baked', 'houdini'),
837                      os.path.join(config_directory, 'baked', 'lustre'),
838                      os.path.join(config_directory, 'baked', 'maya')])
839
840     for d in dirs:
841         not os.path.exists(d) and os.mkdir(d)
842
843     return lut_directory
844
845
846 def create_ACES_config(aces_ctl_directory,
847                        config_directory,
848                        lut_resolution_1d=4096,
849                        lut_resolution_3d=64,
850                        bake_secondary_LUTs=True,
851                        cleanup=True):
852     """
853     Creates the ACES configuration.
854
855     Parameters
856     ----------
857     parameter : type
858         Parameter description.
859
860     Returns
861     -------
862     type
863          Return value description.
864     """
865
866     lut_directory = create_config_dir(config_directory, bake_secondary_LUTs)
867
868     odt_info = aces.get_ODTs_info(aces_ctl_directory)
869     lmt_info = aces.get_LMTs_info(aces_ctl_directory)
870
871     shaper_name = 'Output Shaper'
872     config_data = generate_LUTs(odt_info,
873                                 lmt_info,
874                                 shaper_name,
875                                 aces_ctl_directory,
876                                 lut_directory,
877                                 lut_resolution_1d,
878                                 lut_resolution_3d,
879                                 cleanup)
880
881     print('Creating config - with prefixes, with aliases')
882     gui_config = create_config(config_data, prefix=True, aliases=True)
883     print('\n\n\n')
884
885     write_config(gui_config,
886                  os.path.join(config_directory, 'config.ocio'))
887
888     '''
889     print('Creating config - with prefixes, without aliases')
890     gui_config = create_config(config_data, prefix=True, aliases=False)
891     print('\n\n\n')
892
893     write_config(gui_config,
894                  os.path.join(config_directory, 'config_w_prefixes_no_aliases.ocio'))
895
896     print('Creating config - without prefixes, with aliases')
897     gui_config = create_config(config_data, prefix=False, aliases=True)
898     print('\n\n\n')
899
900     write_config(gui_config,
901                  os.path.join(config_directory, 'config_no_prefixes_w_aliases.ocio'))
902
903     print('Creating config - without prefixes, without aliases')
904     gui_config = create_config(config_data, prefix=False, aliases=False)
905     print('\n\n\n')
906
907     write_config(gui_config,
908                  os.path.join(config_directory, 'config_no_prefixes_no_aliases.ocio'))
909     '''
910
911     if bake_secondary_LUTs:
912         generate_baked_LUTs(odt_info,
913                             shaper_name,
914                             os.path.join(config_directory, 'baked'),
915                             os.path.join(config_directory, 'config.ocio'),
916                             lut_resolution_1d,
917                             lut_resolution_3d,
918                             lut_resolution_1d)
919
920     return True
921
922
923 def main():
924     """
925     Object description.
926
927     Parameters
928     ----------
929     parameter : type
930         Parameter description.
931
932     Returns
933     -------
934     type
935          Return value description.
936     """
937
938     import optparse
939
940     p = optparse.OptionParser(description='An OCIO config generation script',
941                               prog='createACESConfig',
942                               version='createACESConfig 0.1',
943                               usage='%prog [options]')
944     p.add_option('--acesCTLDir', '-a', default=os.environ.get(
945         ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
946     p.add_option('--configDir', '-c', default=os.environ.get(
947         ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
948     p.add_option('--lutResolution1d', default=4096)
949     p.add_option('--lutResolution3d', default=64)
950     p.add_option('--dontBakeSecondaryLUTs', action='store_true')
951     p.add_option('--keepTempImages', action='store_true')
952
953     options, arguments = p.parse_args()
954
955     aces_ctl_directory = options.acesCTLDir
956     config_directory = options.configDir
957     lut_resolution_1d = int(options.lutResolution1d)
958     lut_resolution_3d = int(options.lutResolution3d)
959     bake_secondary_luts = not options.dontBakeSecondaryLUTs
960     cleanup_temp_images = not options.keepTempImages
961
962     # TODO: Investigate the following statements.
963     try:
964         args_start = sys.argv.index('--') + 1
965         args = sys.argv[args_start:]
966     except:
967         args_start = len(sys.argv) + 1
968         args = []
969
970     print('command line : \n%s\n' % ' '.join(sys.argv))
971
972     assert aces_ctl_directory is not None, (
973         'process: No "{0}" environment variable defined or no "ACES CTL" '
974         'directory specified'.format(
975             ACES_OCIO_CTL_DIRECTORY_ENVIRON))
976
977     assert config_directory is not None, (
978         'process: No "{0}" environment variable defined or no configuration '
979         'directory specified'.format(
980             ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
981
982     return create_ACES_config(aces_ctl_directory,
983                               config_directory,
984                               lut_resolution_1d,
985                               lut_resolution_3d,
986                               bake_secondary_luts,
987                               cleanup_temp_images)
988
989
990 if __name__ == '__main__':
991     main()