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