'CreatedInstance' allows to pass in transient data

This commit is contained in:
Jakub Trllo 2025-01-28 11:51:17 +01:00
parent 283465bbb7
commit 5684c941de

View file

@ -1,6 +1,7 @@
import copy import copy
import collections import collections
from uuid import uuid4 from uuid import uuid4
import typing
from typing import Optional, Dict, List, Any from typing import Optional, Dict, List, Any
from ayon_core.lib.attribute_definitions import ( from ayon_core.lib.attribute_definitions import (
@ -17,6 +18,9 @@ from ayon_core.pipeline import (
from .exceptions import ImmutableKeyError from .exceptions import ImmutableKeyError
from .changes import TrackChangesItem from .changes import TrackChangesItem
if typing.TYPE_CHECKING:
from .creator_plugins import BaseCreator
class ConvertorItem: class ConvertorItem:
"""Item representing convertor plugin. """Item representing convertor plugin.
@ -444,10 +448,11 @@ class CreatedInstance:
def __init__( def __init__(
self, self,
product_type, product_type: str,
product_name, product_name: str,
data, data: Dict[str, Any],
creator, creator: "BaseCreator",
transient_data: Optional[Dict[str, Any]] = None,
): ):
self._creator = creator self._creator = creator
creator_identifier = creator.identifier creator_identifier = creator.identifier
@ -462,7 +467,9 @@ class CreatedInstance:
self._members = [] self._members = []
# Data that can be used for lifetime of object # Data that can be used for lifetime of object
self._transient_data = {} if transient_data is None:
transient_data = {}
self._transient_data = transient_data
# Create a copy of passed data to avoid changing them on the fly # Create a copy of passed data to avoid changing them on the fly
data = copy.deepcopy(data or {}) data = copy.deepcopy(data or {})
@ -787,16 +794,26 @@ class CreatedInstance:
self._create_context.instance_create_attr_defs_changed(self.id) self._create_context.instance_create_attr_defs_changed(self.id)
@classmethod @classmethod
def from_existing(cls, instance_data, creator): def from_existing(
cls,
instance_data: Dict[str, Any],
creator: "BaseCreator",
transient_data: Optional[Dict[str, Any]] = None,
) -> "CreatedInstance":
"""Convert instance data from workfile to CreatedInstance. """Convert instance data from workfile to CreatedInstance.
Args: Args:
instance_data (Dict[str, Any]): Data in a structure ready for instance_data (Dict[str, Any]): Data in a structure ready for
'CreatedInstance' object. 'CreatedInstance' object.
creator (BaseCreator): Creator plugin which is creating the creator (BaseCreator): Creator plugin which is creating the
instance of for which the instance belong. instance of for which the instance belongs.
""" transient_data (Optional[dict[str, Any]]): Instance transient
data.
Returns:
CreatedInstance: Instance object.
"""
instance_data = copy.deepcopy(instance_data) instance_data = copy.deepcopy(instance_data)
product_type = instance_data.get("productType") product_type = instance_data.get("productType")
@ -809,7 +826,11 @@ class CreatedInstance:
product_name = instance_data.get("subset") product_name = instance_data.get("subset")
return cls( return cls(
product_type, product_name, instance_data, creator product_type,
product_name,
instance_data,
creator,
transient_data=transient_data,
) )
def attribute_value_changed(self, key, changes): def attribute_value_changed(self, key, changes):