The new NodeBox forum is live! Everything from support.nodebox.net moved here: sign in with the same email to pick up your old posts. Something not working? Write to forum-support@nodebox.net.

NodeBox 1ArchiveFrom the old forum

Calling Nodebox functions from a library Module

1 reply Last reply

  1. 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

    Copy link
  2. NodeBox team

    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.

    Copy link

Know something that helps?

Sign in with your email address to reply. No password needed.

Sign in to reply