How can I use the old nodebox libraries (like L-System) in Nodebox for OpenGL?
I tried ximport, but it doesn't work in NB OpenGL, right? I also tried to copy the LSystem class into my code and then build an L-System object using the example code provided but it didn't work. ('tree = lsystem.create()' and 'tree =LSystem.create()')
Sorry if the question is too 'newbish', I'm not an experienced programmer and hadn't tried doing anything in Python before.
Thanks!
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
1 Posted by kenzenichinyoki... on 05 Nov, 2013 06:45 PM
Hi!
Here's my last code:
# Import the library
from nodebox.graphics import *
#from lsystem import *
try:
# This is the statement you normally use.
lsystem = ximport("lsystem")
except:
# But since these examples are "inside" the library
# we may need to try something different when
# the library is not located in /Application Support
lsystem = ximport("__init__")
reload(lsystem)
# Create a simple treelike pattern.
tree = lsystem.create()
tree.rules = {
"1" : "[-FF-FF1][+FF+FF1]"
}
# Each F command depletes a little bit of time.
# Calculate the total time we need to grow 6 generations.
done = tree.duration(5)
# Our custom segment is a pinkish oval.
# When we divide the current time at the segment by done,
# we get a number between 0.0 and 1.0 we can use for coloring.
def segment(length, generations, time, id):
t = min(1.0, time/done)
fill(1-t*0.75, 0, 0.5, 0.6)
oval(-length/2, -length, length, length)
tree.segment = segment
# Animate the growth of the tree using the FRAME count.
size(500, 250)
speed(20)
def draw(canvas):
time = min(FRAME*0.1, done)
tree.draw(250, 250, 5, time, ease=8)
canvas.run(draw)
And here are the errors listed in the console:
Traceback (most recent call last):
File "E:\workspace\Test2\src\test2.py", line 42, in <module>
canvas.run(draw)
File "E:\workspace\Test2\src\nodebox\graphics\context.py", line 3918, in run
pyglet.app.run()
File "E:\Python2732\lib\site-packages\pyglet\app\__init__.py", line 264, in run
EventLoop().run()
File "E:\Python2732\lib\site-packages\pyglet\app\win32.py", line 63, in run
self._timer_func(0, 0, timer, 0)
File "E:\Python2732\lib\site-packages\pyglet\app\win32.py", line 84, in _timer_func
sleep_time = self.idle()
File "E:\Python2732\lib\site-packages\pyglet\app\__init__.py", line 187, in idle
dt = clock.tick(True)
File "E:\Python2732\lib\site-packages\pyglet\clock.py", line 700, in tick
return _default.tick(poll)
File "E:\Python2732\lib\site-packages\pyglet\clock.py", line 303, in tick
item.func(ts - item.last_ts, *item.args, **item.kwargs)
File "E:\workspace\Test2\src\nodebox\graphics\context.py", line 3847, in _draw
self.draw()
File "E:\workspace\Test2\src\test2.py", line 40, in draw
tree.draw(250, 250, 5, time, ease=8)
File "E:\workspace\Test2\src\lsystem\__init__.py", line 182, in draw
self._grow(generation, self.root, angle, self.d, time, draw=True)
File "E:\workspace\Test2\src\lsystem\__init__.py", line 131, in _grow
draw
File "E:\workspace\Test2\src\lsystem\__init__.py", line 145, in _grow
self.segment(length, generation, None, id=self._segments)
File "E:\workspace\Test2\src\test2.py", line 28, in segment
t = min(1.0, time/done)
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
What am I doing wrong? It is important for me to create LSystems, and why not, use the other clibraries you built for Nodebox 1 in Nodebox OpenGL.
By the, way, and also very important for me: can you export to PDF using Nodebox for OpenGL? I don't have a Mac so for me this is the option to use 'just code'.
I love your tools.
Support Staff 2 Posted by tomdesmedt on 04 Dec, 2013 12:17 AM
Old libraries from NodeBox 1 that "draw stuff" can't be used in NOGL, since the drawing operations are very specific to NB1. Nevertheless, NOGL ported a lot of code from the libraries (e.g., boids, colors, ...) but unfortunately not L-systems.
You could look at the source code of the L-systems library, and replace the drawing commands (they start with "_ctx") with NOGL commands. Here is a simple L-system in Javascript which should not be hard to convert to Python code:
http://www.clips.ua.ac.be/media/canvas/?example=lsystem
NOGL does not have support for PDF, since it draws pixels for speed and not vectors. So chances are pretty slim it will ever support PDF. There is always a trade-off: either it is fast (=graphics card accelerated, pixels) or either it is slower but scalable (vector curves, PDF).
Best,
Tom