mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 16:34:53 +01:00
70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
import re
|
|
import opentimelineio as otio
|
|
|
|
|
|
def timecode_to_frames(timecode, framerate):
|
|
rt = otio.opentime.from_timecode(timecode, 24)
|
|
return int(otio.opentime.to_frames(rt))
|
|
|
|
|
|
def frames_to_timecode(frames, framerate):
|
|
rt = otio.opentime.from_frames(frames, framerate)
|
|
return otio.opentime.to_timecode(rt)
|
|
|
|
|
|
def frames_to_secons(frames, framerate):
|
|
rt = otio.opentime.from_frames(frames, framerate)
|
|
return otio.opentime.to_seconds(rt)
|
|
|
|
|
|
def get_reformated_path(path, padded=True, first=False):
|
|
"""
|
|
Return fixed python expression path
|
|
|
|
Args:
|
|
path (str): path url or simple file name
|
|
|
|
Returns:
|
|
type: string with reformated path
|
|
|
|
Example:
|
|
get_reformated_path("plate.[0001-1008].exr") > plate.%04d.exr
|
|
|
|
"""
|
|
num_pattern = r"(\[\d+\-\d+\])"
|
|
padding_pattern = r"(\d+)(?=-)"
|
|
first_frame_pattern = re.compile(r"\[(\d+)\-\d+\]")
|
|
|
|
if "[" in path:
|
|
padding = len(re.findall(padding_pattern, path).pop())
|
|
if padded:
|
|
path = re.sub(num_pattern, f"%0{padding}d", path)
|
|
elif first:
|
|
first_frame = re.findall(first_frame_pattern, path, flags=0)
|
|
if len(first_frame) >= 1:
|
|
first_frame = first_frame[0]
|
|
path = re.sub(num_pattern, first_frame, path)
|
|
else:
|
|
path = re.sub(num_pattern, "%d", path)
|
|
return path
|
|
|
|
|
|
def get_padding_from_path(path):
|
|
"""
|
|
Return padding number from DaVinci Resolve sequence path style
|
|
|
|
Args:
|
|
path (str): path url or simple file name
|
|
|
|
Returns:
|
|
int: padding number
|
|
|
|
Example:
|
|
get_padding_from_path("plate.[0001-1008].exr") > 4
|
|
|
|
"""
|
|
padding_pattern = "(\\d+)(?=-)"
|
|
if "[" in path:
|
|
return len(re.findall(padding_pattern, path).pop())
|
|
|
|
return None
|