Skip to content

Adding a custom node

blockout22 edited this page Feb 6, 2022 · 1 revision

Requirements

you need to setup your plugin first click here if you haven't done so already

Creating your Main Class

Before we can add any custom nodes we need a create Master class that gets called when we load our graph, the master class will also give you access to the graph window which also gives access to a bunch of other stuff related to the graph it's self

Create a new class that extends VisualScriptingPlugin from there your IDE should force you to implement the init Method, this class is where you will add all your custom nodes to but more on that later.

creating a node

let's create a simple node that adds 2 numbers together, first create a new Class that extends Node (visual.scripting.node.Node)

inside the constructor you can setup a few things:-

'setName("Node Name")' set the Name of the node, these names appear on the right click context menu and the title bar of the Node

setLanguages(new String(){}) lets you specific the exact languages these nodes are intended for, the Graph editor will filter these out from the context menu when a different language file is open... leave black/or don't call if you want it to appear with any language

'setCategory(somecat.subcat.anothersubcat)' create embedded menus to organize the right click context menu

since we are create a implementing a node that adds numbers we need to override 3 methods:- init() execute() and printSource(StringBuilder sb)

inside the init() method we setup our pins, create 3 new pins, 2 inputs and 1 output

//second argument is the type of data (String, Float, Int, Boolean etc...)
inPin1 = addInputPin(Pin.DataType.Float, this);
inPin2 = addInputPin(Pin.DataType.Float, this);

outPin = addOutputPin(Pin.DataType.Float, this);

usually programming languages use variables so we are going to setup a variable for the output pin and put it inside the init() method

String var = "AddFloat" + getGraph().getNextLocalVariableID(); // getNextLocalVariableID is called to avoid duplicating variable names
outPin.setVariable(var);

inside the execute() method (this is called each frame) we are going to calculate our output value

//we get the data type and store it which was set automatically in the init method based on the DataType
NodeData<ImFloat> data1 = inPin1.getData();
NodeData<ImFloat> data2 = inPin2.getData();

NodeData<ImFloat> outData = outPin.getData();
//since the get value returns a float we don't have to do anything fancy to get java to add these up
outData.getValue().set(data1.getValue().get() + data2.getValue().get());

and finally the part of the code that converts everything to source, the printSource(StringBuilder sb) method

String toPrint = "";

//get the data from both input pins and store them in a variable 
NodeData<ImFloat> data1 = inPin1.getData();
NodeData<ImFloat> data2 = inPin2.getData();

//conver the data to a string value
String input1 = String.valueOf(data1.value.get());
String input2 = String.valueOf(data2.value.get());

//check if the pin is connected to another pin 
if(inPin1.connectedTo != -1){
	Pin pin = getGraph().findPinById(inPin1.connectedTo);
        //if the pin is connected then find and ask that pin for it's variable name (remember that variable name we set for our output pin, this is what it's used for)
	input1 = pin.getNode().printSource(sb);
}

//do the same checked as above but with all other input pins
if(inPin2.connectedTo != -1){
	Pin pin = getGraph().findPinById(inPin2.connectedTo);
	input2 = pin.getNode().printSource(sb);
}

//setup String in the format expected from the languages you want to support
toPrint = "float " + outPin.getVariable() + " = " + input1 + " + " + input2 + ";";
sb.append(toPrint + "\n");

//if your node is the kind that will return a variable then get the variable name from your output pins and return it
return outPin.getVariable();

Complete

you can now compile and test your newly create node, remember if you specified any languages then you need to open a graph that is using one of those languages if you want to see your node appear

Creating Plugins

Setup Plugin

Custom Node

Clone this wiki locally