Script node examples - nortikin/sverchok GitHub Wiki

Script node is one of the ways you can use Python to interact with the nodetree. If you are comfortable with Python, this will let you quickly make your own nodes, and iterate through code changes fast. (it has a "hot reload")

it can:

  • Generate or modify existing geometry.
  • Use Blender Python API (bpy) or any third party Python module.
  • Interact with other nodes or other parts of Blender, as a function of any imaginable input.

Here's a collection of SNLite scripts discussed and presented in the Sverchok issue tracker, useful reading if you get stuck. The documentation can be found here, but it's a bit clinical and will make more sense if you've explored some of the examples beforehand.

Petal Sine

Petal Sine was the first ever script written in ScriptNodeLite it shows how to setup a moderately useful node.

"""
in   n_petals s        default=8       nested=2
in   vp_petal s        default=10      nested=2
in   profile_radius s  ;=2.3           n=2
in   amp s             default=1.0     nested=2
in   origin v          defautt=(0,0,0) n=2
out  verts v
out  edges s
"""

from math import sin, cos, radians, pi
from mathutils import Vector, Euler

# create flower
n_verts = n_petals * vp_petal
section_angle = 360.0 / n_verts
position = (2 * (pi / (n_verts / n_petals)))
x, y, z = origin[:3]

Verts = []
Edges = []

for i in range(n_verts):
    difference = amp * cos(i * position)
    arm = profile_radius + difference
    ampline = Vector((arm, 0.0, 0.0))

    rad_angle = radians(section_angle * i)
    myEuler = Euler((0.0, 0.0, rad_angle), 'XYZ')
    ampline.rotate(myEuler)
    Verts.append((ampline.x+x, ampline.y+y, 0.0+z))

# makes edge keys, ensure cyclic
if Verts:
    i = 0
    Edges.extend([[i, i + 1] for i in range(n_verts - 1)])
    Edges.append([len(Edges), 0])

verts = [Verts]
edges = [Edges]

and here's a numpy version of the same script, to show that there's a reason to learn numpy if you want to write fast and succinct code. Notice that you don't have to explicitely import numpy as np this is already provided by SNLite.

"""
in   n_petals s        default=8       nested=2
in   vp_petal s        default=10      nested=2
in   profile_radius s  ;=2.3           n=2
in   amp s             default=1.0     nested=2
in   origin v          defautt=(0,0,0) n=2
out  verts v
out  edges s
"""
from sverchok.data_structure import get_edge_loop

TAU = np.pi * 2
N = n_petals * vp_petal

pi_vals = np.tile(np.linspace(0, TAU, vp_petal, endpoint=False), n_petals)
amps = np.cos(pi_vals) * amp
theta = np.linspace(0, TAU, N, endpoint=False)
circle_coords = np.array([np.sin(theta), np.cos(theta), np.zeros(N)])
coords = circle_coords.T * (profile_radius + amps.reshape((-1, 1)))

# apply origin location translation
coords += np.array(origin)
verts.append(coords.tolist())

# use optimized edge loop function (cyclic.. )
final_edges = get_edge_loop(N)
edges.append(final_edges)

i'm sure it's possible to write that even shorter using numpy, but this will give an idea.