From dfb79da41f0f787656b2bc09496c4c2e90942c95 Mon Sep 17 00:00:00 2001 From: Petr Kalis Date: Thu, 3 Nov 2022 10:24:01 +0100 Subject: [PATCH] OP-3908 - added caching of list_instances list_instances could be expensive in larger workfiles, it makes sense to cache it for all creator, but only for collecting phase. (This is why creator.collection_shared_data is used.) --- openpype/hosts/photoshop/api/pipeline.py | 24 +++++++++++++++++-- .../photoshop/plugins/create/create_image.py | 4 ++-- .../plugins/create/workfile_creator.py | 4 +++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/openpype/hosts/photoshop/api/pipeline.py b/openpype/hosts/photoshop/api/pipeline.py index 64aa7c0c29..f762ca0062 100644 --- a/openpype/hosts/photoshop/api/pipeline.py +++ b/openpype/hosts/photoshop/api/pipeline.py @@ -1,5 +1,6 @@ import os from Qt import QtWidgets +import collections import pyblish.api @@ -38,9 +39,10 @@ class PhotoshopHost(HostBase, IWorkfileHost, ILoadHost, IPublishHost): name = "photoshop" def install(self): - """Install Photoshop-specific functionality of avalon-core. + """Install Photoshop-specific functionality needed for integration. - This function is called automatically on calling `api.install(photoshop)`. + This function is called automatically on calling + `api.install(photoshop)`. """ log.info("Installing OpenPype Photoshop...") pyblish.api.register_host("photoshop") @@ -289,3 +291,21 @@ def containerise( stub.imprint(layer.id, data) return layer + + +def cache_and_get_instances(creator): + """Cache instances in shared data. + Args: + creator (Creator): Plugin which would like to get instances from host. + Returns: + Dict[str, Dict[str, Any]]: Cached instances list from host + implementation. + """ + shared_key = "openpype.photoshop.instances" + if shared_key not in creator.collection_shared_data: + value = collections.defaultdict(list) + for instance in creator.list_instances(): + identifier = instance["creator_identifier"] + value[identifier].append(instance) + creator.collection_shared_data[shared_key] = value + return creator.collection_shared_data[shared_key] diff --git a/openpype/hosts/photoshop/plugins/create/create_image.py b/openpype/hosts/photoshop/plugins/create/create_image.py index c352965df8..53e68ef384 100644 --- a/openpype/hosts/photoshop/plugins/create/create_image.py +++ b/openpype/hosts/photoshop/plugins/create/create_image.py @@ -9,7 +9,7 @@ from openpype.pipeline import ( ) from openpype.lib import prepare_template_data from openpype.pipeline.create import SUBSET_NAME_ALLOWED_SYMBOLS -from openpype.hosts.photoshop.api import PhotoshopHost +from openpype.hosts.photoshop.api.pipeline import cache_and_get_instances class ImageCreator(Creator): @@ -20,7 +20,7 @@ class ImageCreator(Creator): description = "Image creator" def collect_instances(self): - for instance_data in self.host.list_instances(): + for instance_data in cache_and_get_instances(self): # legacy instances have family=='image' creator_id = (instance_data.get("creator_identifier") or instance_data.get("family")) diff --git a/openpype/hosts/photoshop/plugins/create/workfile_creator.py b/openpype/hosts/photoshop/plugins/create/workfile_creator.py index 087d5a8a3f..8ee9a0d832 100644 --- a/openpype/hosts/photoshop/plugins/create/workfile_creator.py +++ b/openpype/hosts/photoshop/plugins/create/workfile_creator.py @@ -5,6 +5,8 @@ from openpype.pipeline import ( CreatedInstance, legacy_io ) +from openpype.hosts.photoshop.api.pipeline import cache_and_get_instances + class PSWorkfileCreator(AutoCreator): identifier = "workfile" @@ -16,7 +18,7 @@ class PSWorkfileCreator(AutoCreator): return [] def collect_instances(self): - for instance_data in self.host.list_instances(): + for instance_data in cache_and_get_instances(self): creator_id = instance_data.get("creator_identifier") if creator_id == self.identifier: subset_name = instance_data["subset"]