1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

startup?

Posted by andrea on Apr 12, 2008

Hi,

I'm using NB to create slides. It's a bit eccentric but works much better than powerpoint/impress (which I hate) and than TeX approaches I used (too boring).
I created a sort of css file, which I import with exec.

I needed arrows, so I defined this which seems to work fine:

def arLine(x1, y1, x2, y2, double = False):
    [x1, x2, y1, y2] = [float(x1), float(x2), float(y1), float(y2)]
    l = sqrt((x2-x1)**2 + (y2-y1)**2)
    line(x1, y1, x2, y2)
    if y2 > y1:
        q = asin((x2-x1)/l)+pi*3*0.5
    elif y2 < y1:
        q = -asin((x2-x1)/l)+pi*0.5      
    elif y2 == y1:
        q = 0
    push()
    translate(x1, y1)
    transform(CORNER)
    rotate(radians=q)
    beginpath(l, 0)
    lineto(l-10, -5)
    lineto(l-10, 5)
    endpath()
    if double:
        beginpath(0, 0)
        lineto(0+10, -5)
        lineto(0+10, 5)
        endpath()    
    pop()
As with other definitions (such as for circle and square) I need arrow line many times, so I have to write the def in each file.

Is there a way/plan to have NB read a startup file with my defs?
What's the way to extend it by adding my own func/classes?

Many thanks

-a-


 
Posted by andrea on Apr 12, 2008

Autoreply. Hmm, Ok, I can put a module in ApplicationSupport. So, just need a startup file.
Best



Posted by andrea on Apr 13, 2008

Ok, my idea was to put a module (e.g. "misc") inside Application Support/NodeBox. In the misc module I have the definitions of some functions using NodeBox' commands, e.g line.
However, if I do in a new script:

from misc import *
an error is raised:
Traceback (most recent call last):
File "nodebox/gui/mac/__init__.pyo", line 358, in _execScript
File "", line 5, in
File "/Users/andreavalle/Library/Application Support/NodeBox/misc.py", line 21, in arLine
NameError: global name 'line' is not defined

so, the misc module does not know NodeBox
I guess I should add something like
from  nodebox import *
inside misc.
?

Many thanks

-a-



Posted by fdb on Apr 18, 2008

Hi,

I think the ximport command is what you need. The imported module gets access to a "_ctx" variable, that has all the NodeBox commands, such as _ctx.line(10, 20, 30, 40) .

For more info, check out the docs:

http://nodebox.net/code/index.php/Libraries

Kind regards,

Frederik



Posted by andrea on Apr 19, 2008

Hi Frederik,

thanks. I've been able to create a package by putting a folder "andrea" with an __init__.py file into app support containing e.g.:

def myline(a,b,c,d):
    return _ctx.line(a,b,c,d)
so in my script I can have
a = ximport("andrea")
 
strokewidth(10)
stroke(color(0,0,0))
a.myline(40, 50, 60, 154)
Still, I don't know what to write in other files of the package to make them use nodebox .
E.g. I have another file in the package "andrea"
with
def circle(x, y, radius, other = None):
    if other == None:
        other = radius
    return oval(x-radius*0.5, y-other*0.5, radius, other)
If my script now I write
a = ximport("andrea")
 
strokewidth(10)
stroke(color(0,0,0))
a.myline(40, 50, 60, 154)
a.circle(10,10, 40) I 'm obtaining:
Traceback (most recent call last):
File "nodebox/gui/mac/__init__.pyo", line 358, in _execScript
File "/uni/pollenzo/slides/testscript.py", line 6, in
AttributeError: 'module' object has no attribute 'circle'

Hmm

Thanks a lot

-a-