Subnetworks - Multiple Outputs?

edv's Avatar

edv

26 Mar, 2015 06:07 PM

Is it possible to publish multiple outputs of a subnetwork?

For example, I'd like to pass through the output (geometry) along with other calculations made within the subnetwork, like distances between points, ect.

I can't seem to find anything about it.

  1. Support Staff 1 Posted by john on 27 Mar, 2015 09:00 PM

    john's Avatar

    I am a relatively new user of NodeBox but perhaps I can attempt an answer.

    NodeBox is a functional language, so each node corresponds to a function with a single output. This is a core principle, so technically the answer to your question is no, a subnetwork can have only one output.

    But in practice you can finesse this a bit. That one output can be any rendered node, so it's possible to output complex objects like shapes or zipMaps that can hold multiple pieces of information.

    The trick to accessing those multiple pieces of information is the Lookup node. You can perform different lookups on the output of a subnetwork to recover multiple pieces of information.

    In order to use a lookup you need to provide a key - and valid keys of node box objects are not well documented (to put it mildly). You can get a fighting chance of guessing at keys by using the new "Keys" node. Attach it the output of your subnetwork and you'll get a list of keys that will work with that object. Some valid lookup keys are not obvious from this list. For example, "bounds" is commonly available as a key, but by using dot notation you can access additional properties like "bounds.width" or "bounds.x". A little experimentation can sometimes uncover the incantation you need.

    I show a nice example of this in response to the Recursion question you posted in the NodeBox 2 & 3 forum. You (or someone) closed the discussion so I had to start a new one, called "Recursive Pentagons".

    The solution I provided includes a subnetwork that takes a parent pentagon as an input and produces a child pentagon as an output. To calculate the size and position of the child pentagon I had to access multiple pieces of information about the parent pentagon. I used a centroid node to recover the parent's position and used a "length" lookup to recover the parent's path length (all shapes in NodeBox are really just paths), which I then used to derive the parent's radius.

    A more straightforward method of outputting multiple pieces of information is to create a zipMap. You can create an arbitrarily long list of key-value pairs by feeding a list of keys and a list of values into a ZipMap node. You can use Make Strings nodes to build both lists and then render the ZipMap mode when you create your subnetwork.

    You can then recover all the pieces of information stored in your subnetwork by attaching it to different lookup nodes (one for each key you need). The drawback of this approach is that you may need your subnetwork to do something other than produce a zipMap. In that case all you can do is break up your code into multiple subnetworks, one to produce the zipMap and another to do stuff with it.

    So it's rather tricky to encode multiple pieces of information into the single output of a subnetwork, but it can be done.

    Hope this helps!

    John

  2. 2 Posted by edv on 28 Mar, 2015 12:56 AM

    edv's Avatar

    Hey John, Thanks for the detailed response, and for the example on the other thread! I closed the recursive thread because I solved it after noodling around, but I must admit your solution is far more elegant!

    Thanks again.

    Evan

  3. 3 Posted by Rory on 31 Mar, 2015 11:43 AM

    Rory's Avatar

    John is a legend!

  4. Support Staff 4 Posted by john on 08 Sep, 2015 09:54 AM

    john's Avatar

    A postscript...

    I recently faced another sitation where I needed multiple outputs from a single subnetwork. The subnetwork takes a csv file and creates a detailed org chart with lots of precisely placed lines and boxes. But in addition to drawing the chart I also need a way of locating individuals within it so that I can color or highlight or animate their boxes.

    My soultion was to simply combine three different objects (using an ordinary Combine node): a group containing all the lines, a group containing all the boxes (drawn in a particular order), and a lookup table (a list of zipmaps) containing each employee name and the position of his/her box within the chart.

    NodeBox has no trouble outputting a heterogeneous collection like this - it's just another list.

    In order to draw the chart from the output of this subnetwork I just feed it into a First node to draw the lines and a Second node to draw the boxes. To access my lookup table I just feed it into two successive Rest nodes; the remaining items form the table.

    Voila! Three very different outputs from a single subnetwork node.

    John

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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

Recent Discussions

10 Sep, 2024 03:41 AM
09 Sep, 2024 02:15 PM
07 Sep, 2024 05:16 AM
05 Sep, 2024 02:21 AM
04 Sep, 2024 05:01 AM

 

03 Sep, 2024 10:07 AM
02 Sep, 2024 05:56 AM
31 Aug, 2024 11:07 PM
27 Aug, 2024 12:08 AM
26 Aug, 2024 01:02 PM
26 Aug, 2024 07:18 AM