Skip to content
Dealga McArdle edited this page May 6, 2017 · 6 revisions

High Level Overview

A new Sverchok Node

This page documents the essentials to make your own node for sverchok. We make several assumptions about your Python knowledge - an affinity for Python is a prerequisite. If you suck at Python, you will have a bad time trying to write nodes for sverchok (-- a hard truth).

The Node Class

This is the basis of all Sverchok nodes

# ##### BEGIN GPL LICENSE BLOCK #####
# ---snipped for brevity ------------
# ##### END GPL LICENSE BLOCK #######

import bpy
# import mathutils
# from mathutils import Vector
# from bpy.props import FloatProperty, BoolProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode

class SvMyFirstNode(bpy.types.Node, SverchCustomTreeNode):
    ''' docstring '''
    bl_idname = 'SvMyFirstNode'
    bl_label = 'name shown in menu'
    bl_icon = 'GREASEPENCIL'

    def sv_init(self, context):
        ...

    def draw_buttons(self, context, layout):
        ...

    def process(self):
        ...


def register():
    bpy.utils.register_class(SvMyFirstNode)


def unregister():
    bpy.utils.unregister_class(SvMyFirstNode)

let's discuss these one by one

License

You can use whatever you want here, but if you ever expect the node to be included in Sverchok as a standard node - then you must be on board with GPL3

Imports

import bpy
# import mathutils
# from mathutils import Vector
# from bpy.props import FloatProperty, BoolProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode
  • You need to import bpy because we use always use bpy.types.Node and often use bpy.props
  • The Sverchok imports are needed for the availability of common Sverchok functions.
    • The updateNode is the function we add to a property to trigger updates when a slider is adjusted.
    • SverchCustomTreeNode's definition can be found in node_tree.py, it adds a poll and several utility functions to the node class.
  • You are free to import any other useful module in that section.

Node Class

class SvMyFirstNode(bpy.types.Node, SverchCustomTreeNode):
    ''' docstring '''
    bl_idname = 'SvMyFirstNode'
    bl_label = 'name shown in menu'
    bl_icon = 'GREASEPENCIL'

    def sv_init(self, context):
        ...

    def draw_buttons(self, context, layout):
        ...

    def process(self):
        ...
class name

A small (but significant) implementation detail of Sverchok is that name of the Node class should be identical to the bl_idname of the node class. This simplified a few things for development. This class name should start with a prefix Sv and contain only alphanumeric characters. Something like SvMyFirstNode would be fine. If you want to automatically make the node available upon startup then this classname must be added to the index.md file in the correct category.

docstring

This is a single or multi line string comment starting on the first line inside a class, this is commonly used in python to help create documentation automatically from class code (we don't use it for that, but we could eventually). In Sverchok this docstring can be used to add a trigger / keyword to the extended search feature.

''' mv - View Matrices /// this is ignored '''

In the Node Search menu, anything trailing the /// is ignored, but anything before it is included in the search results. So if I type mv into the search box then nodes that have those two letters in sequence in their docstring will be returned early. Most nodes do not yet implement this feature. Matrix Viewer does, see:

image

bl_idname

This is the unique identifier for a node type, like a post code for a home.

bl_label

This is the name as it will appear in menus and on the node's header when it's first added to a node tree. Keep this short.

bl_icon

This is used to display a node icon in the shift+A menu. There's also a sv_icon implemented for custom icons (but for a different topic)

def sv_init

This function is used to setup the initial state of a node. This is where you add the default sockets and their properties. This is where we tell a socket to appear as a slider. To better get an idea of what it's used for do a search for this function in the Sverchok repository.

def draw_buttons

This is where we add any custom UI (sliders/buttons/enumerators), this is like the draw function of a panel. The single most helpful resource for UI coding in bpy is still the cookbook . This function can be called many times a second so avoid doing intense computation inside it. This function is for nothing other than drawing the current state of the node, it isn't for updating node properties.

def process

This function is called whenever the node is told to update. It's where we get the content of the input sockets, and set the output of the output sockets. This function is sometimes big..and sometimes merely a few lines - you should look at existing nodes to get a feel for what to put in there.

Usually this function is where you generate data or modify incoming data. You can call other functions from the process function, and you should be doing that if you feel your process function is getting too long. 80 lines long should be more than enough for most nodes, anything above that and you should be thinking about a more modular approach to code.

def register / def unregister

def register():
    bpy.utils.register_class(SvMyFirstNode)


def unregister():
    bpy.utils.unregister_class(SvMyFirstNode)

These function are called when you first register the node or unregister when you hit f8 or disable Sverchok.

continue to the next page low level details

Clone this wiki locally