Python API?

Mike's Avatar

Mike

30 Dec, 2012 10:25 PM

Hello!

I just discovered this wonderful program (N3). Thanks and congratulations!

I'm familiar with Python programming, so I'm wondering what can be done with Python today. I see there's a python console available, but I'm not sure how I could point to an arbitrary script -- "import sys" and "import os" both fail. I also don't know what facilities are available from Python right now. I gather from other discussions that networks cannot be built programmatically, but that Python code can be used to create custom nodes. I don't know what the abilities of such a custom node could be, i.e., what the Python API is. Could it invoke other nodes? Could it invoke drawing primitives? How does the API relate to what could be done in N1 (which, alas, I cannot run because I'm on Windows)? Are there any examples?

I'd be interested in hearing what your plans are for the Python API. Also, if there's a way to "vote up" the priority for working on the Python API or other related functionality, consider this message such a vote :-)

Thanks again for your hard work!
Mike

  1. Support Staff 1 Posted by lievenmenschaer... on 08 Jan, 2013 10:18 AM

    lievenmenschaert's Avatar

    hi mike,

    There is still some information missing on the website when it comes to working with your own code. For the moment NB3 can import python and clojure scripts. An integrated code editing tool (as in NB2) is at this time not implemented.

    The procedure as it is now:

    • Create a folder to store your python file.

    • In NodeBox, create a new document and store it inside this folder. This step is important because NodeBox won't allow you to import
      outside code when the document has not yet been stored. Also, because
      code dependencies are stored inside the .ndbx file in a relative way
      it's wise to have the ndbx file and python code files close to each
      other (e.g. in the same folder).

    • Write some code and put it inside a python file (mycode.py). NodeBox relates every node to a specific function and it calls its parameters
      one on one. The names of the parameters are not important but their
      order is. For example adding 3 numbers:
      def add(a, b, c): return a + b + c

    • Import the code inside NodeBox:

      • Go to File > Code Libraries (a new dialog will show up).
      • Press the + button and choose Python.
      • In the file dialog select mycode.py.
      • Close the dialog.
    • You can now create a node based on this code:

      • Inside NodeBox, open the node selection dialog and add a new "root" node (the first one to show up in the dialog).
      • When added, this node is named _root1, but feel free to rename it.
      • With this node selected, press the Metadata button on top of the ports panel. A new editor window will show up.
      • In the left panel, click on "settings". This will bring up the node metadata.
      • In the field "Function" write down "mycode/add".
        • "mycode" iss the name of the python file we created (here, we'll also call it the function's namespace).
        • "add" is the name of the function we wrote.
      • We have a new node but we still have to map the function's parameters to node ports (parameters):
        • Press the + button in the bottom left part of the metadata panel.
        • An "Add new port" dialog shows up. Give your port a name "a" and a type "float".
        • Optionally, more parameter tweaking can be done at this point.
        • Repeat this process for the "b" and "c" parameters.
        • Press "Ok" to apply your changes.
      • That's it, the node is ready to use!

    Hope this helps. We'll put this information en some examples on the website in the near
    future as well.

  2. lievenmenschaert closed this discussion on 08 Jan, 2013 10:20 AM.

  3. Stefan Gabriels re-opened this discussion on 31 Jan, 2013 08:24 PM

  4. Support Staff 2 Posted by Stefan Gabriels on 31 Jan, 2013 08:24 PM

    Stefan Gabriels's Avatar

    Our documention now includes a page on how to create nodes that link to custom python code. It lives here:
    http://nodebox.net/node/documentation/advanced/programming-nodes.html

  5. Stefan Gabriels closed this discussion on 31 Jan, 2013 08:24 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

04 Nov, 2024 10:52 AM
02 Nov, 2024 11:22 AM
01 Nov, 2024 12:41 AM
01 Oct, 2024 07:59 AM
30 Sep, 2024 11:37 PM

 

30 Sep, 2024 11:11 AM
30 Sep, 2024 02:37 AM
28 Sep, 2024 10:33 AM
26 Sep, 2024 06:41 AM
24 Sep, 2024 12:32 AM
24 Sep, 2024 12:27 AM