flame: improving filename frame number operations

This commit is contained in:
Jakub Jezek 2022-01-19 20:13:32 +01:00
parent 07b9a769e1
commit 33a7ddcf0b
No known key found for this signature in database
GPG key ID: D8548FBF690B100A

View file

@ -16,6 +16,7 @@ from openpype.api import Logger
log = Logger.get_logger(__name__)
FRAME_PATTERN = re.compile(r"[\._](\d+)[\.]")
class CTX:
# singleton used for passing data between api modules
@ -601,12 +602,12 @@ def get_clips_in_reels(project):
return output_clips
def get_reformated_path(fname, padded=True):
def get_reformated_path(filename, padded=True):
"""
Return fixed python expression path
Args:
fname (str): file name
filename (str): file name
Returns:
type: string with reformated path
@ -615,27 +616,28 @@ def get_reformated_path(fname, padded=True):
get_reformated_path("plate.1001.exr") > plate.%04d.exr
"""
padding = get_padding_from_path(fname)
found = get_frame_from_path(fname)
found = FRAME_PATTERN.search(filename)
if not found:
log.info("File name is not sequence: {}".format(fname))
return fname
log.info("File name is not sequence: {}".format(filename))
return filename
if padded:
fname = fname.replace(found, "%0{}d".format(padding))
else:
fname = fname.replace(found, "%d")
padding = get_padding_from_path(filename)
return fname
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(fname):
def get_padding_from_path(filename):
"""
Return padding number from Flame path style
Args:
fname (str): file name
filename (str): file name
Returns:
int: padding number
@ -644,17 +646,17 @@ def get_padding_from_path(fname):
get_padding_from_path("plate.0001.exr") > 4
"""
found = get_frame_from_path(fname)
found = get_frame_from_path(filename)
return len(found) if found else None
def get_frame_from_path(fname):
def get_frame_from_path(filename):
"""
Return sequence number from Flame path style
Args:
fname (str): file name
filename (str): file name
Returns:
int: sequence frame number
@ -664,8 +666,7 @@ def get_frame_from_path(fname):
("plate.0001.exr") > 0001
"""
frame_pattern = re.compile(r"[._](\d+)[.]")
found = re.findall(frame_pattern, fname)
found = re.findall(FRAME_PATTERN, filename)
return found.pop() if found else None