MaterialXglTF 1.39.5
Loading...
Searching...
No Matches
materialxgltf.gltf2Mtlx Namespace Reference

Utility and command line interface to convert from a glTF file to a MaterialX file. More...

Functions

 gltf2Mtlx (gltf_file, mtlx_file, options=GLTF2MtlxOptions(), zip=False)
 Utility to convert a glTF file to MaterialX file.
 main ()
 Command line interface to convert from a glTF file to a MaterialX file.

Detailed Description

Utility and command line interface to convert from a glTF file to a MaterialX file.

Function Documentation

◆ gltf2Mtlx()

materialxgltf.gltf2Mtlx.gltf2Mtlx ( gltf_file,
mtlx_file,
options = GLTF2MtlxOptions(),
zip = False )

Utility to convert a glTF file to MaterialX file.

Parameters
gltf_filePath to glTF file to convert
mtlx_filePath to MaterialX file to write
optionsOptions for conversion
zipWrite document to zip with image references vs just the document.

Definition at line 10 of file gltf2Mtlx.py.

10def gltf2Mtlx(gltf_file, mtlx_file, options=GLTF2MtlxOptions(), zip=False):
11 '''
12 @brief Utility to convert a glTF file to MaterialX file
13
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.
18 '''
19 status = True
20 err = ''
21
22 gltf2MtlxReader = GLTF2MtlxReader()
23 gltf2MtlxReader.setOptions(options)
24 doc = gltf2MtlxReader.convert(gltf_file)
25 if not doc:
26 status = False
27 err = 'Error converting glTF file to MaterialX file'
28 else:
29 status, err = doc.validate()
30 if not status:
31 print('Validation error(s): ', err)
32
33 if options['zip']:
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))
38 else:
39 Util.writeMaterialXDoc(doc, mtlx_file)
40 print('Saved gltf file: %s to MaterialX file: %s. Status: %s.' % (gltf_file, mtlx_file, status))
41
42 return status, err
43

◆ main()

materialxgltf.gltf2Mtlx.main ( )

Command line interface to convert from a glTF file to a MaterialX file.

Definition at line 44 of file gltf2Mtlx.py.

44def main():
45 '''
46 @brief Command line interface to convert from a glTF file to a MaterialX file
47 '''
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=False, help='Assign to transforms vs shapes. Default is False' )
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')
55
56 opts = parser.parse_args()
57
58 # Check input glTF file
59 gltfFileName = opts.gltfFileName
60 if not os.path.exists(gltfFileName):
61 print('Cannot find input file: ', gltfFileName)
62 exit(-1)
63
64 # Set up MTLX file name
65 mtlx_path = gltfFileName + '_converted.mtlx'
66 if opts.mtlxFileName:
67 mtlx_path = opts.mtlxFileName
68
69 # Perform conversion
70 options = GLTF2MtlxOptions()
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)
76