From 0257e04192b40fb3df7b65f12a15fcbcf76cb753 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 9 Mar 2021 16:15:03 +0100 Subject: [PATCH] fixed asset name lookup --- .../plugins/publish/collect_matching_asset.py | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/pype/hosts/standalonepublisher/plugins/publish/collect_matching_asset.py b/pype/hosts/standalonepublisher/plugins/publish/collect_matching_asset.py index 16147dc738..f1686dc42f 100644 --- a/pype/hosts/standalonepublisher/plugins/publish/collect_matching_asset.py +++ b/pype/hosts/standalonepublisher/plugins/publish/collect_matching_asset.py @@ -21,19 +21,23 @@ class CollectMatchingAssetToInstance(pyblish.api.InstancePlugin): version_regex = re.compile(r"^(.+)_v([0-9]+)$") def process(self, instance): - source_file = os.path.basename(instance.data["source"]).lower() + source_filename = self.get_source_filename(instance) self.log.info("Looking for asset document for file \"{}\"".format( - instance.data["source"] + source_filename )) + asset_name = os.path.splitext(source_filename)[0].lower() asset_docs_by_name = self.selection_children_by_name(instance) version_number = None # Always first check if source filename is in assets - matching_asset_doc = asset_docs_by_name.get(source_file) + matching_asset_doc = asset_docs_by_name.get(asset_name) if matching_asset_doc is None: # Check if source file contain version in name - regex_result = self.version_regex.findall(source_file) + self.log.debug(( + "Asset doc by \"{}\" was not found trying version regex." + ).format(asset_name)) + regex_result = self.version_regex.findall(asset_name) if regex_result: asset_name, _version_number = regex_result[0] matching_asset_doc = asset_docs_by_name.get(asset_name) @@ -42,16 +46,19 @@ class CollectMatchingAssetToInstance(pyblish.api.InstancePlugin): if matching_asset_doc is None: for asset_name_low, asset_doc in asset_docs_by_name.items(): - if asset_name_low in source_file: + if asset_name_low in asset_name: matching_asset_doc = asset_doc break if not matching_asset_doc: + self.log.debug("Available asset names {}".format( + str(list(asset_docs_by_name.keys())) + )) # TODO better error message raise AssertionError(( "Filename \"{}\" does not match" " any name of asset documents in database for your selection." - ).format(instance.data["source"])) + ).format(source_filename)) instance.data["asset"] = matching_asset_doc["name"] instance.data["assetEntity"] = matching_asset_doc @@ -62,6 +69,25 @@ class CollectMatchingAssetToInstance(pyblish.api.InstancePlugin): f"Matching asset found: {pformat(matching_asset_doc)}" ) + def get_source_filename(self, instance): + if instance.data["family"] == "background_batch": + return os.path.basename(instance.data["source"]) + + if len(instance.data["representations"]) != 1: + raise ValueError(( + "Implementation bug: Instance data contain" + " more than one representation." + )) + + repre = instance.data["representations"][0] + repre_files = repre["files"] + if not isinstance(repre_files, str): + raise ValueError(( + "Implementation bug: Instance's representation contain" + " unexpected value (expected single file). {}" + ).format(str(repre_files))) + return repre_files + def selection_children_by_name(self, instance): storing_key = "childrenDocsForSelection"