Utility to make file paths relative to a document path.
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