MaterialXglTF 1.39.0.1
Loading...
Searching...
No Matches
materialxgltf.core.Util Class Reference

Basic I/O Utilities. More...

Static Public Member Functions

(mx.Document, list) createMaterialXDoc ()
 Utility to create a MaterialX document with the default libraries loaded.
 
bool skipLibraryElement (elem)
 Utility to skip library elements when iterating over elements in a document.
 
 writeMaterialXDoc (doc, filename, predicate=skipLibraryElement)
 Utility to write a MaterialX document to a file.
 
 writeMaterialXDocString (doc, predicate=skipLibraryElement)
 Utility to write a MaterialX document to string.
 
list makeFilePathsRelative (doc, docPath)
 Utility to make file paths relative to a document path.
 

Detailed Description

Basic I/O Utilities.

Definition at line 30 of file core.py.

Member Function Documentation

◆ createMaterialXDoc()

(mx.Document, list) materialxgltf.core.Util.createMaterialXDoc ( )
static

Utility to create a MaterialX document with the default libraries loaded.

Returns
The created MaterialX document and the list of loaded library filenames.

Definition at line 33 of file core.py.

33 def createMaterialXDoc() -> (mx.Document, list):
34 '''
35 @brief Utility to create a MaterialX document with the default libraries loaded.
36 @return The created MaterialX document and the list of loaded library filenames.
37 '''
38 doc = mx.createDocument()
39 stdlib = mx.createDocument()
40 libFiles = []
41 searchPath = mx.getDefaultDataSearchPath()
42 libFiles = mx.loadLibraries(mx.getDefaultDataLibraryFolders(), searchPath, stdlib)
43 doc.importLibrary(stdlib)
44
45 return doc, libFiles
46

◆ makeFilePathsRelative()

list materialxgltf.core.Util.makeFilePathsRelative ( doc,
docPath )
static

Utility to make file paths relative to a document path.

Parameters
docThe MaterialX document to update.
docPathThe path to make file paths relapy -tive to.
Returns
List of tuples of unresolved and resolved file paths.

Definition at line 86 of file core.py.

86 def makeFilePathsRelative(doc, docPath) -> list:
87 '''
88 @brief Utility to make file paths relative to a document path
89 @param doc The MaterialX document to update.
90 @param docPath The path to make file paths relapy -tive to.
91 @return List of tuples of unresolved and resolved file paths.
92 '''
93 result = []
94
95 for elem in doc.traverseTree():
96 valueElem = None
97 if elem.isA(mx.ValueElement):
98 valueElem = elem
99 if not valueElem or valueElem.getType() != mx.FILENAME_TYPE_STRING:
100 continue
101
102 unresolvedValue = mx.FilePath(valueElem.getValueString())
103 if unresolvedValue.isEmpty():
104 continue
105
106 elementResolver = valueElem.createStringResolver()
107 if unresolvedValue.isAbsolute():
108 elementResolver.setFilePrefix('')
109 resolvedValue = valueElem.getResolvedValueString(elementResolver)
110 resolvedValue = mx.FilePath(resolvedValue).getBaseName()
111 valueElem.setValueString(resolvedValue)
112
113 if unresolvedValue != resolvedValue:
114 result.append([unresolvedValue.asString(mx.FormatPosix), resolvedValue])
115
116 return result
117

◆ skipLibraryElement()

bool materialxgltf.core.Util.skipLibraryElement ( elem)
static

Utility to skip library elements when iterating over elements in a document.

Returns
True if the element is not in a library, otherwise False.

Definition at line 48 of file core.py.

48 def skipLibraryElement(elem) -> bool:
49 '''
50 @brief Utility to skip library elements when iterating over elements in a document.
51 @return True if the element is not in a library, otherwise False.
52 '''
53 return not elem.hasSourceUri()
54

◆ writeMaterialXDoc()

materialxgltf.core.Util.writeMaterialXDoc ( doc,
filename,
predicate = skipLibraryElement )
static

Utility to write a MaterialX document to a file.

Parameters
docThe MaterialX document to write.
filenameThe name of the file to write to.
predicateA predicate function to determine if an element should be written. Default is to skip library elements.

Definition at line 56 of file core.py.

56 def writeMaterialXDoc(doc, filename, predicate=skipLibraryElement):
57 '''
58 @brief Utility to write a MaterialX document to a file.
59 @param doc The MaterialX document to write.
60 @param filename The name of the file to write to.
61 @param predicate A predicate function to determine if an element should be written. Default is to skip library elements.
62 '''
63 writeOptions = mx.XmlWriteOptions()
64 writeOptions.writeXIncludeEnable = False
65 writeOptions.elementPredicate = predicate
66
67 if filename:
68 mx.writeToXmlFile(doc, filename, writeOptions)
69

◆ writeMaterialXDocString()

materialxgltf.core.Util.writeMaterialXDocString ( doc,
predicate = skipLibraryElement )
static

Utility to write a MaterialX document to string.

Parameters
docThe MaterialX document to write.
predicateA predicate function to determine if an element should be written. Default is to skip library elements.
Returns
The XML string representation of the MaterialX document.

Definition at line 71 of file core.py.

71 def writeMaterialXDocString(doc, predicate=skipLibraryElement):
72 '''
73 @brief Utility to write a MaterialX document to string.
74 @param doc The MaterialX document to write.
75 @param predicate A predicate function to determine if an element should be written. Default is to skip library elements.
76 @return The XML string representation of the MaterialX document.
77 '''
78 writeOptions = mx.XmlWriteOptions()
79 writeOptions.writeXIncludeEnable = False
80 writeOptions.elementPredicate = predicate
81
82 result = mx.writeToXmlString(doc, writeOptions)
83 return result
84

The documentation for this class was generated from the following file: