12 Command to convert from XML and JSON representation of a MaterialX document
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")
23 opts = parser.parse_args()
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())
34 print(
'- Write files to outputPath: '+ opts.outputPath)
39 if os.path.isdir(opts.inputFileName):
41 fileList = core.Util.getFiles(opts.inputFileName, extension)
43 extension = mx.FilePath(opts.inputFileName).getExtension()
44 if extension ==
'mtlx':
45 fileList.append(opts.inputFileName)
48 print(
'No files found with extension "%s"' % extension)
56 @brief Utility class to define predicates for skipping elements when iterating over elements in a document.
61 def skip(self, elem: mx.Element):
63 @brief Utility to skip elements when iterating over elements in a document.
65 for predicate
in self.predicates:
66 if not predicate(elem):
70 def skipLibraryElement(elem: mx.Element) -> bool:
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.
75 return not elem.hasSourceUri()
77 def skipAssignments(elem: mx.Element) -> bool:
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.
82 return elem.getCategory()
not in [
'materialassign',
'look',
'lookgroup']
84 def skipMaterials(elem: mx.Element) -> bool:
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.
89 if elem.getCategory()
in [
'surfacematerial']
or elem.getType()
in [
'surfaceshader',
'displacementshader',
'volumeshader']:
93 for fileName
in fileList:
94 if mx.FilePath(fileName).isAbsolute():
95 outputFilePath = mx.FilePath(fileName.replace(
'.mtlx',
'_mtlx.json'))
97 outputFilePath = outputPath / mx.FilePath(fileName.replace(
'.mtlx',
'_mtlx.json'))
98 outputFileName = outputFilePath.asString()
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
110 writeOptions.separators = (
',',
':')
111 writeOptions.indent =
None
112 core.Util.xmlFileToJsonFile(fileName, outputFileName, writeOptions)
113 print(
'Convert XML "%s" -> JSON "%s"' % (fileName, outputFileName))