MaterialXOCIO 0.1.39.0.1
Utilities for using OCIO to generate MaterialX definitions and graphs
Loading...
Searching...
No Matches
genOCIODefinitions.py
1#!/usr/bin/env python
2'''
3
4The script will generate MaterialX definitions for all color spaces found in the
5ACES Cg Config` and `ACES Studio Config` configurations.
6
7For source code generation
8- The definitions will be generated for the following MaterialX targets:
9 - GLSL
10- The resulting output is:
11 - A color4 implementation file containing the source code generated by OCIO.
12 - A color3 node graph implementation which uses the color4 implementation (nodegraph)
13 - A file with both color3 and color4 MaterialX definitions (nodedef).
14 - A MaterialX file containing implementation declarations for color3 and color4 variants.
15
16For functional node graph generation:
17- The definition will generate a `nodegraph` and `nodedef`pair based on the
18transforms returned from the OCIO processor.
19- Currently only a `color3 variant is generated.
20
21Additonaly, the script will generate a markdown file with information about the built-in configurations.
22'''
23
24import os, argparse
25import MaterialX as mx
26import PyOpenColorIO as OCIO
27import core as mxocio
28
29def main():
30 parser = argparse.ArgumentParser(description="Create Materialx definitions using OCIO.")
31 parser.add_argument('--graph', dest='graph', help='Generate a node graph implementations instead of source code.', action='store_true')
32 parser.add_argument('--outputPath', dest='outputPath', help='File path to output material files to.')
33
34 opts = parser.parse_args()
35 outputPath = mx.FilePath("./data/")
36 if opts.outputPath:
37 outputPath = mx.FilePath(opts.outputPath)
38
39 # Check OCIO version
40 ver = OCIO.GetVersion()
41 ocioVersion = ver.split('.')
42 if len(ocioVersion) < 2:
43 print('OCIO version is not in the expected format.')
44 return
45 if int(ocioVersion[0]) < 2 or int(ocioVersion[1]) < 2:
46 print('OCIO version 2.2 or greater is required.')
47 return
48
49 print('OCIO version:', ver)
50 print('MaterialX version:', mx.getVersionString())
51
52 # Get the OCIO built in configs and write out the configuration information
53 # to a markdown file.
54 generator = mxocio.OCIOMaterialaxGenerator()
55 configs, aconfig = generator.getBuiltinConfigs()
56 md = generator.printConfigs(configs)
57 # Save configuration information as markdown
58 if not os.path.exists(outputPath.asString()):
59 os.makedirs(outputPath.asString())
60 configInfoFile = outputPath / mx.FilePath('OCIO_configurations.md')
61 print('Write out OCIO configurations to: ' + configInfoFile.asString())
62 f = open(configInfoFile.asString(), 'w')
63 f.write(md)
64
65 sourceColorSpace = "acescg"
66 targetColorSpace = 'lin_rec709'
67
68 # All code has the same input name
69 # It is possible to use a different name than the name used in the generated function ('inPixel')
70 IN_PIXEL_STRING = 'in'
71
72 # Generate MaterialX definitions and implementations for all color spaces
73 # found in the ACES Cg Config and ACES Studio Config configurations.
74 for c in configs:
75 config = configs[c][0]
76 for colorSpace in config.getColorSpaces():
77 aliases = colorSpace.getAliases()
78 trySource = ''
79 for alias in aliases:
80 # Get alias if it does not contain a space
81 if ' ' not in alias:
82 trySource = alias
83 if not trySource:
84 trySource = colorSpace.getName()
85 if trySource:
86 sourceColorSpace = trySource
87
88 # Skip if the source and target are the same
89 if sourceColorSpace == targetColorSpace:
90 continue
91
92 print('--- Generate transform for source color space:', trySource, '---')
93
94 # Generate source code
95 if not opts.graph:
96 definitionDoc = mx.createDocument()
97 implDoc = mx.createDocument()
98
99 definition, transformName, code, extension, target = generator.generateOCIO(aconfig, definitionDoc, implDoc, sourceColorSpace, targetColorSpace, 'color4')
100
101 # Write the definition, implementation and source code files
102 if definition:
103
104 filename = outputPath / mx.FilePath(definition.getName() + '.' + 'mtlx')
105 print('Write MaterialX definition file:', filename.asString())
106 mx.writeToXmlFile(definitionDoc, filename)
107
108 # Write the implementation document
109 implFileName = outputPath / mx.FilePath('IM_' + transformName + '.' + 'mtlx')
110 print('Write MaterialX implementation file:', implFileName.asString())
111 result = mx.writeToXmlFile(implDoc, implFileName)
112
113 generator.writeShaderCode(outputPath, code, transformName, extension, target)
114 else:
115 # Generate node graph
116 outputType = 'color3'
117 graphDoc = generator.generateOCIOGraph(aconfig, sourceColorSpace, targetColorSpace, outputType)
118 if graphDoc:
119 transformName = generator.createTransformName(sourceColorSpace, targetColorSpace, outputType, 'mxgraph_')
120 filename = outputPath / mx.FilePath(transformName + '.' + 'mtlx')
121 print('Write MaterialX node graph definition file:', filename.asString())
122 mx.writeToXmlFile(graphDoc, filename)
123
124 else:
125 print('Could not find suitable color space name to use: ', colorSpace.getName())
126
127
128if __name__ == '__main__':
129 main()