OP-3909 - 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.)
This commit is contained in:
Petr Kalis 2022-11-03 10:26:53 +01:00
parent 414afab0c0
commit f78bd11977
3 changed files with 22 additions and 4 deletions

View file

@ -1,5 +1,5 @@
import os
import collections
from Qt import QtWidgets
import pyblish.api
@ -292,3 +292,20 @@ def containerise(name,
return comp
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.aftereffects.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]