-
Notifications
You must be signed in to change notification settings - Fork 635
How To Create Your Own Nodes
Dynamo has a large assortment of core nodes that you can rely on to build pretty much any graph you need, but sometimes a quicker, more elegant, and more easily shared solution is to build your own nodes. These can be reused among different projects, they make your program clearer and cleaner, and they can be pushed to the package manager and shared.
Dynamo also offers three different methods for importing and creating custom nodes. Each method gives you some more control of how the node interacts with the graph, and they get increasingly complex to build. For most day to day work you'll probably want to stick with custom nodes built directly in dynamo using the UI or Zero Touch importing, which lets you import a .dll of c# code that will get turned automatically into a series of nodes. The last option is to build explicit custom nodes in c#. Nodes built this way have the most flexibility, you can create custom UI's, respond to other nodes, or effect the state of the graph.
###Custom Nodes
Custom Node usually refers to a node that was built in the Dynamo UI and was constructed by nesting other nodes and custom nodes inside of a container. When this container node is executed in your graph, everything inside it will be executed. You can even place instances of this same node inside of itself to build recursive custom nodes!
To build a custom node you'll need to either start a new custom node or select some existing nodes in your dynamo graph, right click on the canvas, and hit node from selection.
Once you're in the yellow background canvas, you know you're working inside of a custom node. Custom nodes require a few parts:
Inputs - input nodes create input ports on this custom nodes whenever it is placed, strictly speaking these are not necessary, since you could build a custom node that only has an output. Like a constant value.
Outputs - these nodes are necessary and dynamo will create outputs automatically for you if you don't specify one. It's good practice to define these explicitly. Similar to inputs, these will create outports on the custom node when it's used in another graph.
####Use
In Dynamo, many nodes are functions, this means they have some input, and they produce some output. Your custom nodes are functions too! This means you can map them over a list, use them to sort a list, or filter a list, they can even be the function you pass to reduce. These are all built-in nodes that take functions as one of their inputs.
You can tell that a node is acting as a function if its title bar is light grey. This means that one of its input arguments is left blank. For instance when we map this function over a list, we'll execute the custom node once for each item in the list and each time the current item will get set to that input. Thats the super quick functional programming intro - for more information check out the function passing sample in the help menu of Dynamo.
... ####Sharing
Any set of custom nodes you design and build in the UI can be uploaded to the package manager and shared. You can supply tags for others to search for it, and you can upload different versions to make fixes and changes. At this time only custom nodes built in the UI can be shared on the package manager. A much requested feature that is being worked on is the ability to upload .dlls and share nodes built with the other two methods.
This method is internally referred to as Zero Touch Import. The idea is that you can use c# to code a series of types, static methods, and static constructors which will get automatically converted into nodes and exposed inside of dynamo when you import the library. A library is a .dll file which is a complied version of a library project file.
TODO (Mike/Patrick) Fold in https://github.com/DynamoDS/Dynamo/wiki/Zero-Touch-Plugin-Development
You might want to create a custom UI for displaying an image, building some kind of interactivity, like a slider or dropdown box, or getting some feedback about the state of the node.
The color range node is an example of a node that requires a custom UI. It draws the specified color range gradient into the dynamo graph. We’ll be looking at this node as an example of a custom UI node. The portions of code below are pulled directly from the dynamo source code. The src of this node is locate at Dynamo\src\Libraries\CoreNodesUI\ColorRange.cs
When creating nodes that require custom UI, exposing static constructors and methods won’t work. We need to override a few inherited methods. These are inherited from IWpfNode and NodeModel.
These methods include: SetupCustomUIElements(dynNodeView view) to build our custom UI using Windows Presentation Foundation (WPF)
BuildOutputAst(List inputAstNodes) where you transform your inputs into your outputs and wrap them in the correct node data types,
DispatchOnUIThread(delegate) which will call the code you use to update the UI of your node. WPF requires that you only make changes to data bound to the UI on the UI thread. This method lets you make sure you execute those changes on the UI thread.
UI is created using Windows Presentation Foundation elements. The initial UI is built with c# in the method SetupCustomUIElements(dynNodeView view). Here you’ll usually add panels, buttons, and dropdowns to the inputgrid element of your node’s view, you can think of this as the root of the node’s UI.
Besides overriding these methods, the other major difference is the constructor, which is no longer static. in and out ports must be defined explicitly, as can be seen below in the constructor ColorRange(), which takes no parameters, this is unlike zero touch nodes.
Looking for help with using the Dynamo application? Try dynamobim.org.
- Dynamo 2.0 Language Changes Explained
- How Replication and Replication Guide work: Part 1
- How Replication and Replication Guide work: Part 2
- How Replication and Replication Guide work: Part 3