Created
April 17, 2025 13:55
-
-
Save BigRoy/317e8f49271db420c5c06e146207548d to your computer and use it in GitHub Desktop.
Resolve script to match version in name of selected timeline items in media pool to current resolve project version number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Set selected Resolvetimeline name versions to project name version.""" | |
from typing import Optional | |
import re | |
import DaVinciResolveScript as bmd | |
VERSION_REGEX = re.compile(r'_v(\d+)') | |
resolve = bmd.scriptapp('Resolve') | |
manager = resolve.GetProjectManager() | |
project = manager.GetCurrentProject() | |
pool = project.GetMediaPool() | |
num_timelines = project.GetTimelineCount() | |
timelines_by_mpi_id = {} | |
for idx in range(1, num_timelines+1): | |
timeline = project.GetTimelineByIndex(idx) | |
timeline_mpi = timeline.GetMediaPoolItem() | |
timeline_mpi_id = timeline_mpi.GetUniqueId() | |
timelines_by_mpi_id[timeline_mpi_id] = timeline | |
def get_version(text: str) -> Optional[int]: | |
"""Parse version number from 'file_v001.dpr' or alike""" | |
if not text: | |
return None | |
match = VERSION_REGEX.search(text) | |
if not match: | |
return None | |
try: | |
return int(match.group(1)) | |
except ValueError: | |
return None | |
project_name = project.GetName() | |
project_version = get_version(project_name) | |
selected_clips = pool.GetSelectedClips() or [] | |
selected_timelines = [ | |
timelines_by_mpi_id[mpi.GetUniqueId()] | |
for mpi in selected_clips | |
if mpi.GetUniqueId() in timelines_by_mpi_id | |
] | |
for timeline in selected_timelines: | |
timeline_name = timeline.GetName() | |
timeline_version = get_version(timeline_name) | |
# TODO: Instead of creating a new version identifier | |
# directly use the string taken from the project name | |
# so that we match its padding automatically. | |
version_suffix = "_v{0:03d}".format(project_version) | |
if timeline_version is None: | |
# Add version suffix | |
timeline.SetName(timeline_name + version_suffix) | |
elif timeline_version != project_version: | |
new_name = VERSION_REGEX.sub(version_suffix, timeline_name) | |
timeline.SetName(new_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment