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

Basic I/O Utilities. More...

Static Public Member Functions

tuple[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.
 writeMaterialXZip (doc, mtlx_filename, zip_filename, image_references, predicate=skipLibraryElement)
 Utility to write a MaterialX document and its referenced images to a zip file.
 writeMaterialXStringZip (mtlx_str, mtlx_filename, zip_filename, image_references, predicate=skipLibraryElement)
 Utility to write a MaterialX document and its referenced images to a zip 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 34 of file core.py.

Member Function Documentation

◆ createMaterialXDoc()

tuple[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 37 of file core.py.

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

◆ 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 122 of file core.py.

122 def makeFilePathsRelative(doc, docPath) -> list:
123 '''
124 @brief Utility to make file paths relative to a document path
125 @param doc The MaterialX document to update.
126 @param docPath The path to make file paths relapy -tive to.
127 @return List of tuples of unresolved and resolved file paths.
128 '''
129 result = []
130
131 for elem in doc.traverseTree():
132 valueElem = None
133 if elem.isA(mx.ValueElement):
134 valueElem = elem
135 if not valueElem or valueElem.getType() != mx.FILENAME_TYPE_STRING:
136 continue
137
138 unresolvedValue = mx.FilePath(valueElem.getValueString())
139 if unresolvedValue.isEmpty():
140 continue
141
142 elementResolver = valueElem.createStringResolver()
143 if unresolvedValue.isAbsolute():
144 elementResolver.setFilePrefix('')
145 resolvedValue = valueElem.getResolvedValueString(elementResolver)
146 resolvedValue = mx.FilePath(resolvedValue).getBaseName()
147 valueElem.setValueString(resolvedValue)
148
149 if unresolvedValue != resolvedValue:
150 result.append([unresolvedValue.asString(mx.FormatPosix), resolvedValue])
151
152 return result
153

◆ 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 52 of file core.py.

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

◆ 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 60 of file core.py.

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

◆ 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 107 of file core.py.

107 def writeMaterialXDocString(doc, predicate=skipLibraryElement):
108 '''
109 @brief Utility to write a MaterialX document to string.
110 @param doc The MaterialX document to write.
111 @param predicate A predicate function to determine if an element should be written. Default is to skip library elements.
112 @return The XML string representation of the MaterialX document.
113 '''
114 writeOptions = mx.XmlWriteOptions()
115 writeOptions.writeXIncludeEnable = False
116 writeOptions.elementPredicate = predicate
117
118 result = mx.writeToXmlString(doc, writeOptions)
119 return result
120

◆ writeMaterialXStringZip()

materialxgltf.core.Util.writeMaterialXStringZip ( mtlx_str,
mtlx_filename,
zip_filename,
image_references,
predicate = skipLibraryElement )
static

Utility to write a MaterialX document and its referenced images to a zip file.

Parameters
mtlx_strThe MaterialX document string to write.
mtlx_filenameThe name of the MaterialX file to write within the zip file.
zip_filenameThe name of the zip file to write to.
image_referencesThe list of image file references to include in the zip file.
predicateA predicate function to determine if an element should be written. Default is to skip library elements.

Definition at line 88 of file core.py.

88 def writeMaterialXStringZip(mtlx_str, mtlx_filename, zip_filename, image_references, predicate=skipLibraryElement):
89 '''
90 @brief Utility to write a MaterialX document and its referenced images to a zip file.
91 @param mtlx_str The MaterialX document string to write.
92 @param mtlx_filename The name of the MaterialX file to write within the zip file.
93 @param zip_filename The name of the zip file to write to.
94 @param image_references The list of image file references to include in the zip file.
95 @param predicate A predicate function to determine if an element should be written. Default is to skip library elements.
96 '''
97 with zipfile.ZipFile(zip_filename, "w") as zipf:
98 # Write MaterialX file
99 zipf.writestr(mtlx_filename, mtlx_str)
100 # Write texture files
101 for path in image_references:
102 zipf.write(path, os.path.basename(path))
103 zipf.close( )
104
105

◆ writeMaterialXZip()

materialxgltf.core.Util.writeMaterialXZip ( doc,
mtlx_filename,
zip_filename,
image_references,
predicate = skipLibraryElement )
static

Utility to write a MaterialX document and its referenced images to a zip file.

Parameters
docThe MaterialX document to write.
mtlx_filenameThe name of the MaterialX file to write within the zip file.
zip_filenameThe name of the zip file to write to.
image_referencesThe list of image file references to include in the zip file.
predicateA predicate function to determine if an element should be written. Default is to skip library elements.

Definition at line 75 of file core.py.

75 def writeMaterialXZip(doc, mtlx_filename, zip_filename, image_references, predicate=skipLibraryElement):
76 '''
77 @brief Utility to write a MaterialX document and its referenced images to a zip file.
78 @param doc The MaterialX document to write.
79 @param mtlx_filename The name of the MaterialX file to write within the zip file.
80 @param zip_filename The name of the zip file to write to.
81 @param image_references The list of image file references to include in the zip file.
82 @param predicate A predicate function to determine if an element should be written. Default is to skip library elements.
83 '''
84 mtlx_str = Util.writeMaterialXDocString(doc, predicate)
85 Util.writeMaterialXStringZip(mtlx_str, mtlx_filename, zip_filename, image_references)
86

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