소스 검색

New Socket Template class and methods

This commit introduces the dataclass and methods for the unified socket generation goal,
which will make it so I can declare the sockets in one place and then use them to initialize both
the node-container and the ui-node
The result of this will be simpler code, less total lines of code, reduction of both boilerplate and
code duplication. Thus it will also lead to stability improvements.
Finally it also means that I can set default values and make copy constructors.
This commit may also have beneficial ramifications for versioning, too, since it could be used
to generate nodes again from scratch very easily.
Joseph Brandenburg 7 달 전
부모
커밋
e06562fd71
1개의 변경된 파일47개의 추가작업 그리고 4개의 파일을 삭제
  1. 47 4
      base_definitions.py

+ 47 - 4
base_definitions.py

@@ -136,6 +136,23 @@ class SchemaTree(NodeTree):
             
 
 
+from dataclasses import dataclass, field
+from typing import Any
+
+@dataclass
+class MantisSocketTemplate():
+    name            : str = field(default="")
+    bl_idname       : str = field(default="")
+    traverse_target : str = field(default="")
+    identifier      : str = field(default="")
+    display_shape   : str = field(default="") # for arrays
+    category        : str = field(default="") # for use in display update
+    is_input        : bool = field(default=False)
+    hide            : bool = field(default=False)
+    use_multi_input : bool = field(default=False)
+    default_value   : Any = field(default=None)
+
+
 #TODO: do a better job explaining how MantisNode and MantisUINode relate.
 
 class MantisUINode:
@@ -174,6 +191,29 @@ class MantisUINode:
                     node_tree.num_links+=1
                 elif (link.to_socket.is_multi_input):
                     node_tree.num_links+=1
+    
+    def init_sockets(self, socket_templates : tuple[MantisSocketTemplate]):
+        for template in socket_templates:
+            collection = self.outputs
+            if template.is_input:
+                collection = self.inputs
+            identifier = template.name
+            if template.identifier:
+                identifier = template.identifier
+            socket = collection.new(
+                template.bl_idname,
+                template.name,
+                identifier=identifier,
+                use_multi_input=template.use_multi_input
+            )
+            socket.hide= template.hide
+            if template.category:
+                # a custom property for the UI functions to use.
+                socket['category'] = template.category
+            if template.default_value is not None:
+                socket.default_value = template.default_value
+                # this can throw a TypeError - it is the caller's
+                #   responsibility to send the right type.
         
             
 class SchemaUINode(MantisUINode):
@@ -307,8 +347,6 @@ def node_group_update(node, force = False):
         node.id_data.do_live_update = toggle_update
 
 
-
-
 def node_tree_prop_update(self, context):
     if self.is_updating: # update() can be called from update() and that leads to an infinite loop.
         return           # so we check if an update is currently running.
@@ -691,8 +729,13 @@ class MantisNodeSocketCollection(dict):
     
     def init_sockets(self, sockets):
         for socket in sockets:
-            if not isinstance(socket, str): raise RuntimeError("NodeSocketCollection keys must be str.")
-            self[socket] = NodeSocket(is_input=self.is_input, name=socket, node=self.node)
+            if isinstance(socket, str):
+                self[socket] = NodeSocket(is_input=self.is_input, name=socket, node=self.node)
+            elif isinstance(socket, MantisSocketTemplate):
+                if socket.is_input != self.is_input: continue
+                self[socket.name] = NodeSocket(is_input=self.is_input, name=socket.name, node=self.node)
+            else:
+                raise RuntimeError("NodeSocketCollection keys must be str.")
             
     def __delitem__(self, key):
         """Deletes a node socket by name, and all its links."""