3 ways to integrate a modifier into Sverchok

If you have been using Sverchok you probably have already wondered how to integrate one or more modifiers inside of it. Personally, I am looking forward to a Modifiers node. In the meantime, I propose you three alternative solutions.

Sverchok modifier using Viewer BMesh and the Properties editor

This is the simplest solution. We generate the geometry in Sverchok, then we plug the data to a Viewer BMesh. The node will add a new object to the scene. Because of that, we can apply to it all the modifiers that we want. If we want to keep editing the geometry in Sverchok, we can use an Object ID out MK2 node, from the BPY Data group, activating the Post modifiers tick. To see the effects, you might need to update the node tree from the Properties panel. Don’t apply directly the modifiers from the Properties editor.

Sverchok modifier 1

Sverchok modifier using Viewer BMesh and Exec Node Mod

This approach is similar to the previous one but it has the advantage of keeping things inside of Sverchok. Instead of adding the modifiers from the user interface, we create them trough scripting inside an Exec Node Mod. If we want we can then pass the objects to the Object ID Out MK2 node as we have seen in the previous method. In the hypothesis that we want to add a subdivision surface modifier, we could write write something like this in the Exec Node Mod:

for obj in V1:
    if not "subsurf" in obj.modifiers:
        obj.modifiers.new("subsurf", 'SUBSURF')
    elif obj.modifiers["subsurf"].levels != V2[0][0]:
        obj.modifiers["subsurf"].levels = V2[0][0]
Sverchok modifier 2

Sverchok modifier trough custom script inside Scripted Node

The last possibility is to write a custom node via Scripted Node Lite or Scripted Node. This solution is probably best suited if we want to do also something else more complex inside the script. If you follow this approach you can pass to the script an object created with the Viewer BMesh or you can build everything inside of it. In the following example we will use this second method inside a Scripted Node Lite. In this script we add a subsurf modifier, but you can adapt it for any other case. Please refer to the Blender API documentation if you have doubts, or ask in the comments.

in levels  s d=1  n=0
in vert_in v d=[] n=1
in edge_in s d=[] n=1
in poly_in s d=[] n=1
out verts v
out edgs  s
out polys s
import bpy, bmesh

#Create a new mesh from the Sverchok data
mesh = bpy.data.meshes.new("mesh")
mesh.from_pydata(vert_in, edge_in, poly_in)

#Create a new object from the mesh
obj = bpy.data.objects.new("obj", mesh)

#Add modifiers
mod = obj.modifiers.new("subsurf", 'SUBSURF')
mod.levels = levels

#Create a bmesn from the object (the modifiers will be applied)
bm = bmesh.new()
bm.from_object(obj, bpy.context.scene)

#Apply the new bmesh data to the mesh object

#Get the vertex, edge and polygon list from the mesh    
verts = [[v.co.to_tuple() for v in mesh.vertices]]
edgs =  [[[i for i in e.vertices] for e in mesh.edges]]
polys = [[[i for i in p.vertices] for p in mesh.polygons]]

#Clean up bpy.data
Sverchok modifier 3

Learning Sverchok front page

Interested in Sverchok? Then check out Learning Sverchok, the free ebook by CodePlastic!

Leave a Reply