MaterialXJSON 1.39.0
Loading...
Searching...
No Matches
mtlx2json.py
1#!/usr/bin/env python
2'''
3Command to convert from XML and JSON representation of a MaterialX document
4'''
5import MaterialX as mx
6import core
7import json
8import os, argparse
9
10def main():
11 '''
12 Command to convert from XML and JSON representation of a MaterialX document
13 '''
14 parser = argparse.ArgumentParser(description="Utility to convert from XML to JSON representations of a MaterialX document")
15 parser.add_argument('--outputPath', dest='outputPath', default='', help='File path to output results to.')
16 parser.add_argument('--indent', dest='indent', type=int, default=2, help='Indentation for nested elements. Default is 2.')
17 parser.add_argument('--compact', dest='compact', type=mx.stringToBoolean, default=False, help='Write in compact format. Default is False.')
18 parser.add_argument('--skipLibraryElements', dest='skipLibraryElements', type=mx.stringToBoolean, default=True, help='Skip any library elements. Default is True.')
19 parser.add_argument('--skipMaterials', dest='skipMaterials', type=mx.stringToBoolean, default=False, help='Skip any material elements. Default is False.')
20 parser.add_argument('--skipAssignments', dest='skipAssignments', type=mx.stringToBoolean, default=False, help='Skip any material assignment elements. Default is False.')
21 parser.add_argument(dest="inputFileName", help="Filename of the input document or folder containing input documents")
22
23 opts = parser.parse_args()
24
25 # Get absolute path of opts.outputPath
26 if opts.outputPath:
27 opts.outputPath = os.path.abspath(opts.outputPath)
28 outputPath = mx.FilePath(opts.outputPath)
29 if outputPath.size() > 0:
30 if os.path.isdir(outputPath.asString()):
31 print('Output path "%s" does not exist.' % outputPath.asString())
32 exit(-1)
33 else:
34 print('- Write files to outputPath: '+ opts.outputPath)
35
36 # Get list of MaterialX files
37 fileList = []
38 extension = 'mtlx'
39 if os.path.isdir(opts.inputFileName):
40 extension = 'mtlx'
41 fileList = core.Util.getFiles(opts.inputFileName, extension)
42 else:
43 extension = mx.FilePath(opts.inputFileName).getExtension()
44 if extension == 'mtlx':
45 fileList.append(opts.inputFileName)
46
47 if not fileList:
48 print('No files found with extension "%s"' % extension)
49 exit(-1)
50
51
52 mtlxjson = core.MaterialXJson()
53
54 class Predicates:
55 '''
56 @brief Utility class to define predicates for skipping elements when iterating over elements in a document.
57 '''
58 def __init__(self):
59 self.predicates = []
60
61 def skip(self, elem: mx.Element):
62 '''
63 @brief Utility to skip elements when iterating over elements in a document.
64 '''
65 for predicate in self.predicates:
66 if not predicate(elem):
67 return False
68 return True
69
70 def skipLibraryElement(elem: mx.Element) -> bool:
71 '''
72 @brief Utility to skip library elements when iterating over elements in a document.
73 @return True if the element is not in a library, otherwise False.
74 '''
75 return not elem.hasSourceUri()
76
77 def skipAssignments(elem: mx.Element) -> bool:
78 '''
79 @brief Utility to skip material assignment elements when iterating over elements in a document.
80 @return True if the element is not a material assignment, otherwise False.
81 '''
82 return elem.getCategory() not in ['materialassign', 'look', 'lookgroup']
83
84 def skipMaterials(elem: mx.Element) -> bool:
85 '''
86 @brief Utility to skip material elements when iterating over elements in a document.
87 @return True if the element is not a material element, otherwise False.
88 '''
89 if elem.getCategory() in ['surfacematerial'] or elem.getType() in ['surfaceshader', 'displacementshader', 'volumeshader']:
90 return False
91 return True
92
93 for fileName in fileList:
94 if mx.FilePath(fileName).isAbsolute():
95 outputFilePath = mx.FilePath(fileName.replace('.mtlx', '_mtlx.json'))
96 else:
97 outputFilePath = outputPath / mx.FilePath(fileName.replace('.mtlx', '_mtlx.json'))
98 outputFileName = outputFilePath.asString()
99 writeOptions = core.JsonWriteOptions()
100 predicate = Predicates()
101 if opts.skipAssignments:
102 predicate.predicates.append(skipAssignments)
103 if opts.skipMaterials:
104 predicate.predicates.append(skipMaterials)
105 if opts.skipLibraryElements:
106 predicate.predicates.append(skipLibraryElement)
107 writeOptions.elementPredicate = predicate.skip
108 writeOptions.indent = opts.indent
109 if opts.compact:
110 writeOptions.separators = (',', ':')
111 writeOptions.indent = None
112 core.Util.xmlFileToJsonFile(fileName, outputFileName, writeOptions)
113 print('Convert XML "%s" -> JSON "%s"' % (fileName, outputFileName))
114
115if __name__ == '__main__':
116 main()
Class for holding options for writing MaterialX to JSON.
Definition core.py:31
Class for handling read and write of MaterialX from and to JSON.
Definition core.py:63
main()
Command to convert from XML and JSON representation of a MaterialX document.
Definition mtlx2json.py:10