MaterialXglTF 1.39.0.1
Loading...
Searching...
No Matches
gltf2Mtlx.py
1#!/usr/bin/env python
2'''
3Utility and command line interface to convert from a glTF file to a MaterialX file
4'''
5import os
6import argparse
7
8#import MaterialX as mx
9#from materialxgltf.core import *
10from core import *
11
12def gltf2Mtlx(gltfFileName, mtlxFileName, options=GLTF2MtlxOptions()):
13 '''
14 @brief Utility to convert a glTF file to MaterialX file
15
16 @param gltfFileName Path to glTF file to convert
17 @param mtlxFileName Path to MaterialX file to write
18 @param options Options for conversion
19 '''
20 status = True
21 err = ''
22
23 gltf2MtlxReader = GLTF2MtlxReader()
24 gltf2MtlxReader.setOptions(options)
25 doc = gltf2MtlxReader.convert(gltfFileName)
26 if not doc:
27 status = False
28 err = 'Error converting glTF file to MaterialX file'
29 else:
30 status, err = doc.validate()
31 if not status:
32 print('Validation error: ', err)
33 #print(mx.writeToXmlString(doc))
34 Util.writeMaterialXDoc(doc, mtlxFileName)
35
36 return status, err
37
38def main():
39 '''
40 @brief Command line interface to convert from a glTF file to a MaterialX file
41 '''
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')
47
48 opts = parser.parse_args()
49
50 # Check input glTF file
51 gltfFileName = opts.gltfFileName
52 if not os.path.exists(gltfFileName):
53 print('Cannot find input file: ', gltfFileName)
54 exit(-1)
55
56 # Set up MTLX file name
57 mtlxFilePath = gltfFileName + '_converted.mtlx'
58 if opts.mtlxFileName:
59 mtlxFilePath = opts.mtlxFileName
60
61 # Perform conversion
62 options = GLTF2MtlxOptions()
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))
67 if not converted:
68 print('- Error: ', err)
69
70if __name__ == "__main__":
71 main()
Class to hold options for glTF to MaterialX conversion.
Definition core.py:121
Class to read glTF and convert to MaterialX.
Definition core.py:139
main()
Command line interface to convert from a glTF file to a MaterialX file.
Definition gltf2Mtlx.py:38