In today's post, we are going to discuss how the use of Dictionaries in Dynamo can simplify a script's organization and streamline its logic and data flow. Stay tuned for part 2 which is a Demo on how to put this into practice.
If you have ever worked on a complex Dynamo script, you have likely realized that as the complexity of a script increases, the amount of spaghetti also increases thus requiring more time spent organizing or managing lists.
This likely works in the moment but as soon as you take a break from the script and then come back to it at a later point, the spaghetti nodes can be quite difficult to untangle. Maintenance or refactoring can be quite the pain.
Dictionaries are a great way to combat the difficulties of spaghetti nodes in your script. With a little bit of upfront work thinking through the organization of your data, you can maintain most, if not all, of the data you need within one node. From there, it becomes a simple exercise of calling the information you need to modify, working with it, and then pushing it back to the script.
So what is a dictionary? In the image above, you see that a Dictionary is an object made up of Keys and Values. The Key are the name of the information. By calling upon a key, you get access to that information. You can make Dictionaries in Dynamo either by using the Dynamo provided nodes or by using a Code Block with { } to label it as a Dictionary and : to separate the Keys and Values.
For instance, the images above show an initial template Dictionary called “Story” that is duplicated and pushed to a list so that we can now use the list of “Stories” to create the tower. Each Story Dictionary in the list is individually editable as a separate object. Because of this, there is very little spaghetti going on in the script. Each group represents an encapsulated piece of logic that takes in the list of Story Dictionaries, as well as a maybe a user input or two from the orange group at left, pulls out the desired properties from that list of Stories, and outputs the list of Dictionaries that has had its Values modified.
Zooming in on one of those groups, we can see how this group has three inputs: one list of Dictionaries and two user inputs. It then uses the Dictionary.ValueAtKey provided by Dynamo to get the values that we need for this particular group of logic. It can use those values, for instance, to create new geometry (not shown) or sort and pass a particular color to each floor of the tower-based on program type. We perform similar actions throughout the script managing separate groups of logic in the tower.
While this does add overhead to the creation of each encapsulated piece of logic – getting values and setting values requires a new use of nodes each time – Dictionaries are often worth the effort because of how simple it keeps the script. We know exactly what needs to go in and out of the script at each group and we can use that to more easily maintain and modify it in the future. In Part 2 of this video, we will demo that simplicity of modification.
Another thing to note is that Dictionaries are great up to a point. However, once a script reaches a certain point of complexity, it is worth considering the use of a zeroTouch node, with true object-oriented programming through C#, rather than maintaining Dictionaries.
About the Author
Chris Woodward
Design Technologist
As a design technologist, Chris combines a true fascination with both design and technology to streamline design processes and construct computational design solutions. His interest in coding can be traced back to video games mods. A more rigorous exploration started as an M.Arch Student at the University of Illinois Champaign-Urbana where he dug deep into Rhino and Grasshopper. Then, while working as an architectural designer, Chris brought this technology interest into world BIM with Revit and Dynamo. Along the way, Chris has learned a myriad of programming languages such as Python, JavaScript and C# allowing him further access to custom solutions. Not one to get too caught up in just one thing, Chris supplements has supplemented his passion in design technology with a number of hobbies and interests including mechanical keyboards, urban design, web design, and a budding interest in fountain pens. Chris can also often also be found outdoors on long-distance run or hitting the tennis or basketball courts.
By focusing on one of those groupings, it is evident that doodle jump it has three inputs: one list of Dictionaries and two user inputs.