MaterialXglTF 1.39.5
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('-fn', '--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('-ca', '--createAssignments', dest='createAssignments', type=mx.stringToBoolean, default=True, help='Create material assignments. Default is True')
46 parser.add_argument('-ai','--addAllInputs', dest='addAllInputs', type=mx.stringToBoolean, default=False, help='Add all definition inputs to MaterialX shader nodes. Default is False')
47 parser.add_argument('-ax', '--assignXform', dest='assignXform', type=mx.stringToBoolean, default=False, help='Assign to transforms vs shapes. Default is False' )
48
49 opts = parser.parse_args()
50
51 # Check input glTF file
52 gltfFileName = opts.gltfFileName
53 if not os.path.exists(gltfFileName):
54 print('Cannot find input file: ', gltfFileName)
55 exit(-1)
56
57 # Set up MTLX file name
58 mtlxFilePath = gltfFileName + '_converted.mtlx'
59 if opts.mtlxFileName:
60 mtlxFilePath = opts.mtlxFileName
61
62 # Perform conversion
63 options = GLTF2MtlxOptions()
64 options['createAssignments'] = opts.createAssignments
65 options['addAllInputs'] = opts.addAllInputs
66 options['assignXform'] = opts.assignXform
67 converted, err = gltf2Mtlx(gltfFileName, mtlxFilePath, options)
68 print('Converted glTF file %s to MaterialX file: %s. Status: %s.' % (gltfFileName, mtlxFilePath, converted))
69 if not converted:
70 print('- Error: ', err)
71
72if __name__ == "__main__":
73 main()
Class to read glTF and convert to MaterialX.
Definition core.py:141
Class to hold options for glTF to MaterialX conversion.
Definition core.py:121
main()
Command line interface to convert from a glTF file to a MaterialX file.
Definition gltf2Mtlx.py:38