From 943dfc2568c7a49cf8c98d0f3caccb5911b07d93 Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Wed, 19 Jan 2022 21:01:53 +0100 Subject: [PATCH] flame: updating filename operations also otio export and utils --- openpype/hosts/flame/api/__init__.py | 12 ++-- openpype/hosts/flame/api/lib.py | 16 +++--- openpype/hosts/flame/otio/flame_export.py | 31 +++++----- openpype/hosts/flame/otio/utils.py | 56 +++++++++---------- .../publish/collect_timeline_instances.py | 2 +- 5 files changed, 55 insertions(+), 62 deletions(-) diff --git a/openpype/hosts/flame/api/__init__.py b/openpype/hosts/flame/api/__init__.py index 656ba11617..8e5418c78b 100644 --- a/openpype/hosts/flame/api/__init__.py +++ b/openpype/hosts/flame/api/__init__.py @@ -25,9 +25,9 @@ from .lib import ( reset_segment_selection, get_segment_attributes, get_clips_in_reels, - get_reformated_path, - get_frame_from_path, - get_padding_from_path, + get_reformated_filename, + get_frame_from_filename, + get_padding_from_filename, maintained_object_duplication ) from .utils import ( @@ -92,9 +92,9 @@ __all__ = [ "reset_segment_selection", "get_segment_attributes", "get_clips_in_reels", - "get_reformated_path", - "get_frame_from_path", - "get_padding_from_path", + "get_reformated_filename", + "get_frame_from_filename", + "get_padding_from_filename", "maintained_object_duplication", # pipeline diff --git a/openpype/hosts/flame/api/lib.py b/openpype/hosts/flame/api/lib.py index f524ea0ede..f3c918caab 100644 --- a/openpype/hosts/flame/api/lib.py +++ b/openpype/hosts/flame/api/lib.py @@ -602,7 +602,7 @@ def get_clips_in_reels(project): return output_clips -def get_reformated_path(filename, padded=True): +def get_reformated_filename(filename, padded=True): """ Return fixed python expression path @@ -613,7 +613,7 @@ def get_reformated_path(filename, padded=True): type: string with reformated path Example: - get_reformated_path("plate.1001.exr") > plate.%04d.exr + get_reformated_filename("plate.1001.exr") > plate.%04d.exr """ found = FRAME_PATTERN.search(filename) @@ -622,7 +622,7 @@ def get_reformated_path(filename, padded=True): log.info("File name is not sequence: {}".format(filename)) return filename - padding = get_padding_from_path(filename) + padding = get_padding_from_filename(filename) replacement = "%0{}d".format(padding) if padded else "%d" start_idx, end_idx = found.span(1) @@ -632,7 +632,7 @@ def get_reformated_path(filename, padded=True): ) -def get_padding_from_path(filename): +def get_padding_from_filename(filename): """ Return padding number from Flame path style @@ -643,15 +643,15 @@ def get_padding_from_path(filename): int: padding number Example: - get_padding_from_path("plate.0001.exr") > 4 + get_padding_from_filename("plate.0001.exr") > 4 """ - found = get_frame_from_path(filename) + found = get_frame_from_filename(filename) return len(found) if found else None -def get_frame_from_path(filename): +def get_frame_from_filename(filename): """ Return sequence number from Flame path style @@ -662,7 +662,7 @@ def get_frame_from_path(filename): int: sequence frame number Example: - def get_frame_from_path(path): + def get_frame_from_filename(path): ("plate.0001.exr") > 0001 """ diff --git a/openpype/hosts/flame/otio/flame_export.py b/openpype/hosts/flame/otio/flame_export.py index 615904ec09..562a368215 100644 --- a/openpype/hosts/flame/otio/flame_export.py +++ b/openpype/hosts/flame/otio/flame_export.py @@ -284,23 +284,20 @@ def create_otio_reference(clip_data): # get padding and other file infos log.debug("_ path: {}".format(path)) - is_sequence = padding = utils.get_frame_from_path(path) - if is_sequence: - number = utils.get_frame_from_path(path) - file_head = file_name.split(number)[:-1] - frame_start = int(number) - frame_duration = clip_data["source_duration"] - - if is_sequence: - metadata.update({ - "isSequence": True, - "padding": len(padding) - }) - otio_ex_ref_item = None + is_sequence = frame_number = utils.get_frame_from_filename(file_name) if is_sequence: + file_head = file_name.split(frame_number)[:-1] + frame_start = int(frame_number) + padding = len(frame_number) + + metadata.update({ + "isSequence": True, + "padding": padding + }) + # if it is file sequence try to create `ImageSequenceReference` # the OTIO might not be compatible so return nothing and do it old way try: @@ -322,10 +319,12 @@ def create_otio_reference(clip_data): pass if not otio_ex_ref_item: - reformat_path = utils.get_reformated_path(path, padded=False) + dirname, file_name = os.path.split(path) + file_name = utils.get_reformated_filename(file_name, padded=False) + reformated_path = os.path.join(dirname, file_name) # in case old OTIO or video file create `ExternalReference` otio_ex_ref_item = otio.schema.ExternalReference( - target_url=reformat_path, + target_url=reformated_path, available_range=create_otio_time_range( frame_start, frame_duration, @@ -346,7 +345,7 @@ def create_otio_clip(clip_data): media_reference = create_otio_reference(clip_data) # calculate source in - first_frame = utils.get_frame_from_path(clip_data["fpath"]) or 0 + first_frame = utils.get_frame_from_filename(clip_data["fpath"]) or 0 source_in = int(clip_data["source_in"]) - int(first_frame) # creatae source range diff --git a/openpype/hosts/flame/otio/utils.py b/openpype/hosts/flame/otio/utils.py index 57a15d65a1..e3ffdfce95 100644 --- a/openpype/hosts/flame/otio/utils.py +++ b/openpype/hosts/flame/otio/utils.py @@ -4,6 +4,8 @@ import opentimelineio as otio import logging log = logging.getLogger(__name__) +FRAME_PATTERN = re.compile(r"[\._](\d+)[\.]") + def timecode_to_frames(timecode, framerate): rt = otio.opentime.from_timecode(timecode, framerate) @@ -20,79 +22,71 @@ def frames_to_seconds(frames, framerate): return otio.opentime.to_seconds(rt) -def get_reformated_path(path, padded=True): +def get_reformated_filename(filename, padded=True): """ Return fixed python expression path Args: - path (str): path url or simple file name + filename (str): file name Returns: type: string with reformated path Example: - get_reformated_path("plate.1001.exr") > plate.%04d.exr + get_reformated_filename("plate.1001.exr") > plate.%04d.exr """ - basename = os.path.basename(path) - dirpath = os.path.dirname(path) - padding = get_padding_from_path(basename) - found = get_frame_from_path(basename) + found = FRAME_PATTERN.search(filename) if not found: - log.info("Path is not sequence: {}".format(path)) - return path + log.info("File name is not sequence: {}".format(filename)) + return filename - if padded: - basename = basename.replace(found, "%0{}d".format(padding)) - else: - basename = basename.replace(found, "%d") + padding = get_padding_from_filename(filename) - return os.path.join(dirpath, basename) + replacement = "%0{}d".format(padding) if padded else "%d" + start_idx, end_idx = found.span(1) + + return replacement.join( + [filename[:start_idx], filename[end_idx:]] + ) -def get_padding_from_path(path): +def get_padding_from_filename(filename): """ Return padding number from Flame path style Args: - path (str): path url or simple file name + filename (str): file name Returns: int: padding number Example: - get_padding_from_path("plate.0001.exr") > 4 + get_padding_from_filename("plate.0001.exr") > 4 """ - found = get_frame_from_path(path) + found = get_frame_from_filename(filename) - if found: - return len(found) - else: - return None + return len(found) if found else None -def get_frame_from_path(path): +def get_frame_from_filename(filename): """ Return sequence number from Flame path style Args: - path (str): path url or simple file name + filename (str): file name Returns: int: sequence frame number Example: - def get_frame_from_path(path): + def get_frame_from_filename(path): ("plate.0001.exr") > 0001 """ - frame_pattern = re.compile(r"[._](\d+)[.]") - found = re.findall(frame_pattern, path) + found = re.findall(FRAME_PATTERN, filename) - if found: - return found.pop() - else: - return None + return found.pop() if found else None diff --git a/openpype/hosts/flame/plugins/publish/collect_timeline_instances.py b/openpype/hosts/flame/plugins/publish/collect_timeline_instances.py index df7b0026fb..6424bce3bc 100644 --- a/openpype/hosts/flame/plugins/publish/collect_timeline_instances.py +++ b/openpype/hosts/flame/plugins/publish/collect_timeline_instances.py @@ -47,7 +47,7 @@ class CollectTimelineInstances(pyblish.api.ContextPlugin): # get source clip source_clip = self._get_reel_clip(file_path) - first_frame = opfapi.get_frame_from_path(file_path) or 0 + first_frame = opfapi.get_frame_from_filename(file_path) or 0 head, tail = self._get_head_tail(clip_data, first_frame)