diff --git a/meshroom/core/fileUtils.py b/meshroom/core/fileUtils.py new file mode 100644 index 0000000000..f7b4739199 --- /dev/null +++ b/meshroom/core/fileUtils.py @@ -0,0 +1,66 @@ +import os +import re + +pattern = r"(?P.*?)(?P[-._]\d+)?(?P\.\w{3,4})" +compiled_pattern = re.compile(pattern) +compiled_frameId = re.compile(r"(\D+)?(?P\d+$)") + +def getFileElements(inputFilePath: str): + + filename = os.path.basename(inputFilePath) + match = compiled_pattern.search(filename) + frameId_str = match.group("FRAMEID_STR") + + fileElements = {} + if match: + fileElements = { + "": inputFilePath, + "": filename, + "": match.group("FILESTEM_PREFIX"), + "": match.group("FILESTEM_PREFIX"), + "": match.group("EXTENSION"), + } + if frameId_str is not None: + fileElements[""] = frameId_str + fileElements[""] += frameId_str + match_frameId = compiled_frameId.search(frameId_str) + fileElements[""] = match_frameId.group("FRAMEID") + + return fileElements + + +def getViewElements(vp): + + vpPath = vp.childAttribute("path").value + + viewElements = getFileElements(vpPath) + + viewElements[""] = str(vp.childAttribute("viewId").value) + viewElements[""] = str(vp.childAttribute("intrinsicId").value) + viewElements[""] = str(vp.childAttribute("poseId").value) + + return viewElements + + +def replacePatterns(input, pattern, replacements): + # Use all substrings of "input" matching the regex "pattern" as a key to substitute themselves by their value in the dictionary "replacements". + # If "replacements" does not contain the key, the key is removed from "input" to build the resolved string. + def replaceMatch(match): + key = match.group() + return replacements.get(key, "") + return pattern.sub(replaceMatch, input) + + +compiled_element = re.compile(r"<\w*>") + +def resolvePath(input, outputTemplate: str) -> str: + + if isinstance(input, str): + replacements = getFileElements(input) + else: + replacements = getViewElements(input) + + resolved = replacePatterns(outputTemplate, compiled_element, replacements) + + return resolved + diff --git a/meshroom/ui/components/filepath.py b/meshroom/ui/components/filepath.py index bb30153195..3e4b31e3e4 100644 --- a/meshroom/ui/components/filepath.py +++ b/meshroom/ui/components/filepath.py @@ -111,8 +111,8 @@ def fileSizeMB(self, path): @Slot(str, QObject, result=str) def resolve(self, path, vp): # Resolve dynamic path that depends on viewpoint + from meshroom.core import fileUtils - replacements = {} if vp == None: replacements = FilepathHelper.getFilenamesFromFolder(FilepathHelper, FilepathHelper.dirname(FilepathHelper, path), FilepathHelper.extension(FilepathHelper, path)) resolved = [path for i in range(len(replacements))] @@ -120,25 +120,8 @@ def resolve(self, path, vp): for i in range(len(resolved)): resolved[i] = resolved[i].replace("", replacements[i]) return resolved - else: - vpPath = vp.childAttribute("path").value - filename = FilepathHelper.basename(FilepathHelper, vpPath) - replacements = { - "": str(vp.childAttribute("viewId").value), - "": str(vp.childAttribute("intrinsicId").value), - "": str(vp.childAttribute("poseId").value), - "": vpPath, - "": filename, - "": FilepathHelper.removeExtension(FilepathHelper, filename), - "": FilepathHelper.extension(FilepathHelper, filename), - } - - resolved = path - for key in replacements: - resolved = resolved.replace(key, replacements[key]) - - return resolved + return fileUtils.resolvePath(vp, path) @Slot(str, result="QVariantList") @Slot(str, str, result="QVariantList")