diff --git a/openpype/pipeline/__init__.py b/openpype/pipeline/__init__.py index 8f370d389b..ca2a6bcf2c 100644 --- a/openpype/pipeline/__init__.py +++ b/openpype/pipeline/__init__.py @@ -48,6 +48,7 @@ from .load import ( loaders_from_representation, get_representation_path, get_representation_context, + get_representation_files, get_repres_contexts, ) @@ -152,6 +153,7 @@ __all__ = ( "loaders_from_representation", "get_representation_path", "get_representation_context", + "get_representation_files", "get_repres_contexts", # --- Publish --- diff --git a/openpype/pipeline/load/__init__.py b/openpype/pipeline/load/__init__.py index 7320a9f0e8..c07388fd45 100644 --- a/openpype/pipeline/load/__init__.py +++ b/openpype/pipeline/load/__init__.py @@ -11,6 +11,7 @@ from .utils import ( get_contexts_for_repre_docs, get_subset_contexts, get_representation_context, + get_representation_files, load_with_repre_context, load_with_subset_context, @@ -64,6 +65,7 @@ __all__ = ( "get_contexts_for_repre_docs", "get_subset_contexts", "get_representation_context", + "get_representation_files", "load_with_repre_context", "load_with_subset_context", diff --git a/openpype/pipeline/load/utils.py b/openpype/pipeline/load/utils.py index b10d6032b3..81175a8261 100644 --- a/openpype/pipeline/load/utils.py +++ b/openpype/pipeline/load/utils.py @@ -1,4 +1,6 @@ import os +import re +import glob import platform import copy import getpass @@ -286,6 +288,54 @@ def get_representation_context(representation): return context +def get_representation_files(context, filepath): + """Return list of files for representation. + + Args: + representation (dict): Representation document. + filepath (str): Filepath of the representation. + + Returns: + list[str]: List of files for representation. + """ + version = context["version"] + frame_start = version["data"]["frameStart"] + frame_end = version["data"]["frameEnd"] + handle_start = version["data"]["handleStart"] + handle_end = version["data"]["handleEnd"] + + first_frame = frame_start - handle_start + last_frame = frame_end + handle_end + dir_path = os.path.dirname(filepath) + base_name = os.path.basename(filepath) + + # prepare glob pattern for searching + padding = len(str(last_frame)) + str_first_frame = str(first_frame).zfill(padding) + + # convert str_first_frame to glob pattern + # replace all digits with `?` and all other chars with `[char]` + # example: `0001` -> `????` + glob_pattern = re.sub(r"\d", "?", str_first_frame) + + # in filename replace number with glob pattern + # example: `filename.0001.exr` -> `filename.????.exr` + base_name = re.sub(str_first_frame, glob_pattern, base_name) + + files = [] + # get all files in folder + for file in glob.glob(os.path.join(dir_path, base_name)): + files.append(file) + + # keep only existing files + files = [f for f in files if os.path.exists(f)] + + # sort files by frame number + files.sort(key=lambda f: int(re.findall(r"\d+", f)[-1])) + + return files + + def load_with_repre_context( Loader, repre_context, namespace=None, name=None, options=None, **kwargs ):