Simply put, Code Blocks are the bomb. If you’ve been following their development through the last six months of pull requests and Dynamo daily builds, you’ll know what I mean. There’s a lot going on in these little guys, and here is the CliffsNotes version of a how-to guide.
“Where did this come from!?” you ask. Dynamo and DesignScript, a text-based language for computational design, joined forces, and now there are more ways to make a series of points than I can count. And a lot of other stuff too.
It’s two good things in one. Like chocolate and peanut butter—chocobutter. Or if Brad Pitt and Angelina Jolie had a baby—that happened!
You too can make your own Code Blocks by double-clicking in the Dynamo canvas in version 0.7.0 or later.
Numbers, strings, and formulas
Code Blocks can hold numbers, strings, and formulas. You won’t even need those old-school nodes anymore. Enter a number like you would in the number node. Use quotation marks for strings. You can do any math in a Code Block that you could do in a Formula node. There are some differences, though; check out this post for a fuller comparison.
All statements, or lines, in a Code Block must end with a semicolon. You’re allowed to be lazy for the last line you write; Dynamo will fill it in for you.
Calling other nodes
You can call any regular node in the library through a Code Block as long as the node isn’t a special “UI” node: those with a special user interface feature. For instance, you can call Circle.ByCenterPointRadius, but it wouldn’t make much sense to call a Watch 3D node.
Regular nodes (most of your library), generally come in three types:
Create | Create something |
Action | Perform an action on something |
Query | Get a property of something that already exists |
You’ll find that your library is organized with these categories in mind. Methods, or nodes, of these three types are treated differently when invoked within a Code Block.
Create.
When you use a method to create something, call it by name and specify the inputs in the order found in the out-of-the-box node.
Create-type nodes that have no inputs still require parentheses.
Action.
An action is something you do to an object of that type. Dynamo uses dot notation, common to many coding languages, to apply an action to a thing. Once you have the thing, type a dot then the name of the action. The action-type method’s input is placed in parentheses just like create-type methods, only you don’t have to specify the first input you see on the corresponding node since that’s the thing you are acting on, and you already know it.
For example, if I want to call the node Point.Add from a Code Block, I notice that this is an action-type node. It’s inputs are (1) the point, and (2) the vector to add to it. In a Code Block, I’ve named the point (the thing) “pt”. To add a vector named “vec” to “pt,” I would write pt.Add(vec)
, or: thing, dot, action. The Add action only has one input, or all the inputs from the Point.Add node minus the first one. The first input for the Point.Add node is the point itself, which you already know or you wouldn’t be here.
Query.
Query-type methods get a property of an object. Since the object itself is the input, you don’t have to specify any inputs. No parentheses required.
Flexibility
With Code Blocks, a user has flexibility to decide how to specify inputs. Here are several different ways to make the point (10, 5, 0).
As you learn more of the available functions in the library, and you know what you want, you might even find that typing “Point.ByCoordinates” is faster than searching in the library and finding the proper node.
Make lists and get items from a list
Make lists with braces (a.k.a. “curly brackets”). Get items from a list with brackets (a.k.a. “square brackets”).
Working with nested lists is just a variation on the theme.
Replication guides
Replication guides set how several one-dimensional lists should be paired. It’s like list lacing but with more control. Use numbers in angle brackets with the inputs for a method. The hierarchy of the resulting nested list of results will be determined by the order of the numbers: <1>, <2>, <3>, etc.
In this example, I have lists of 2 x-values and 5 y-values. If I don’t use replication guides with these mismatched lists, I would get two points as a result, the same as the length of the shortest list. Using replication guides, I can find all of the possible combinations of 2 and 5 coordinates. And I can specify which list will be dominant: 2 lists of 5 things or 5 lists of 2 things. In the example, changing the order of the replication guides makes the result a list of rows of points or a list of columns of points in a grid.
Make your own functions. Seriously.
Functions can be defined directly in a Code Block and called elsewhere in the Dynamo definition.
The first line has the key word “def”, then the function name, then the names of inputs in parentheses. Braces define the body of the function. Return a value with “return =”. Code Blocks that define a function do not have input or output ports because they are called from other Code Blocks.
Call the function with another Code Block in the same file by giving the name and the same number of arguments. It works just like the out-of-the-box nodes in your library.
** This image was not altered! The Code Block on the right calls the function defined in the Code Block on the left. Same Dynamo file. “Look Mom, no wires!”
Find these and other examples in the Code Block Sample File in the Dynamo Help Menu: Help/Samples/Core/CoreCodeBlocks.