Calling Nodebox functions from a library Module

Kevin Karney's Avatar

Kevin Karney

08 Mar, 2015 10:43 AM

I am sure this is an easy question.

I have built many Nodebox 1 utilities that I want to put in a library module. The routines that just use raw Python mathematics work just fine, but any with Nodebox functions (lineto, oval, push, etc etc) fail since the function does not seem to be recognised.

I have tried
from node box.graphics import *
and many other variations - but nothing seems to resolve this for me.

I am a heavy user of Nodebox 1 but am a very lightweight Python user.....

Any help would be appreciated
Kevin

  1. Support Staff 1 Posted by Frederik De Ble... on 09 Mar, 2015 09:27 AM

    Frederik De Bleser's Avatar

    You're right: the NodeBox 3 graphics API is different from the NodeBox 1 API. That's because the new API uses Path objects instead of directly drawing which is what NodeBox 1 did. We do this because we can take these generated Path objects and modify them using different nodes, for example the "wiggle" node.

    So whereas in NodeBox 1 you would write:

    rect(10, 20, 30, 40)
    

    In NodeBox 3, you would construct a Path and add a rectangle to it:

    from nodebox.graphics import Path
    
    def my_rect(x, y, w, h):
        p = Path()
        p.rect(x, y, w, h)
        return p
    

    We call these nodes generators: they only take in primitive values (numbers) and generate shapes. Nodes that take in shapes and modify them are called filters. They might look like this:

    def my_colorize(shape, fill):
        if shape is None: return None
        new_shape = shape.clone()
        new_shape.fill = fill
        return new_shape
    

    Note that these nodes are required to clone the input shape, instead of modifying the input shape directly.

    The documentation for the graphics API is unfortunately still missing. You can look at the source code for the Path class to see how it works internally.

    I hope this helps. Feel free to ask any additional questions.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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

17 Jun, 2025 06:15 AM
17 Jun, 2025 06:11 AM
16 Jun, 2025 02:18 AM
11 Jun, 2025 10:57 AM
11 Jun, 2025 09:53 AM

 

10 Jun, 2025 08:03 AM
10 Jun, 2025 01:33 AM
08 Jun, 2025 09:25 AM
08 Jun, 2025 09:19 AM
08 Jun, 2025 09:19 AM
08 Jun, 2025 09:18 AM