Introduction to modular components¶
Same as lesson 01.
Lesson 02 - A Circle¶
This lesson will introduce the following nodes:
List Length, Int Range, List Shift, List Zip
This will continue from the previous lesson where we made a plane from 4 vectors. We can reuse some of these nodes in order to make a Circle. If you saved it as suggested load it up, or download from here. You can also create it from scratch by cross referencing this image.
Just like Blender has a Circle primitive, Sverchok also has a built in Circle primitive called the Circle Generator. We will avoid using the primtives until we’ve covered more of the fundamental nodes and how they interact.
In the collection of nodes we have in the Node View at the moment, the sequence used for linking up vertices to form a polygon is input manually. As mentioned earlier, as soon as you need to link many vertices instead of the current 4, you will want to make this list creation automatic. You will probably also want to make it dynamic to add new segments automatically if the vertex count is changeable.
Because this is a common task, there’s a dedicated node for it called
UV Connect (link) , but just like the Circle generator nodes we will avoid using that for the same reason. Learning how to build these things yourself is the best way to learn Visual Programming with nodes.
Generating an index list for the polygon
In order to make the list automatically, we should know how many vertices there are at any given moment.
Add -> List Main -> List Length
The List Length node lets you output the length of incoming data, it also lets you pick what level of the data you want to inspect. It’s worth reading the reference of this node for a comprehensive tour of its capabilities.
- hook the Vector In output into the Data input of List Length
- hook a new Stethoscope up to the output of the List Length node.
- notice the Level slider is set to 1 by default, you should see Stethoscope shows output.
Notice that, besides all the square brackets, you see the length of the incoming data is 4, as expected. We want to generate a sequence (list) that goes something like
[0,1,2,...n] where n is the index of that last vertex. In Python you might write something like this this:
n = 4 range(start=0, end=n, step=1) >> [0,1,2,...n]
To generate the index list for the polygon we need a node that outputs a sequential list of integers, Sverchok has exactly such a node and it accepts values for start, step and count as parameters. This is what the Range Integer (count mode) node does.
Add -> Numbers -> Range Int
- Set the mode of List Range int to Count
- Make sure start is 0 and step is 1
- Hook the output of List Length into the count socket of Range Int
- Disconnect the Formula node from the EdgPol socket
- Connect the output of Range Int into EdgPol instead
- Optionally you can connect a Stethoscope also to the output of Range Int in order to see the generated list for yourself
Generating the set of circular verts
The 4 verts we’ve had from the very beginning are already points on a circular path, we can make a simple change to finally see this Circle emerge.
- Set the mode of the Float series node to Range
- Set the stop parameter to 2.0
- Set the step to 0.2 for example.
2.0 / 0.2 = 10, this means the Float Series node will now output
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8]. Notice that it does not output 2.0 at the end, because this mode excludes the terminating value. (called non inclusive)
You can see the beginnings of a circle.
Forcing an even spread of Vertices
Above we have the step set to 0.2, this manually sets the distance but calculation of this step value soon gets cumbersome. We will add nodes to do the calculation for us. Think about how you might do that.
I would want to have something like
1 / number_vertices, this calls for a Math node and an Int to represent the whole number of vertices.
Add -> Numbers -> Math
Add -> Numbers -> Int
- Set the Math node mode to
/ (division), and put 1.0 in the numerator
- Connect the Int node into the bottom socket of the division Math node
- Adjust the integer value on the Int node to 18 for example
- In the image below I’ve connected a Stethoscope to the output of the Math Node to see the value of this computation
- Finally, hook up the output of the division Math node into the step socket of Float series
You should see something like this, if not you can by now probably figure out what to do.
Notice this is starting to get crowded, let’s minimize nodes
Before going any further I would like to draw attention to the fact that you can make nodes smaller. This minimizing feature is called hide, we can argue about how good or badly that option is named. With Any node selected press H, to ‘minimize/hide’.
In Sverchok we added special functionality to certain nodes to draw information about themselves into their header area. This allows you to see what the node is supposed to be doing even when the UI is minimized. Currently the Int, Float, Math, Vector Math nodes have this behaviour because they are essential nodes and used very often.
In future lessons you will often see minimized/hidden nodes
Polygon is easy, what about Edges?
Remember, there are nodes that can take an incoming set of vertices and generate the required Edges index lists. But we’re trying to explore the modular features of Sverchok – we’ll build our own Edges generator this time.
The edge index list of the square looked like
[[0,1],[1,2],[2,3],[3,0]]. For the Circle of a variable number of vertices that list will look like
[[0,1],[1,2],...,[n-1,n],[n,0]]. Notice i’m just showing the start of the list and the end, to indicate that there might be a formula for it based purely on how many verts you want to link.
In python you might express this using a for loop or a list comprehension:
# for loop n = 5 for i in range(n): print(i, (i+1) % n) >> 0 1 >> 1 2 >> 2 3 >> 3 4 >> 4 0 # list comprehension n = 5 edges = [[i, (i+1) % n] for i in range(n)] print(edges) >> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]]
In Sverchok the end result will be the same, but we’ll arrive at the result in a different way.
The second index of each edge is one higher than the first index, except for the last edge. The last edge closes the ring of edges and meets back up with the first vertex. In essenence this is a wrap-around. Or, you can think of it as two lists, one of which is shifted by one with respect the other list.
Sverchok has a node for this called List Shift. We’ll zip the two lists together using List Zip node.
add -> List Struct -> List Shift
add -> List Main -> List Zip
- Hook the output of List Range Int into the first Data socket of the List Zip node.
- Hook the output of List Range Int also into the List Shift node.
- To make the wrap-around, simply set the Shift slider to 1.
- connect the output of List Shift to the second Data input of List Zip.
- Make sure the level parameter on List Zip is set to 1.
- Hook up a Stethoscope to the output of List Zip to verify
Notice in this image I have minimized/hidden (shortcut H) a few nodes to keep the node view from getting claustrophobic.
- Or hook up the output of List Zip straight into the EdgPol socket of`Viewer Draw`.
End of lesson 02
Save this .blend you’ve been working on as Sverchok_Unit_01_Lesson_02 for future tutorials or as reference if you want to look something up later if you like.
You now know how to create basic shapes programmatically using Sverchok nodes. In Lesson 03 a dynamic grid will be generated, but first relax and reiterate what has been learned so far.
Viewer Draw automatically generates Edges when you pass one or more Vertices and Polygons. This means in practice when you already have the Polygons for an object then you don’t need to also pass in the Edges, they are inferred purely from the indices of the incoming Polygons.