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

How to use var()

Posted by Pieter Nuyts on Jun 08, 2009

Hi,

I am trying to use var() to get some string input. I have some commands

var("Name", TEXT)
var("Context", TEXT)

and I use the variable Name in my code to create a new node when a button is pressed, by calling

g.add_node(Name)

But this does not work: if I press enter after entering a name, the whole program is executed again (deleting any changes I made previously) and the name is still "hello" (the default value). If I don't press enter, the name of the new node is also "hello".

I don't really understand very well how this var() thing works, since normally variables are not updated on the fly, but only when you actively collect the entered value and store it into a variable. But the var thing is supposed to be updated live, right?

Is this a bug or am I doing something wrong?

Thanks for your help,
Pieter


 
Posted by Stefan on Jun 09, 2009

My guess is you're trying to do an animation? In that case this is definitely a bug, since the variables panel doesn't seem to work very well (if at all) with animated scripts. In case of a static script, however, it may be helpful to see more source code here.



Posted by Pieter Nuyts on Jun 10, 2009

test (I didn't see my previous post appearing)



Posted by Pieter Nuyts on Jun 10, 2009

Sorry about that test thing; apparently something went wrong.

Here's my code (I tried to remove as much irrelevant stuff as possible while still showing the same effect):

#==========================================================================================
graph = ximport("graph")
 
print "Starting..."
 
g = graph.create()
#==========================================================================================
# CONSTANTS
global DEFAULT_STYLE; DEFAULT_STYLE = "default"
#==========================================================================================
# VAR INTERFACE
var("Name", TEXT)
var("Add_node", BUTTON)
var("Screen_size", NUMBER, 500, 0, 1000)
 
def Add_node():
    print Name
    g.add_node(Name)
    update_all()
    mode = "default"
 
#==========================================================================================
# Define initial nodes and edges
g.add_node("jos")
g.add_node("jef")
g.add_edge("jef", "jos")
g.add_edge("jos", "louis")
g.add_edge("jef", "louis")
g.add_edge("jef", "francois")
g.add_edge("francois", "emile")
g.add_edge("francois", "jean-pierre")
g.add_node("franske")
#==========================================================================================
# METHODS
 
# Draw the graph in its current state
def draw(highlight=None):    
    g.update()
    
    g.draw(highlight=highlight, weighted=True)
    
           
# Update the graph.  The path through the nodes given in highlight is highlighted.
def update_all(highlight = None):
    draw(highlight = highlight)
 
#==========================================================================================    
 
size(Screen_size, Screen_size)
 
speed(30)
 
update_all()
#==========================================================================================
The "Add_node" button by itself works fine: if you click it, a node called "hello" is added. But there seems to be no way to make a node with a different name.

Also, when you press enter, or when you change the screen size with the slider, the whole program is re-executed (contrarily to what the documentation says), so the newly created node disappears again.

Indeed I am using animation, but I tried disabling it and this changes nothing. Also I need animation because in the full version you can select nodes by clicking them and this does not seem to work without animation.

My guess is it's either a bug or I am missing out on the way the var pane works.

The thing is I am not familiar to Nodebox or Python, but I am using it to try and create a quick fix for someone who needs a program rather urgently, and Nodebox's graph library seems to be ideal for this. But the problem is I have no experience whatsoever in creating GUIs in Python or on a Mac, and since there is not too much time I was hoping I could do with the var pane for all buttons and string input because learning how to make a real GUI would be time-consuming. But I don't think the var pane is really intended to be used the way I want to use it...

Thanks for your help,
Pieter



Posted by Pieter Nuyts on Jun 10, 2009

PS The global DEFAULT_STYLE and the line mode = "default" are not important, I just forgot to remove them.



Posted by Stefan on Jun 12, 2009

Yes sadly the var panel doesn't work like it should when you're using animation. I don't know if or when it will be fixed.