Multi_Switch Node
A few months ago I introduced a new data type, multi-lists:
http://support.nodebox.net/discussions/show-your-work/886-multi-lists
Multi-lists are a way of combining multiple separate lists into a single list. The multi-list node works like a combine node except that its output can later be decomposed back into separate lists.
The node you use to do that decomposition is multi_switch. It's very simple and takes only two parameters:
- Multi-list. A multi-list (the output of a multi_list node)
- Index. The list you want to isolate
For example, suppose you make a subnetwork which outputs a multi-list containing three separate lists. If you feed that output into a multi_switch and set the index to 0, you'll get the first list. An index of 1 will get the second list, an index of 2 will get the third list, and so forth.
The attached demo shows an example.
I create a subnetwork called shape_wheel which takes a shape, explodes it into segments, and returns a multi-list containing three lists:
- List 0. A one-item list containing the shape
- List 1. A list of the shape's segment lengths
- List 2. A list of colors representing each segment type: blue for straight edge, red for curve
The demo takes the single multi-list coming out of shape_wheel and decomposes it into three lists. It uses list 0 to display the shape, and lists 1 and 2 to create a wheel of colored lines where each line represents a shape length and type. A rectangle shape would produce a 4-line wheel, all blue, with two lines representing its width and two its height. A circle (which in Nodebox consists of 4 quarter-curves) would produce a 4-line wheel, all red. A more complex shape like a text path would produce a wheel with a mix of blue and red lines with lengths corresponding to the length of each segment.
The shape_wheel produces a kind of fingerprint of a shape that shows how complex it is, and how angular or curvy it is. You could use it, for example, to compare the same character in several different fonts.
If you want to see how it looks with a single character, change the text field of my charpath node to, say, lower case a. Try different characters and different fonts to see how it works.
For a single character, the three lists inside the multi-list output of shape_wheel show just that character's shape, its segment lengths, and its segment types. But if you reset my charpath node input to "abcdefghi", list 0 will show all 9 characters, list 1 will be a combined list of all their segment lengths, and list 2 will be combined list of all the segment colors. This allows you to create a single shape wheel for a phrase or other string of characters. Multi_switch makes this easy.
To make this distinction clearer I made a separate subnetwork, draw_wheel. Draw_wheel is just shape_wheel plus the same nodes I used to turn lists 1 and 2 into a color wheel. Instead of outputting a multi-list, it outputs the completed wheel as a group. Because it takes one shape at a time, if you feed it a list of 9 shapes, instead of producing a single multi-list, it outputs 9 separate wheels. Those nine wheels are shown at the bottom of the canvas.
Multi-lists are particularly useful in subnetworks when you need to output different kinds of information which may have variable lengths. In this case, for a single shape, the output of shape_wheel will contain one item for the shape and then a variable number of length and color values. It would be harder to decompose this output from an ordinary list. You could break shape_wheel into two separate subnetworks, one to compute lengths and one to compute colors, but this would force you to reinvent parts of the process in both, and future changes to one might have to also be made to the other. This is messier, less efficient, and results in a larger overall network that is harder to maintain.
I hope this makes it clear how to use the multi_switch node. I included multi_list in the last rev of my library (3.6), but did not show multi_switch as a separate node. Instead, I placed it inside the demo for multi_list. Now, having used both of them extensively over the last few months, I realize they really do work as a pair, and both should have equal billing as primary nodes.
So here it is. Enjoy!
-
multi_switch_screenshot.png
1.14 MB
- multi_switch_demo.ndbx.zip 153 KB
Keyboard shortcuts
Generic
| ? | Show this help |
|---|---|
| ESC | Blurs the current field |
Comment Form
| r | Focus the comment reply box |
|---|---|
| ^ + ↩ | Submit the comment |
You can use Command ⌘ instead of Control ^ on Mac