MaterialXJSON 1.39.0
Loading...
Searching...
No Matches
json2mtlx.py
1#!/usr/bin/env python
2'''
3Command to convert from JSON to XML representation of a MaterialX document
4'''
5import MaterialX as mx
6from materialxjson import core
7import json
8import os, argparse
9
10def main():
11 '''
12 Command to convert from JSON to XML representation of a MaterialX document
13 '''
14 parser = argparse.ArgumentParser(description="Utility to convert from JSON to XML representation of a MaterialX document")
15 parser.add_argument('--outputPath', dest='outputPath', default='', help='File path to output results to.')
16 parser.add_argument('--upgradeVersion', dest='upgradeVersion', type=mx.stringToBoolean, default=True, help='Upgrade document version. Default is True.')
17 parser.add_argument(dest="inputFileName", help="Filename of the input document or folder containing input documents")
18
19 opts = parser.parse_args()
20
21 # Get absolute path of opts.outputPath
22 if opts.outputPath:
23 opts.outputPath = os.path.abspath(opts.outputPath)
24 outputPath = mx.FilePath(opts.outputPath)
25 # Check that output path exists
26 if outputPath.size() > 0:
27 if os.path.isdir(outputPath.asString()):
28 print('Output path "%s" does not exist.' % outputPath.asString())
29 exit(-1)
30 else:
31 print('- Write files to outputPath: '+ opts.outputPath)
32
33 fileList = []
34 extension = 'json'
35 if os.path.isdir(opts.inputFileName):
36 fileList = core.Util.getFiles(opts.inputFileName, extension)
37 else:
38 extension = mx.FilePath(opts.inputFileName).getExtension()
39 if extension == 'json':
40 fileList.append(opts.inputFileName)
41
42 if not fileList:
43 print('No files found with extension "%s"' % extension)
44 exit(-1)
45
46
47 mtlxjson = core.MaterialXJson()
48
49 for fileName in fileList:
50
51 if extension == 'json':
52 if mx.FilePath(fileName).isAbsolute():
53 outputFilePath = mx.FilePath(fileName.replace('.json', '_json.mtlx'))
54 else:
55 outputFilePath = outputPath / mx.FilePath(fileName.replace('.json', '_json.mtlx'))
56 outputFileName = outputFilePath.asString()
57 readOptions = core.JsonReadOptions()
58 readOptions.upgradeVersion = opts.upgradeVersion
59 converted = core.Util.jsonFileToXmlFile(fileName, outputFileName, readOptions)
60 print('Convert JSON file "%s" -> XML file "%s". Status: %s' % (fileName, outputFileName, converted))
61
62if __name__ == '__main__':
63 main()
Class for holding options for reading MaterialX from JSON.
Definition core.py:50
Class for handling read and write of MaterialX from and to JSON.
Definition core.py:63
main()
Command to convert from JSON to XML representation of a MaterialX document.
Definition json2mtlx.py:10