MCPcopy Create free account
hub / github.com/AcademySoftwareFoundation/MaterialX / flattenFilenames

Function flattenFilenames

source/MaterialXFormat/Util.cpp:146–206  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

144}
145
146void flattenFilenames(DocumentPtr doc, const FileSearchPath& searchPath, StringResolverPtr customResolver)
147{
148 for (ElementPtr elem : doc->traverseTree())
149 {
150 ValueElementPtr valueElem = elem->asA<ValueElement>();
151 if (!valueElem || valueElem->getType() != FILENAME_TYPE_STRING)
152 {
153 continue;
154 }
155
156 FilePath unresolvedValue(valueElem->getValueString());
157 if (unresolvedValue.isEmpty())
158 {
159 continue;
160 }
161 StringResolverPtr elementResolver = elem->createStringResolver();
162 // If the path is already absolute then don't allow an additional prefix
163 // as this would make the path invalid.
164 if (unresolvedValue.isAbsolute())
165 {
166 elementResolver->setFilePrefix(EMPTY_STRING);
167 }
168 string resolvedString = valueElem->getResolvedValueString(elementResolver);
169
170 // Convert relative to absolute pathing if the file is not already found
171 if (!searchPath.isEmpty())
172 {
173 FilePath resolvedValue(resolvedString);
174 if (!resolvedValue.isAbsolute())
175 {
176 for (size_t i = 0; i < searchPath.size(); i++)
177 {
178 FilePath testPath = searchPath[i] / resolvedValue;
179 testPath = testPath.getNormalized();
180 if (testPath.exists())
181 {
182 resolvedString = testPath.asString();
183 break;
184 }
185 }
186 }
187 }
188
189 // Apply any custom filename resolver
190 if (customResolver && customResolver->isResolvedType(FILENAME_TYPE_STRING))
191 {
192 resolvedString = customResolver->resolve(resolvedString, FILENAME_TYPE_STRING);
193 }
194
195 valueElem->setValueString(resolvedString);
196 }
197
198 // Remove any file prefix attributes
199 for (ElementPtr elem : doc->traverseTree())
200 {
201 if (elem->hasFilePrefix())
202 {
203 elem->removeAttribute(Element::FILE_PREFIX_ATTRIBUTE);

Callers 4

preprocessDocumentMethod · 0.85
File.cppFile · 0.85
runRendererMethod · 0.85

Calls 12

traverseTreeMethod · 0.80
createStringResolverMethod · 0.80
existsMethod · 0.80
removeAttributeMethod · 0.80
getTypeMethod · 0.45
getValueStringMethod · 0.45
isEmptyMethod · 0.45
sizeMethod · 0.45
getNormalizedMethod · 0.45
asStringMethod · 0.45
resolveMethod · 0.45

Tested by

no test coverage detected