From f92b68f62a5bbe4597edf9e90b2a6fd53b616c29 Mon Sep 17 00:00:00 2001 From: "florentin.luce" Date: Fri, 1 Mar 2024 11:16:31 +0100 Subject: [PATCH] create node child object --- core/node.py | 66 ++++++++++++++++++++++++++++++++--------------- core/node_tree.py | 2 +- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/core/node.py b/core/node.py index a5e5e5b..35f05a7 100644 --- a/core/node.py +++ b/core/node.py @@ -83,31 +83,25 @@ class Node: if self._parent: self.bl_node.parent = self._parent.bl_node - @property - def scene(self): - """Get the name of the scene used by the node. - - Returns: - str: scene name. - """ - if self._scene: - return self._scene.name - - @scene.setter - def scene(self, value): - """Set the blender scene using the bpy Scene object or its name. + @classmethod + def from_blender_node(cls, bl_node, tree): + """Instanciate an abstract class based of the blender node idname. Args: - value (str|bpy.types.Scene): scene name or scene object to set the node. + bl_node (bpy.types.Node): Blender Node To create abstraction from. + tree (NodeTree): Node tree object node belongs to. + + Returns: + Node: Node abstract according to the blender node type. """ - if isinstance(value, str): - self._scene = bpy.data.scenes[value] + if bl_node.bl_idname == 'CompositorNodeRLayers': + return RenderLayersNode(bl_node, tree) - elif isinstance(value, bpy.types.Scene): - self._scene = value + elif bl_node.bl_idname == 'CompositorNodeValToRGB': + return ColorRampNode(bl_node, tree) - if self._scene: - self.bl_node.scene = self._scene + else: + return cls(bl_node, tree) @classmethod def from_dict(cls, data, tree): @@ -122,7 +116,7 @@ class Node: """ new_bl_node = tree.bl_node_tree.nodes.new(type=data['bl_idname']) - node = cls(new_bl_node, parent=tree) + node = cls.from_blender_node(new_bl_node, tree) node.id = data['id'] for p in node.parameters: @@ -167,6 +161,36 @@ class Node: return self.data +class RenderLayersNode(Node): + """Blender Render Layers Node abstraction""" + + @property + def scene(self): + """Get the name of the scene used by the node. + + Returns: + str: scene name. + """ + if self._scene: + return self._scene.name + + @scene.setter + def scene(self, value): + """Set the blender scene using the bpy Scene object or its name. + + Args: + value (str|bpy.types.Scene): scene name or scene object to set the node. + """ + if isinstance(value, str): + self._scene = bpy.data.scenes[value] + + elif isinstance(value, bpy.types.Scene): + self._scene = value + + if self._scene: + self.bl_node.scene = self._scene + + class Link: """Blender Link abstraction.""" diff --git a/core/node_tree.py b/core/node_tree.py index 6a07b1e..4e605c0 100644 --- a/core/node_tree.py +++ b/core/node_tree.py @@ -16,7 +16,7 @@ class NodeTree: 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)) + self.nodes.append(Node.from_blender_node(n, self)) def to_dict(self, select_only=False): """Convert all blender nodes and links inside the tree into a dictionnary.