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
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
53
54 generator = mxocio.OCIOMaterialaxGenerator()
55 configs, aconfig = generator.getBuiltinConfigs()
56 md = generator.printConfigs(configs)
57
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
69
70 IN_PIXEL_STRING = 'in'
71
72
73
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
81 if ' ' not in alias:
82 trySource = alias
83 if not trySource:
84 trySource = colorSpace.getName()
85 if trySource:
86 sourceColorSpace = trySource
87
88
89 if sourceColorSpace == targetColorSpace:
90 continue
91
92 print('--- Generate transform for source color space:', trySource, '---')
93
94
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
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
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
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