From 5b8f06991cf575c3a22832e3569f13da299e66d0 Mon Sep 17 00:00:00 2001 From: Cyprien CAILLOT Date: Wed, 11 Sep 2024 16:31:49 +0200 Subject: [PATCH] Bug: Sometime the latest version number cannot be retrieved from instance data use tracked versions --- openpype/modules/ftrack/ftrack_module.py | 18 +++++++++ .../pipeline/latest_tracked_version_number.py | 37 +++++++++++++++++++ .../publish/collect_anatomy_instance_data.py | 22 ++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 openpype/pipeline/latest_tracked_version_number.py diff --git a/openpype/modules/ftrack/ftrack_module.py b/openpype/modules/ftrack/ftrack_module.py index 2042367a7e0..d972a755de5 100644 --- a/openpype/modules/ftrack/ftrack_module.py +++ b/openpype/modules/ftrack/ftrack_module.py @@ -62,6 +62,24 @@ def initialize(self, settings): self.timers_manager_connector = None self._timers_manager_module = None + def get_asset_latest_version_number(self, asset_ftrack_id, task_name, asset_name): + try: + session = self.create_ftrack_session() + except Exception: # noqa + self.log.warning("Couldn't create ftrack session.", exc_info=True) + return + + asset = session.query( + f"Asset where parent.id is '{asset_ftrack_id}'" + f" and latest_version.task.name is '{task_name}'" + f" and name like '%{asset_name}%'" + ).first() + + if asset: + return asset["latest_version"]["version"] + + return None + def get_ftrack_url(self): """Resolved ftrack url. diff --git a/openpype/pipeline/latest_tracked_version_number.py b/openpype/pipeline/latest_tracked_version_number.py new file mode 100644 index 00000000000..ef8daf25fe1 --- /dev/null +++ b/openpype/pipeline/latest_tracked_version_number.py @@ -0,0 +1,37 @@ +import logging + +from openpype.pipeline.context_tools import _get_modules_manager as get_modules_manager + + +log = logging.getLogger(__name__) + + +def get_latest_tracked_version_number(instance, task_name): + """Get the latest version of the instance on the active tracker else None. + Args: + instance (dict): The instance whose version is to be retrieved. + task_name (str): Name of the instance's task. + """ + version_number = None + modules_manager = get_modules_manager() + + # Tracker modules + ftrack_module = modules_manager.modules_by_name.get("ftrack") + kitsu_module = modules_manager.modules_by_name.get("kitsu") + + if ftrack_module and ftrack_module.enabled: + asset_name = instance.data["subset"] + asset_ftrack_id = instance.data["assetEntity"]["data"].get("ftrackId") + version_number = ( + ftrack_module.get_asset_latest_version_number( + asset_ftrack_id, + task_name, + asset_name + ) + ) + elif kitsu_module and kitsu_module.enabled: + # TODO: add support for Kitsu + pass + # Add additional tracker support here + + return version_number diff --git a/openpype/plugins/publish/collect_anatomy_instance_data.py b/openpype/plugins/publish/collect_anatomy_instance_data.py index b1b7ecd1387..c95384bd1b2 100644 --- a/openpype/plugins/publish/collect_anatomy_instance_data.py +++ b/openpype/plugins/publish/collect_anatomy_instance_data.py @@ -34,6 +34,7 @@ get_asset_name_identifier, ) from openpype.pipeline.version_start import get_versioning_start +from openpype.pipeline.latest_tracked_version_number import get_latest_tracked_version_number class CollectAnatomyInstanceData(pyblish.api.ContextPlugin): @@ -211,10 +212,29 @@ def fill_anatomy_data(self, context): if version_number is None: version_number = instance.data.get("version") - # use latest version (+1) if already any exist if version_number is None: + # Try using the latest version data latest_version = instance.data["latestVersion"] + + if version_number is None: + # Last attempt to get the correct version number + # using the data on the tracker + + # Firstly, get the task name + task_name = instance.data.get("task") + if not task_name: + task_data = anatomy_data.get("task") or {} + task_name = task_data.get("name") + + # Try getting the latest version + version_number = get_latest_tracked_version_number( + instance, + task_name + ) + if latest_version is not None: + # We found the latest version + # Increment the value version_number = int(latest_version) + 1 # If version is not specified for instance or context