Updating your Packages and Dynamo Libraries for Dynamo 2.x
Last updated
Last updated
Dynamo 2.0 is a major release and some APIs have been changed or removed. One of the largest changes that will effect node and package authors is our move to a JSON file format.
In general Zero Touch node authors will have little to no work to do to get their packages running in 2.0.
UI nodes and nodes that derive directly from NodeModel will take more work to get running in 2.x.
Extension authors may also have some potential changes to make - depending on how much of the Dynamo core APIs they use in their extensions.
Do not bundle Dynamo or Dynamo Revit .dlls with your package. These dlls will already be loaded by dynamo. If you bundle a different version than the user has loaded (ie you distribute dynamo core 1.3 but the user is running your package on dynamo 2.0) mysterious runtime bugs will occur. This includes dlls like DynamoCore.dll
, DynamoServices.dll
, DSCodeNodes.dll
, ProtoGeometry.dll
Do not bundle and distribute newtonsoft.json.net
with your package if you can avoid it. This dll will also be loaded by dynamo 2.x already. The same issue as above can occur.
Do not bundle and distribute CEFSharp
with your package if you can avoid it. This dll will also be loaded by Dynamo 2.x already. The same issue as above can occur.
In general avoid sharing dependencies with dynamo or revit if you need to control the version of that dependency.
1) Upon opening a graph some nodes have multiple ports with the same name, but the graph looked fine when saving. This issue can have a few causes.
The common root cause is because the node was created using a constructor that recreated the ports. Instead a constructor which loaded the ports should have been used. These constructors are usually marked [JsonConstructor]
see below for examples
This can occur because:
There was simply no matching [JsonConstructor]
, or it was not passed the Inports
and Outports
from the JSON .dyn.
There were two versions of JSON.net loaded into the same process at the same time causing .net runtime failure so that the [JsonConstructor]
attribute could not be used correctly to mark the constructor.
DynamoServices.dll with a different version than the current dynamo version has been bundled with the package and is causing the .net runtime to fail to identify the [MultiReturn]
attribute so zero touch nodes marked with various attributes will fail to have them applied. You might find that a node returns a single dictionary output instead of multiple ports.
2) Nodes are completely missing upon loading the graph with some errors in the console.
This might occur if your deserialization failed for some reason. It's good practice to serialize only properties you need. We can use [JsonIgnore]
on complex properties you don't need to load or save to ignore them. Properties like a function pointer, delegate, action,
or event
etc. These should not be serialized as they will usually fail to deserialize and cause a runtime error.
Organizing Custom Nodes in librarie.js
Known Issues:
A coinciding custom node name and category name at the same level in librarie.js causes unexpected behavior. QNTM-3653 - avoid using the same names for category and nodes.
Comments will be turned into block comments instead of line comments.
Short type names will be replaced with full names. For example if you did not specify a type when you load the custom node again you will see var[]..[]
- as this is the default type.
In Dynamo 2.0 List and Dictionary types have been split and the syntax for creating Lists and Dictionaries has been changed. Lists are initialized using []
while dictionaries use {}
.
If you were previously using the DefaultArgument
attribute to mark parameters on your zero touch nodes and used list syntax to default to a specific list like someFunc([DefaultArgument("{0,1,2}")])
- this will no longer be valid, and you will need to modify the DesignScript snippet to use the new initialization syntax for lists.
As noted above do not distribute Dynamo dlls with your packages. (DynamoCore
, DynamoServices
etc).
Node Model nodes require the most work to update to Dynamo 2.x. At a high level you will need to implement constructors that will only be used for loading your nodes from json in addition to the regular nodeModel constructors that are used for instantiating new instances of your node types. To differentiate between these you mark the load time constructors with [JsonConstructor]
which is an attribute from newtonsoft.Json.net.
The names of the parameters in the constructor should generally match the names of the JSON properties - though this mapping gets more complicated if you override the names that are serialized using [JsonProperty] attributes. See the Json.net documentation for more information.
The most common change required for updating nodes derived from the NodeModel
base class (or other dynamo core base classes, ie DSDropDownBase
) is the need to add a JSON constructor to your class.
Your original parameter-less constructor will still handle initializing a new node that is created within Dynamo (via the library for example). The JSON constructor is required to initialize a node that is deserialized (loaded) from a saved .dyn or .dyf file.
The JSON constructor differs from the base constructor in that it has PortModel
parameters for the inPorts
and outPorts
, which are provided by the JSON loading logic. The call to register the ports for the node are not required here, as the data exists in the .dyn file. An example of a JSON constructor looks like this:
using Newtonsoft.Json; //New dependency for Json
………
[JsonConstructor] //Attribute required to identity the Json constructor
//Minimum constructor implementation. Note that the base method invocation must also be present.
FooNode(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts) { }
This syntax :base(Inports,outPorts){}
calls the base nodeModel
constructor and passes the deserialized ports to it.
Any special logic that existed in the class constructor that involves initialization of specific data that is serialized into the .dyn file (for example setting Port registration, lacing strategy, etc) are not required to be repeated in this constructor as these values can be read from the JSON.
This is the major difference between the JSON constructor and non-JSON constructors for your nodeModels. JSON constructors are invoked when loading from a file and are passed loaded data. Other user logic however must be duplicated in the JSON constructor (for example, initializing event handlers for the node or attaching).
Examples can be found here in the DynamoSamples repo -> ButtonCustomNodeModel, DropDown, or SliderCustomNodeModel
Previously a developer could serialize and deserialize specific model data to the xml document via the SerializeCore
and DeserializeCore
methods. These methods still exist in the API but will be deprecated in a future release of Dynamo (an example can be found here). With the JSON.NET implementation now public
properties on the NodeModel derived class can be serialized to the .dyn file directly. JSON.Net provides multiple attributes to control how the property is serialized.
This example which specifies a PropertyName
is found here in the Dynamo repo.
[JsonProperty(PropertyName = "InputValue")]
public DSColor DsColor {...
Note
If you make your own JSON.net converter class, Dynamo does not currently have a mechanism to let you inject it into the load and save methods and so even if you mark your class with the [JsonConverter]
attribute it may not be used - instead you can call your converter directly in your setter or getter. //TODO need confirmation of this limitation. Any evidence is welcome.
An example that specifies a serialization method to convert the property to a string is found here in the Dynamo repo.
[JsonProperty("MeasurementType"), JsonConverter(typeof(StringEnumConverter))]
public ConversionMetricUnit SelectedMetricConversion{...
public
properties that are not meant for serialization need to have the [JsonIgnore]
attribute added. When the nodes is saved to the .dyn file this insures this data is ignored by the serialization mechanism and will not cause unexpected consequences when the graph is opened again. An example of this can be here in the Dynamo repo.
As mentioned above SerializeCore
and DeserializeCore
methods were used in the past to save and load nodes into the xml .dyn file. In addition they were also used to save and load the node state for undo/redo and still are! If you wish to implement complex undo/redo functionality for your nodeModel UI node then you will need to implement these methods and serialize into the XML document object provided as a parameter to these methods. This should be a rare use case except for complex UI nodes.
One common occurrence in nodeModel nodes affected by 2.0 API changes is port registration in the node constructor. Looking at examples in the Dynamo or DynamoSamples repo you previously will have found use of the InPortData.Add()
or OutPortData.Add()
methods. Previously in the Dynamo API the InPortData
and OutPortData
public properties were marked as deprecated. In 2.0 these properties have been removed. Developers should now use InPorts.Add()
and OutPorts.Add()
methods. Additionally these two Add()
methods have slightly different signatures:
InPortData.Add(new PortData("Port Name", "Port Description")); //Old version valid in 1.3 but now deprecated
vs
InPorts.Add(new PortModel(PortType.Input, this, new PortData("Port Name", "Port Description"))); //Recommended 2.0
Examples of converted code can be found here in the Dynamo Repo -> DynamoConvert.cs, or FileSystem.cs
The other common use case that is affected by the 2.0 API changes relates to the methods commonly used in the BuildAst()
method to determine node behavior based on the presence or absence of port connectors. Previously HasConnectedInput(index)
was used to validate a connected port state. Developers should now use the InPorts[0].IsConnected
property to check the port connection state. An example of the this con be found in ColorRange.cs in the Dynamo Repo.
Let's walk through upgrading a 1.3 UI node to Dynamo 2.x.
All we need to do to this nodeModel
class to get it loading and saving correctly in 2.0 is add a jsonConstructor to handle loading of the ports. We simply pass the ports on the base constructor and this implementation is empty.
Note: Do not call RegisterPorts()
or some variation of that in your JsonConstructor - this will use the input and output parameter attributes on your node class to construct new ports! We don't want this, since we want to use the loaded ports which are passed to your constructor.
This example adds the minimal loading JSON constructor possible. But what if we need to do some more complex construction logic, like setup some listeners for event handling inside the constructor. The next sample taken from the
DynamoSamples Repo is linked above in the JsonConstructors Section
of this document.
Here is a more complex constructor for a UI node:
When we add a JSON constructor for loading this node from a file we have to recreate some of this logic, but note that we do not include the code that creates ports, sets lacing, or sets the default values for properties which we can load from the file.
Note that other public properties that were serialized into the JSON like ButtonText
and WindowText
will not to be added as explicit parameters to the constructor - they are set automatically by JSON.net using the setters for those properties.