From d0cc3ac4a795096fc67c450506e2f2cd8b1a3bb1 Mon Sep 17 00:00:00 2001 From: "florentin.luce" Date: Mon, 26 Feb 2024 17:01:57 +0100 Subject: [PATCH] Handeling parent parameter --- core/node.py | 70 +++++++++++++++++++++++++++++++++++++++++++++-- core/node_tree.py | 11 +++++--- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/core/node.py b/core/node.py index c07e638..c5945df 100644 --- a/core/node.py +++ b/core/node.py @@ -1,3 +1,7 @@ + +import bpy +from mathutils import Color, Vector + from .sockets import Input, Output @@ -13,6 +17,8 @@ class Node: self.data = {} self.parameters = [] + self._parent = None + for prop in self.bl_node.bl_rna.properties: if prop.is_readonly: continue @@ -25,6 +31,57 @@ class Node: self.inputs = [Input(ipt, self.tree) for ipt in self.bl_node.inputs] self.outputs = [Output(opt, self.tree) for opt in self.bl_node.outputs] + @property + def parent(self): + """Get the Node from all the other nodes in the tree checking that the + parent of its blender node is the same as the blender node we are comparing. + + Returns: + Node: Node parent. + """ + + if self._parent: + return self._parent + + # if blender node doesn't have a parent + if not self.bl_node.parent: + self._parent = None + return self._parent + + for node in self.tree.nodes: + if node.bl_node == self.bl_node.parent: + self._parent = node + return self._parent + + @parent.setter + def parent(self, value): + """Set the Node parent, using the python object, it's id or the blender node. + + Args: + value (Node|str|bpy.types.Node): Node, id or blender node to set as parent. + """ + + # Node object case + if isinstance(value, Node): + self._parent = value + + # Node id case + elif isinstance(value, str) and value.startswith('0x'): + for node in self.tree.nodes: + if node.id == value: + self._parent = node + else: + print('Cannot find parent') + + # blender node case + elif isinstance(value, bpy.types.Node): + for node in self.tree.nodes: + if node.bl_node == value: + self._parent = node + + if self._parent: + self.bl_node.parent = self._parent.bl_node + @classmethod def from_dict(cls, data, tree): """Create all nodes from their dict representation. @@ -37,12 +94,16 @@ class Node: Node: Create abstract node. """ - new_bl_node = tree.nodes.new(type=data['bl_idname']) + new_bl_node = tree.bl_node_tree.nodes.new(type=data['bl_idname']) node = cls(new_bl_node, parent=tree) + node.id = data['id'] for p in node.parameters: setattr(node, p, data[p]) - setattr(node.bl_node, p, data[p]) + + # set attribute on the blender node only if correct type is retrieve + if p not in ('parent',): + setattr(node.bl_node, p, getattr(node, p)) node.inputs = [Input.from_dict(ipt_data, node) for ipt_data in data['inputs'].values()] node.outputs = [Output.from_dict(opt_data, node) for opt_data in data['outputs'].values()] @@ -64,7 +125,10 @@ class Node: if attr_value is None: attr_value = None - elif not isinstance(attr_value, (str, int, float, list, tuple)): + elif isinstance(attr_value, Node): + attr_value = attr_value.id + + elif isinstance(attr_value, (Color, Vector)): attr_value = list(attr_value) self.data[prop_id] = attr_value diff --git a/core/node_tree.py b/core/node_tree.py index 7337baf..6a07b1e 100644 --- a/core/node_tree.py +++ b/core/node_tree.py @@ -12,10 +12,11 @@ class NodeTree: self.bl_node_tree = bl_node_tree self.data = {} - self.tmp_file = Path('/home/florentin.luce/Bureau/copy_nodes.json') - self.nodes = [Node(n, parent=self) for n in self.bl_node_tree.nodes] - self.links = [Link(l, parent=self) for l in self.bl_node_tree.links] + self.links = [Link(lnk, parent=self) for lnk in self.bl_node_tree.links] + self.nodes = [] + for n in self.bl_node_tree.nodes: + self.nodes.append(Node(n, parent=self)) def to_dict(self, select_only=False): """Convert all blender nodes and links inside the tree into a dictionnary. @@ -47,7 +48,9 @@ class NodeTree: for node_id, node_data in self.data['nodes'].items(): - new_node = Node.from_dict(node_data, self.bl_node_tree) + new_node = Node.from_dict(node_data, self) + self.nodes.append(new_node) + new_node.bl_node.select = True for ipt in new_node.inputs: