Main entry point for generating MaterialX definitions using OCIO.
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
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
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
60
61 generator = mxocio.OCIOMaterialaxGenerator()
62 configs, aconfig = generator.getBuiltinConfigs()
63 md = generator.printConfigs(configs)
64
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
76
77 IN_PIXEL_STRING = 'in'
78
79
80
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
88 if ' ' not in alias:
89 trySource = alias
90 if not trySource:
91 trySource = colorSpace.getName()
92 if trySource:
93 sourceColorSpace = trySource
94
95
96 if sourceColorSpace == targetColorSpace:
97 continue
98
99
100
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
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
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
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