[core] init nodes uids from project file

This commit is contained in:
Fabien Castan 2024-06-16 18:54:37 +02:00
parent 412c496804
commit 77571dca38
2 changed files with 17 additions and 7 deletions

View file

@ -341,8 +341,13 @@ class Graph(BaseObject):
for nodeName, nodeData in sorted(data.items(), key=lambda x: self.getNodeIndexFromName(x[0])): for nodeName, nodeData in sorted(data.items(), key=lambda x: self.getNodeIndexFromName(x[0])):
node = self.node(nodeName) node = self.node(nodeName)
savedUid = nodeData.get("uids", "").get("0", "") # Node's UID from the graph file savedUid = nodeData.get("uids", {}) # Node's UID from the graph file
graphUid = node._uids.get(0) # Node's UID from the graph itself # JSON enfore keys to be strings, see
# https://docs.python.org/3.8/library/json.html#json.dump
# We know our keys are integers, so we convert them back to int.
savedUid = {int(k): v for k, v in savedUid.items()}
graphUid = node._uids # Node's UID from the graph itself
if savedUid != graphUid and graphUid is not None: if savedUid != graphUid and graphUid is not None:
# Different UIDs, remove the existing node from the graph and replace it with a CompatibilityNode # Different UIDs, remove the existing node from the graph and replace it with a CompatibilityNode
logging.debug("UID conflict detected for {}".format(nodeName)) logging.debug("UID conflict detected for {}".format(nodeName))

View file

@ -477,7 +477,7 @@ class BaseNode(BaseObject):
# i.e: a.b, a[0], a[0].b.c[1] # i.e: a.b, a[0], a[0].b.c[1]
attributeRE = re.compile(r'\.?(?P<name>\w+)(?:\[(?P<index>\d+)\])?') attributeRE = re.compile(r'\.?(?P<name>\w+)(?:\[(?P<index>\d+)\])?')
def __init__(self, nodeType, position=None, parent=None, **kwargs): def __init__(self, nodeType, position=None, parent=None, uids=None, **kwargs):
""" """
Create a new Node instance based on the given node description. Create a new Node instance based on the given node description.
Any other keyword argument will be used to initialize this node's attributes. Any other keyword argument will be used to initialize this node's attributes.
@ -502,7 +502,7 @@ class BaseNode(BaseObject):
self.graph = None self.graph = None
self.dirty = True # whether this node's outputs must be re-evaluated on next Graph update self.dirty = True # whether this node's outputs must be re-evaluated on next Graph update
self._chunks = ListModel(parent=self) self._chunks = ListModel(parent=self)
self._uids = dict() self._uids = uids if uids else {}
self._cmdVars = {} self._cmdVars = {}
self._size = 0 self._size = 0
self._position = position or Position() self._position = position or Position()
@ -1332,8 +1332,8 @@ class Node(BaseNode):
""" """
A standard Graph node based on a node type. A standard Graph node based on a node type.
""" """
def __init__(self, nodeType, position=None, parent=None, **kwargs): def __init__(self, nodeType, position=None, parent=None, uids=None, **kwargs):
super(Node, self).__init__(nodeType, position, parent, **kwargs) super(Node, self).__init__(nodeType, position, parent=parent, uids=uids, **kwargs)
if not self.nodeDesc: if not self.nodeDesc:
raise UnknownNodeTypeError(nodeType) raise UnknownNodeTypeError(nodeType)
@ -1764,6 +1764,11 @@ def nodeFactory(nodeDict, name=None, template=False, uidConflict=False):
version = nodeDict.get("version", None) version = nodeDict.get("version", None)
internalFolder = nodeDict.get("internalFolder", None) internalFolder = nodeDict.get("internalFolder", None)
position = Position(*nodeDict.get("position", [])) position = Position(*nodeDict.get("position", []))
uids = nodeDict.get("uids", {})
# JSON enfore keys to be strings, see
# https://docs.python.org/3.8/library/json.html#json.dump
# We know our keys are integers, so we convert them back to int.
uids = {int(k): v for k, v in uids.items()}
compatibilityIssue = None compatibilityIssue = None
@ -1824,7 +1829,7 @@ def nodeFactory(nodeDict, name=None, template=False, uidConflict=False):
break break
if compatibilityIssue is None: if compatibilityIssue is None:
node = Node(nodeType, position, **inputs, **internalInputs, **outputs) node = Node(nodeType, position, uids=uids, **inputs, **internalInputs, **outputs)
else: else:
logging.debug("Compatibility issue detected for node '{}': {}".format(name, compatibilityIssue.name)) logging.debug("Compatibility issue detected for node '{}': {}".format(name, compatibilityIssue.name))
node = CompatibilityNode(nodeType, nodeDict, position, compatibilityIssue) node = CompatibilityNode(nodeType, nodeDict, position, compatibilityIssue)