Available with the following Subscriptions: All Subscriptions
Available for the following Project Roles: All Roles
This article explains how to create a Code Node as well as related best practices and troubleshooting tips. Code Nodes are used in a Smart Template's Visual Program to write and test code that transforms graph inputs into outputs. Ā
š”Tip: Share this page with your preferred AI tool, and ask it to generate the code for you.Ā
Create a Code Node
The Code Node is only available within Visual Programs for Smart Templates. In order to create a Code Node, first choose a Smart Template to modify or create a new one. A Visual Program may contain multiple Code Nodes.
-
Click Smart Templates (1) from the Toolbar. Select an existing Smart Template (2) or click Create Template (3).Ā
Click on Edit template, then Visual Prog. (4) in the Sidesheet Toolbar to open the expanded Visual Program editor.
Right-click the canvas and select Add Node (5) > Tools (6) > Code (7) to add a Code Node.
Ā
Code Node Editor š§Ŗ Experimental
This feature is in Experimental Features. Turn on Experimental Features to try it.
The Code Node has three main parts, the inputs, the outputs and the script also referred to as the code. The script uses the supplied inputs to generate the desired outputs. Its recommended to first define/configure the inputs and outputs by adjusting each ones propertiesĀ before writing the script/code.Ā
- From within the Visual Program, locate the code Node, in the Node click the Edit Code button (1). The Code Node Editor opens in a panel at the bottom of the screen (2). To rename the Node, click the pencil icon next to its name.
Ā - From within the Code Node Editor, set up the inputs and outputs:
- Select Add Input (1) to add an input. Up to 32 inputs are allowed.Ā
- SelectĀ Add Output (2). At least one output is required and up to 32 outputs are allowed.Ā
- For each Input and Output, enter a Variable Name, a Display Name, and select a data Type from the available options which currently include: Number, Boolean, String, Vec2, Vec3, or Sketch.
- While an input or output is being created, its Variable Name stays in sync with its Display Name.
- To edit an input or output after it is created, click the 3-dots button on its card and choose Rename, Edit code name, Change type, Move up, Move down, Duplicate or Delete (5).
- After setting up inputs and outputs move on to the Code section of the editor, to code the desired logic. Input and Output objects are meant to be referenced in the logic by using its exact code name.Ā
- The default scripting language is JavaScript, as indicated by the language tag (6). This guide doesn't provide instruction on using JavaScript. A quick web search will provide plenty of learning resources. Ā
- It is possible that upon code execution of the code, an input contains no (null) value or even multiple values (in the form of an array). Make sure the code logic is able to handle null and array values if needed.
- The code logic should also supply (return) a value for each named output -make sure object's returnedĀ in the code match the listed output's specific type.Ā
- When all inputs and outputs have been defined, and the program has been coded, click Confirm (7) in the lower left corner of the panel to close the Code Node Editor and return to the Visual Program. To close it without saving, click Cancel, then select Discard if there are unsaved changes.
- In the Visual Program editor (the graph/flow chart), connect the inputs (8) and output (9)Ā portsĀ of the code Node to the rest of the Visual Program's Nodes.Ā
- It is always recommended to test the behavior and inspect the result. From within the Visual Program, on the code Node, click Edit Code (10), to open the Code Node Editor, click on Test (11). Check the outputs (#) in the scene (12). Runtime errors and missing or invalid outputs are indicated in the code editor, while test status next to the Test button containing one of the following messages:
- Not tested
- Untested changes
- Test failed
- Test passed.Ā
- After testing, click Confirm (13) to save changes to the code Node. Continue developing the Visual Program and Smart Template. Consider downloading (14) the Smart template as it will not be available to view or alter after clicking confirm.
- Lastly click Confirm (15) to save the Visual Program changes, or Cancel (16) to close the Visual Program editor. Clicking Cancel (17) while Visual ProgramĀ changes are still unsaved will result in a popup. Select Discard (3) to close without saving or Keep Editing (18) to return to the editor to make changes or save before closing.
Edit or Delete a Code Node
You can edit, rename, duplicate, or delete a Code Node using the controls that are available. Click Edit (1) on the Code Node to reopen the Code editor and modify its inputs, outputs, or code.
To rename, duplicate, or delete the Node, right-click the Code Node in the Visual Program graph and select Rename, Duplicate, or Delete. The Node can also be renamed with the pencil icon in the Code Node Editor.
Troubleshoot
- An input has no value: Check its connection and handle undefined inputs.
- An output is missing or invalid: Check the returned object and match each property name and value type to its output port.
- The test status says Untested changes: Run Test again after editing.
- An error appears: Read the error message in the editor. You can copy it if you need to share it.
Comments
Please sign in to leave a comment.