Command line utility to convert a MaterialX file to a glTF file.
70def main():
71 '''
72 Command line utility to convert a MaterialX file to a glTF file
73 '''
74 parser = argparse.ArgumentParser(description='Utility to convert a MaterialX file to a glTF file')
75 parser.add_argument(dest='mtlxFileName', help='Path containing MaterialX file to convert.')
76 parser.add_argument('--gltfFileName', dest='gltfFileName', default='', help='Name of MaterialX output file. If not specified the glTF name with "_tomtlx.mtlx" suffix will be used')
77 parser.add_argument('--gltfGeomFileName', dest='gltfGeomFileName', default='', help='Name of MaterialX output file. If not specified the glTF name with "_tomtlx.mtlx" suffix will be used')
78 parser.add_argument('--primsPerMaterial', dest='primsPerMaterial', type=mx.stringToBoolean, default=False, help='Create a new primitive per material and assign the material. Default is False')
79 parser.add_argument('--packageBinary', dest='packageBinary', type=mx.stringToBoolean, default=False, help='Create a biary packaged GLB file. Default is False')
80 parser.add_argument('--translateShaders', dest='translateShaders', type=mx.stringToBoolean, default=False, help='Translate shaders to glTF. Default is False')
81 parser.add_argument('--bakeTextures', dest='bakeTextures', type=mx.stringToBoolean, default=False, help='Bake pattern graphs as textures. Default is False')
82 parser.add_argument('--bakeResolution', dest='bakeResolution', type=int, default=256, help='Bake image resolution. Default is 256')
83 parser.add_argument('--writeDefaultInputs', dest='writeDefaultInputs', type=mx.stringToBoolean, default=False, help='Write default inputs on shader nodes. Default is False')
84
85 opts = parser.parse_args()
86
87
88 mtlxFileName = opts.mtlxFileName
89 if not os.path.exists(mtlxFileName):
90 print('Cannot find input file: ', mtlxFileName)
91 exit(-1)
92
93
94 mtlxFiles = []
95 ignoreGltfFileName = False
96 if mx.FilePath(mtlxFileName).isDirectory():
97 for file in os.listdir(mtlxFileName):
98 if file.endswith('.mtlx'):
99 mtlxFiles.append(os.path.join(mtlxFileName, file))
100 if len(mtlxFiles) == 0:
101 print('No MaterialX files found in folder: ', mtlxFileName)
102 exit(-1)
103 ignoreGltfFileName = True
104 else:
105 mtlxFiles.append(mtlxFileName)
106
107 for mtlxFileName in mtlxFiles:
108 if len(mtlxFiles) > 1:
109 print('*** Converting MaterialX file:', mtlxFileName)
110
111
112 gltfFileName = mtlxFileName + '.gltf'
113 if not ignoreGltfFileName and len(opts.gltfFileName) > 0:
114 gltfFileName = opts.gltfFileName
115
116
117 options = MTLX2GLTFOptions()
118 options['primsPerMaterial'] = opts.primsPerMaterial
119 options['packageBinary'] = opts.packageBinary
120 gltfGeomFileName = opts.gltfGeomFileName
121 if len(gltfGeomFileName) > 0:
122 if not mx.FilePath(gltfGeomFileName).isAbsolute():
123 gltfGeomFileName = os.path.abspath(gltfGeomFileName)
124 options['geometryFile'] = opts.gltfGeomFileName
125 options['translateShaders'] = opts.translateShaders
126 options['bakeTextures'] = opts.bakeTextures
127 options['bakeResolution'] = opts.bakeResolution
128 options['writeDefaultInputs'] = opts.writeDefaultInputs
129
130
131
132 searchPath = mx.getDefaultDataSearchPath()
133 if not mx.FilePath(mtlxFileName).isAbsolute():
134 mtlxFileName = os.path.abspath(mtlxFileName)
135 searchPath.append(mx.FilePath(mtlxFileName).getParentPath())
136 searchPath.append(mx.FilePath.getCurrentPath())
137 searchPath.append(mx.FilePath(gltfGeomFileName).getParentPath())
138 options['searchPath'] = searchPath
139
140 print("- Search path set to:", searchPath.asString())
141 converted, err = mtlx2gltf(mtlxFileName, gltfFileName, options)
142 print('Converted MaterialX file %s to gltf file: %s. Status: %s.' % (mtlxFileName, gltfFileName, converted))
143 if not converted:
144 print('- Error: ', err)
145