sverchok icon indicating copy to clipboard operation
sverchok copied to clipboard

Socket templates

Open Durman opened this issue 2 years ago • 0 comments

Problem statement

This proposal can be considered as evolution of this #3437 one. Sockets template is alternative of sv_init method. The advantage is that template can be used in many situations. For example it will be possible to detect the cases when nodes get extra sockets and automatically update nodes in old layouts.

class SocketTemplate(NamedTuple):
    type: NodeSocket
    name: str = ''
    enabled: bool = True
    default_value: Any = None
    update_ref: str = None  # for dynamic sockets

    def init(self, node: Node):
        sock = node.inputs/outputs.new(self.type, self.name)
        sock.enabled = self.enabled
        ...


class Node(BaseNode):
    Inputs = namedtuple('Inputs', ['verts', 'faces', 'values'])  # socket identifiers and default names
    input_template = Inputs(
        SocketTemplate('SvVerticesSocket', deep_copy=False),  # Named tuple with some properties
        SocketTemplate('SvStringsSocket', deep_copy=False),
        SocketTemplate('SvStringsSocket', use_prop=True, default_val=0.5)
    )

The template can be used during node initialization.

class BaseNode:
    input_template = []  # should be overridden 

    def init(self, context):
        for template in self.input_template:
            template.init(self)
        sv_init(context)  # for some extra stuff

Also this can be used by update system to parse input/output data, to apply vectorization rules, for autocomplete when a node is coded.

def update_system():
    for node, input_data in zip(nodes, data):
        node.process(node.Inputs(*input_data)


class Node:
    Inputs = namedtuple('Inputs', ['verts', 'faces', 'values'])

    ...

    @staticmethod
    def process(input: Inputs):
        verts = input.verts

Durman avatar May 07 '22 19:05 Durman