10def gltf2Mtlx(gltf_file, mtlx_file, options=GLTF2MtlxOptions(), zip=
False):
12 @brief Utility to convert a glTF file to MaterialX file
14 @param gltf_file Path to glTF file to convert
15 @param mtlx_file Path to MaterialX file to write
16 @param options Options for conversion
17 @param zip Write document to zip with image references vs just the document.
23 gltf2MtlxReader.setOptions(options)
24 doc = gltf2MtlxReader.convert(gltf_file)
27 err =
'Error converting glTF file to MaterialX file'
29 status, err = doc.validate()
31 print(
'Validation error(s): ', err)
34 image_references = gltf2MtlxReader.getImageReferences()
35 zip_path = mtlx_file.replace(
'.mtlx',
'.zip')
36 Util.writeMaterialXZip(doc, mtlx_file, zip_path, image_references)
37 print(
'Saved gltf file: %s to MaterialX zip file: %s. Status: %s.' % (gltf_file, zip_path, status))
39 Util.writeMaterialXDoc(doc, mtlx_file)
40 print(
'Saved gltf file: %s to MaterialX file: %s. Status: %s.' % (gltf_file, mtlx_file, status))
46 @brief Command line interface to convert from a glTF file to a MaterialX file
48 parser = argparse.ArgumentParser(description=
'Utility to convert a glTF file to MaterialX file')
49 parser.add_argument(dest=
'gltfFileName', help=
'Path containing glTF file to convert.')
50 parser.add_argument(
'-fn',
'--mtlxFileName', dest=
'mtlxFileName', default=
'', help=
'Name of MaterialX output file. If not specified the glTF name with "_converted.mtlx" suffix will be used')
51 parser.add_argument(
'-ca',
'--createAssignments', dest=
'createAssignments', type=mx.stringToBoolean, default=
True, help=
'Create material assignments. Default is True')
52 parser.add_argument(
'-ai',
'--addAllInputs', dest=
'addAllInputs', type=mx.stringToBoolean, default=
False, help=
'Add all definition inputs to MaterialX shader nodes. Default is False')
53 parser.add_argument(
'-ax',
'--assignXform', dest=
'assignXform', type=mx.stringToBoolean, default=
True, help=
'Assign to transforms vs shapes. Default is True' )
54 parser.add_argument(
'-z',
'--zip', dest=
'zip', type=mx.stringToBoolean, default=
False, help=
'Write a zip file containing the MaterialX file and all referenced texture files. Default is False')
56 opts = parser.parse_args()
59 gltfFileName = opts.gltfFileName
60 if not os.path.exists(gltfFileName):
61 print(
'Cannot find input file: ', gltfFileName)
65 mtlx_path = gltfFileName +
'_converted.mtlx'
67 mtlx_path = opts.mtlxFileName
71 options[
'createAssignments'] = opts.createAssignments
72 options[
'addAllInputs'] = opts.addAllInputs
73 options[
'assignXform'] = opts.assignXform
74 options[
'zip'] = opts.zip
75 converted, err = gltf2Mtlx(gltfFileName, mtlx_path, options)