MaterialXOCIO 0.1.39.5
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 """
31 Main entry point for generating MaterialX definitions using OCIO.
32 """
33 parser = argparse.ArgumentParser(description="Create Materialx definitions using OCIO.")
34 parser.add_argument('-g', '--graph', dest='graph', help='Generate a node graph implementations instead of source code.', action='store_true')
35 parser.add_argument('-o', '--outputPath', dest='outputPath', help='File path to output material files to.')
36
37 opts = parser.parse_args()
38 outputPath = mx.FilePath("./data/")
39 if opts.outputPath:
40 # If not existing create the output path
41 if not os.path.exists(opts.outputPath):
42 print('Creating output path: ' + opts.outputPath)
43 os.makedirs(opts.outputPath, exist_ok=True)
44 outputPath = mx.FilePath(opts.outputPath)
45
46 # Check OCIO version
47 ver = OCIO.GetVersion()
48 ocioVersion = ver.split('.')
49 if len(ocioVersion) < 2:
50 print('OCIO version is not in the expected format.')
51 return
52 if int(ocioVersion[0]) < 2 or int(ocioVersion[1]) < 2:
53 print('OCIO version 2.2 or greater is required.')
54 return
55
56 print('OCIO version:', ver)
57 print('MaterialX version:', mx.getVersionString())
58
59 # Get the OCIO built in configs and write out the configuration information
60 # to a markdown file.
61 generator = mxocio.OCIOMaterialaxGenerator()
62 configs, aconfig = generator.getBuiltinConfigs()
63 md = generator.printConfigs(configs)
64 # Save configuration information as markdown
65 if not os.path.exists(outputPath.asString()):
66 os.makedirs(outputPath.asString())
67 configInfoFile = outputPath / mx.FilePath('OCIO_configurations.md')
68 print('Write out OCIO configurations to: ' + configInfoFile.asString())
69 f = open(configInfoFile.asString(), 'w')
70 f.write(md)
71
72 sourceColorSpace = "acescg"
73 targetColorSpace = 'lin_rec709'
74
75 # All code has the same input name
76 # It is possible to use a different name than the name used in the generated function ('inPixel')
77 IN_PIXEL_STRING = 'in'
78
79 # Generate MaterialX definitions and implementations for all color spaces
80 # found in the ACES Cg Config and ACES Studio Config configurations.
81 for c in configs:
82 config = configs[c][0]
83 for colorSpace in config.getColorSpaces():
84 aliases = colorSpace.getAliases()
85 trySource = ''
86 for alias in aliases:
87 # Get alias if it does not contain a space
88 if ' ' not in alias:
89 trySource = alias
90 if not trySource:
91 trySource = colorSpace.getName()
92 if trySource:
93 sourceColorSpace = trySource
94
95 # Skip if the source and target are the same
96 if sourceColorSpace == targetColorSpace:
97 continue
98
99
100 # Generate source code
101 if not opts.graph:
102 definitionDoc = mx.createDocument()
103 implDoc = mx.createDocument()
104
105 definition, transformName, code, extension, target = generator.generateOCIO(aconfig, definitionDoc, implDoc, sourceColorSpace, targetColorSpace, 'color4')
106
107 # Write the definition, implementation and source code files
108 if definition:
109 valid, errors = definitionDoc.validate()
110 if not valid:
111 print('Generated MaterialX definition document is not valid:')
112 print(errors)
113
114 print('--- Generated shader code for source color space:', trySource, '---')
115
116 filename = outputPath / mx.FilePath(definition.getName() + '.' + 'mtlx')
117 print('Write MaterialX definition file:', filename.asString())
118 mx.writeToXmlFile(definitionDoc, filename)
119
120 # Write the implementation document
121 implFileName = outputPath / mx.FilePath('IM_' + transformName + '.' + 'mtlx')
122 print('Write MaterialX implementation file:', implFileName.asString())
123 result = mx.writeToXmlFile(implDoc, implFileName)
124
125 generator.writeShaderCode(outputPath, code, transformName, extension, target)
126 else:
127 # Generate node graph
128 outputType = 'color3'
129
130 graphDoc = generator.generateOCIOGraph(aconfig, sourceColorSpace, targetColorSpace, outputType)
131 if graphDoc:
132 valid, errors = graphDoc.validate()
133 if not valid:
134 print('Generated MaterialX definition document is not valid:')
135 print(errors)
136
137 print('Generated <nodegraph> for source color space:', trySource, '---')
138 sourceColorSpace = generator.createValidName(sourceColorSpace)
139 targetColorSpace = generator.createValidName(targetColorSpace)
140
141 transformName = generator.createTransformName(sourceColorSpace, targetColorSpace, outputType, 'mxgraph_')
142 filename = outputPath / mx.FilePath(transformName + '.' + 'mtlx')
143 print('Write MaterialX node graph definition file:', filename.asString())
144 mx.writeToXmlFile(graphDoc, filename)
145
146 else:
147 print('Could not find suitable color space name to use: ', colorSpace.getName())
148
149
150if __name__ == '__main__':
151 main()
main()
Main entry point for generating MaterialX definitions using OCIO.