12def gltf2Mtlx(gltfFileName, mtlxFileName, options=GLTF2MtlxOptions()):
14 @brief Utility to convert a glTF file to MaterialX file
16 @param gltfFileName Path to glTF file to convert
17 @param mtlxFileName Path to MaterialX file to write
18 @param options Options for conversion
24 gltf2MtlxReader.setOptions(options)
25 doc = gltf2MtlxReader.convert(gltfFileName)
28 err =
'Error converting glTF file to MaterialX file'
30 status, err = doc.validate()
32 print(
'Validation error: ', err)
34 Util.writeMaterialXDoc(doc, mtlxFileName)
40 @brief Command line interface to convert from a glTF file to a MaterialX file
42 parser = argparse.ArgumentParser(description=
'Utility to convert a glTF file to MaterialX file')
43 parser.add_argument(dest=
'gltfFileName', help=
'Path containing glTF file to convert.')
44 parser.add_argument(
'--mtlxFileName', dest=
'mtlxFileName', default=
'', help=
'Name of MaterialX output file. If not specified the glTF name with "_converted.mtlx" suffix will be used')
45 parser.add_argument(
'--createAssignments', dest=
'createAssignments', type=mx.stringToBoolean, default=
True, help=
'Create material assignments. Default is True')
46 parser.add_argument(
'--addAllInputs', dest=
'addAllInputs', type=mx.stringToBoolean, default=
False, help=
'Add all definition inputs to MaterialX shader nodes. Default is False')
48 opts = parser.parse_args()
51 gltfFileName = opts.gltfFileName
52 if not os.path.exists(gltfFileName):
53 print(
'Cannot find input file: ', gltfFileName)
57 mtlxFilePath = gltfFileName +
'_converted.mtlx'
59 mtlxFilePath = opts.mtlxFileName
63 options[
'createAssignments'] = opts.createAssignments
64 options[
'addAllInputs'] = opts.addAllInputs
65 converted, err = gltf2Mtlx(gltfFileName, mtlxFilePath, options)
66 print(
'Converted glTF file %s to MaterialX file: %s. Status: %s.' % (gltfFileName, mtlxFilePath, converted))
68 print(
'- Error: ', err)