refactor loading for abstracted get_representation_files

This commit is contained in:
Jakub Jezek 2023-10-09 13:58:19 +02:00
parent 0a71b89ddd
commit 26b2817a70
No known key found for this signature in database
GPG key ID: 730D7C02726179A7
3 changed files with 35 additions and 95 deletions

View file

@ -190,11 +190,7 @@ def remove_media_pool_item(media_pool_item: object) -> bool:
def create_media_pool_item(
fpath: str,
frame_start: int,
frame_end: int,
handle_start: int,
handle_end: int,
files: list,
root: object = None,
) -> object:
"""
@ -212,49 +208,23 @@ def create_media_pool_item(
root_bin = root or media_pool.GetRootFolder()
# try to search in bin if the clip does not exist
existing_mpi = get_media_pool_item(fpath, root_bin)
existing_mpi = get_media_pool_item(files[0], root_bin)
if existing_mpi:
return existing_mpi
files = []
first_frame = frame_start - handle_start
last_frame = frame_end + handle_end
dir_path = os.path.dirname(fpath)
base_name = os.path.basename(fpath)
# 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)
# 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)]
# add all data in folder to media pool
media_pool_items = media_pool.ImportMedia(files)
return media_pool_items.pop() if media_pool_items else False
def get_media_pool_item(fpath, root: object = None) -> object:
def get_media_pool_item(filepath, root: object = None) -> object:
"""
Return clip if found in folder with use of input file path.
Args:
fpath (str): absolute path to a file
filepath (str): absolute path to a file
root (resolve.Folder)[optional]: root folder / bin object
Returns:
@ -262,7 +232,7 @@ def get_media_pool_item(fpath, root: object = None) -> object:
"""
media_pool = get_current_project().GetMediaPool()
root = root or media_pool.GetRootFolder()
fname = os.path.basename(fpath)
fname = os.path.basename(filepath)
for _mpi in root.GetClipList():
_mpi_name = _mpi.GetClipProperty("File Name")

View file

@ -290,7 +290,7 @@ class ClipLoader:
active_bin = None
data = dict()
def __init__(self, loader_obj, context, path, **options):
def __init__(self, loader_obj, context, **options):
""" Initialize object
Arguments:
@ -303,7 +303,6 @@ class ClipLoader:
self.__dict__.update(loader_obj.__dict__)
self.context = context
self.active_project = lib.get_current_project()
self.fname = path
# try to get value from options or evaluate key value for `handles`
self.with_handles = options.get("handles") or bool(
@ -343,37 +342,29 @@ class ClipLoader:
data structure:
{
"name": "assetName_subsetName_representationName"
"path": "path/to/file/created/by/get_repr..",
"binPath": "projectBinPath",
}
"""
# create name
repr = self.context["representation"]
repr_cntx = repr["context"]
asset = str(repr_cntx["asset"])
subset = str(repr_cntx["subset"])
representation = str(repr_cntx["representation"])
representation = self.context["representation"]
representation_context = representation["context"]
asset = str(representation_context["asset"])
subset = str(representation_context["subset"])
representation_name = str(representation_context["representation"])
self.data["clip_name"] = "_".join([
asset,
subset,
representation
representation_name
])
self.data["versionData"] = self.context["version"]["data"]
# gets file path
file = self.fname
if not file:
repr_id = repr["_id"]
print(
"Representation id `{}` is failing to load".format(repr_id))
return None
self.data["path"] = file.replace("\\", "/")
self.data["timeline_basename"] = "timeline_{}_{}".format(
subset, representation)
subset, representation_name)
# solve project bin structure path
hierarchy = str("/".join((
"Loader",
repr_cntx["hierarchy"].replace("\\", "/"),
representation_context["hierarchy"].replace("\\", "/"),
asset
)))
@ -390,39 +381,20 @@ class ClipLoader:
asset_name = self.context["representation"]["context"]["asset"]
self.data["assetData"] = get_current_project_asset(asset_name)["data"]
def _get_frame_data(self):
# create mediaItem in active project bin
# create clip media
frame_start = self.data["versionData"].get("frameStart")
frame_end = self.data["versionData"].get("frameEnd")
if frame_start is None:
frame_start = int(self.data["assetData"]["frameStart"])
if frame_end is None:
frame_end = int(self.data["assetData"]["frameEnd"])
# get handles
handle_start = self.data["versionData"].get("handleStart")
handle_end = self.data["versionData"].get("handleEnd")
if handle_start is None:
handle_start = int(self.data["assetData"]["handleStart"])
if handle_end is None:
handle_end = int(self.data["assetData"]["handleEnd"])
def load(self, files):
"""Load clip into timeline
return frame_start, frame_end, handle_start, handle_end
def load(self):
Arguments:
files (list): list of files to load into timeline
"""
# create project bin for the media to be imported into
self.active_bin = lib.create_bin(self.data["binPath"])
frame_start, frame_end, handle_start, handle_end = \
self._get_frame_data()
handle_start = self.data["versionData"].get("handleStart", 0)
handle_end = self.data["versionData"].get("handleEnd", 0)
media_pool_item = lib.create_media_pool_item(
self.data["path"],
frame_start,
frame_end,
handle_start,
handle_end,
files,
self.active_bin
)
_clip_property = media_pool_item.GetClipProperty
@ -446,21 +418,14 @@ class ClipLoader:
print("Loading clips: `{}`".format(self.data["clip_name"]))
return timeline_item
def update(self, timeline_item):
def update(self, timeline_item, files):
# create project bin for the media to be imported into
self.active_bin = lib.create_bin(self.data["binPath"])
frame_start, frame_end, handle_start, handle_end = \
self._get_frame_data()
# create mediaItem in active project bin
# create clip media
media_pool_item = lib.create_media_pool_item(
self.data["path"],
frame_start,
frame_end,
handle_start,
handle_end,
files,
self.active_bin
)
_clip_property = media_pool_item.GetClipProperty

View file

@ -3,6 +3,7 @@ from openpype.pipeline import (
get_representation_path,
get_representation_context,
get_current_project_name,
get_representation_files
)
from openpype.hosts.resolve.api import lib, plugin
from openpype.hosts.resolve.api.pipeline import (
@ -44,9 +45,11 @@ class LoadClip(plugin.TimelineItemLoader):
def load(self, context, name, namespace, options):
# load clip to timeline and get main variables
path = self.filepath_from_context(context)
filepath = self.filepath_from_context(context)
files = get_representation_files(context, filepath)
timeline_item = plugin.ClipLoader(
self, context, path, **options).load()
self, context, **options).load(files)
namespace = namespace or timeline_item.GetName()
# update color of clip regarding the version order
@ -73,9 +76,11 @@ class LoadClip(plugin.TimelineItemLoader):
media_pool_item = timeline_item.GetMediaPoolItem()
path = get_representation_path(representation)
loader = plugin.ClipLoader(self, context, path)
timeline_item = loader.update(timeline_item)
filepath = get_representation_path(representation)
files = get_representation_files(context, filepath)
loader = plugin.ClipLoader(self, context)
timeline_item = loader.update(timeline_item, files)
# update color of clip regarding the version order
self.set_item_color(timeline_item, version=context["version"])