global: adding abstracted get_representation_files

This commit is contained in:
Jakub Jezek 2023-10-09 13:57:53 +02:00
parent 3d2a5a3e8c
commit 0a71b89ddd
No known key found for this signature in database
GPG key ID: 730D7C02726179A7
3 changed files with 54 additions and 0 deletions

View file

@ -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 ---

View file

@ -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",

View file

@ -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
):