Implement node options
Customize node behavior and structure with configurable options.
Read time 3 minutesLast updated 25 days ago
Customize node behavior and structure with configurable options.
Use node options to dynamically customize node behavior and structure without creating multiple node variants. Configure the node option settings to use a single node type in different scenarios. For example, you can use node options to dynamically adjust both the number of ports and their data types. With node options, you can simplify the node hierarchy while maintaining flexibility.
This page covers how to implement node options only. For information on how to implement nodes, refer to Implement nodes for your graph tool. For information about node types, refer to About nodes.
Prerequisites
Add a node option to change the number of ports of a node
To create a node with a configurable number of ports:
-
Define a constant name for the port count option.
-
Overrideto create the option with a default value.
OnDefineOptions -
Callto defer processing until the input is complete.
Delayed()const string k_PortCountName = "PortCount"; protected override void OnDefineOptions(IOptionDefinitionContext context) { context.AddOption<int>(k_PortCountName) .WithDisplayName("Port Count") .WithDefaultValue(2) .Delayed(); } -
Implementto create ports based on the option value as follows:
OnDefinePortsprotected override void OnDefinePorts(IPortDefinitionContext context) { var portCountOption = GetNodeOptionByName(k_PortCountName); portCountOption.TryGetValue<int>(out var portCount); for (var i = 0; i < portCount; i++) { context.AddInputPort<Vector2>($"{i}").Build(); } context.AddOutputPort<Vector2>("result").Build(); }
The method retrieves the option using its name, and then extracts its current value. The node can dynamically adjust its structure based on user configuration.
GetNodeOptionByNameTryGetValueOpen the graph and instantiate the node to check the result.
Add a node option to change port types of a node
To create a node with switchable port data types:
-
Define an enum for your type options.
-
To create an option that uses this enum as its type, override.
OnDefineOptions -
To determine port data types in yourimplementation, use the selected enum value.
OnDefinePortsenum PortTypes { Vec2, Vec3 } const string k_PortTypeName = "PortType"; protected override void OnDefineOptions(IOptionDefinitionContext context) { context.AddOption<PortTypes>(k_PortTypeName).WithDisplayName("Port Type"); } protected override void OnDefinePorts(IPortDefinitionContext context) { var portTypeOption = GetNodeOptionByName(k_PortTypeName); portTypeOption.TryGetValue<PortTypes>(out var portType); if (portType == PortTypes.Vec3) { context.AddOutputPort<Vector3>("result").Build(); } else { context.AddOutputPort<Vector2>("result").Build(); } } -
Go back to the graph, and use the two new options to change the layout of the node.
Complete node option example
[Serializable] public class NodeWithOptions : Node { enum PortTypes { Vec2, Vec3 } const string k_PortCountName = "PortCount"; const string k_PortTypeName = "PortType"; protected override void OnDefineOptions(IOptionDefinitionContext context) { context.AddOption<int>(k_PortCountName) .WithDisplayName("Port Count") .WithDefaultValue(2) .Delayed(); context.AddOption<PortTypes>(k_PortTypeName); } protected override void OnDefinePorts(IPortDefinitionContext context) { var portCountOption = GetNodeOptionByName(k_PortCountName); portCountOption.TryGetValue<int>(out var portCount); for (var i = 0; i < portCount; i++) { context.AddInputPort<Vector2>($"{i}").Build(); } var portTypeOption = GetNodeOptionByName(k_PortTypeName); portTypeOption.TryGetValue<PortTypes>(out var portType); if (portType == PortTypes.Vec3) { context.AddOutputPort<Vector3>("result").Build(); } else { context.AddOutputPort<Vector2>("result").Build(); } } }