![]() ![]() Notice that in the network view an error message is shown now: eval: evaluate() takes exactly 4 arguments (0 given). You probably remember from before that we’re linking the node to a function called evaluate that resides in a namespace called mycode. In the Settings section, change the text in the Function field to mycode/evaluate.With the node selected, press the Metadata button.You can give the node a new name already this will be our eval node.You can now create a node based on this code: Go to File > Code Libraries (a new dialog will show up).You can now import the mycode.py file in NodeBox: Save the file as mycode.py in the folder where you stored pylib.ndbx.Please try to stick to this when you write some code yourself. In the functions body some work goes on and the function returns one value. So we always use a named function (in the above case it’s called evaluate) that takes a certain amount of arguments (here there’s four: expr, x, y and z). Every node computes a value from a set of inputs you give it and the same set of inputs will always yield the same value. It’s important to keep in mind that NodeBox uses a functional coding model. This snippet evaluates a mathematical expression with at most 3 arguments and returns the result. Now open up a text editor and write some Python:.If you don’t save the file, NodeBox won’t allow you to import any code dependencies. Create a new document in NodeBox and save it as pylib.ndbx in the new folder.It’s important to do this first because NodeBox files store dependencies (meaning references to external pieces of code) relative to your NodeBox document file. First off, create a new folder on your hard disk.We’re now going to look how you can link to an external piece of Python code. If you want to, you can rename your ‘node1’ node to something more comprehensible, like ‘add’ or ‘multiply’ (by right clicking and choosing rename) Adding node with custom Python code. What comes out of the output port of a node is the result (or return value) of that function.The input ports of a node relate to the arguments of that function (therefore: 2 arguments = 2 ports).If you return to the metadata panel and change the value of Function to math/multiply, you will see that the two values are being multiplied instead. You can now drag the value1 and value2 values around and see the results change in the viewer: the two values are added together. Change the value of Description to Add two numbers.Īn overview of node and port settings in the Metadata panel.Change the value of Function to math/add.For example the namespace ‘math’ contains functions like add, multiply, sin, cos, etc… A namespace can be seen as some kind of container of functions that are related to each other. This means that the node refers to a function named zero and this function resides in a namespace called core. The Function field contains by default the value ‘core/zero’. ![]() With node1 still selected press the Metadata button again.We have to tell our node what to do exactly with those values: You can now drag the value1 and value2 numbers but the viewer still shows 0.0. With the metadata panel open it’s not yet possible to change their values. Note that each time you add a new port, the parameter panel adds a new parameter field widget below the last one. Repeat the previous step to add a port named value2.This creates a new port and brings up an overview of its metadata. In the dialog that appears, enter value1 in the name field and choose float as the type.Press the + button below the left sidebar.Since we want to add 2 numbers we need 2 ports: What they mean becomes clearer when we have some ports to work with. The items on the left relate to node and port settings. The metadata panel always relates to one specific node, in this case node1. With node1 selected, press the Metadata button on top of the parameter panel. Locating the ‘node’ node inside the node selection dialog. This is the most basic of the available nodes and it’s also the one from which all other nodes are derived. ![]() We won’t write our own code yet but we’ll reference some code that already exists inside NodeBox. Creating a new node from existing code.įirst off, let’s build a node that does something really simple like adding two numbers together. Or maybe you just want to experiment with writing a new node from scratch. Maybe NodeBox is missing some key piece of functionality that you need. Maybe you want to reuse a cool piece of code in NodeBox that you’ve written. However, sometimes there are cases where you wish you could write your own nodes. Once you get the hang of it, it becomes fairly easy to build networks and hide lengthy procedures behind a subnetwork facade. ![]() Other code that uses generic Python libraries should be okay. We’re currently rewriting the graphics API to be more performant. Writing your own Nodes This is important: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |