We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › A text console for Processing
Page Index Toggle Pages: 1
A text console for Processing (Read 2906 times)
A text console for Processing
Jul 3rd, 2008, 10:10am
 
Goodies first, talk later:

...

Download: http://www.ghost-hack.com/p5/postprocessing/console_070308.zip

Includes source code and example project.




Hey guys. This is part of the "post-processing" series of libraries I'm working on to release.

All you do is hit tilde to bring up the console (like Quake 3 and other video-games that have consoles). From here you can access pretty much any variable of function you have written inside your Processing code.

Say you have a color variable, and you just want to tweak it to see what it looks like, but changing it requires breaking too many things, or compile or loading time would take too long to restart, etc. So all you do is bring up the console, and change it directly. Simple!

How this is done: I am using java reflection and abusing it. The console latches onto your object and it knows what static functions you have (private or public! doesn't matter) and what fields (variables) you've defined. I then wrote a text console that interprets what you write and directly calls a variable, or modifies a function.



There are a few restrictions and I'm working to fix them:

1. You can only access static variables. This is because the console doesn't latch onto objects, only classes. This is something I'm hoping to change but presents somewhat of a design problem (how do you select object instances from a console).

2. Integers fail to be detected, so the console treats everything as floats. This needs to be fixed in order so that modifying color variables can work (they are integers in processing).

3. No expressions / in-line math stuff! You can't do MyFunction( anotherFunction() )!

4. Calling functions doesn't return what the function returns. The console should print out what your function spits back.

5. The console rendering fails when you have no background() in your draw loop. That's unfortunate, because the best use of this console is to tweak tiny, iterative drawing sketches.



Help needed

If anyone wants to help out, I've included the source code with the library. I'd love to see if anyone could help resolve some of the issues I listed above.

In the future, I want to be able to allow this console to run scripts. So for example, you can write a script that executes your processing program one way, and another script that runs it completely differently. You can save these scripts from the console, or load and run any scripts you've written and saved to your sketch folder.

The power is yours!! \o/
Re: A text console for Processing
Reply #1 - Jul 3rd, 2008, 12:01pm
 
I have been thinking about attaching jython to running Processing sketches and tweaking them on the fly that way.  

http://www.jython.org/
"Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython."

This might be a nifty alternative way to achieve what you're doing here--read lines from the onscreen console and feed them into jython instead of a custom command parser.  I've only briefly messed around in Jython, but Im fairly certain it would solve issues 2-4.  

It could be used to solve 1 also.  If there was a static function to get the running PApplet instance, you could then access all of the top-level/instance variables in the sketch.  

spencer
Re: A text console for Processing
Reply #2 - Jul 3rd, 2008, 12:19pm
 
Yes but what fun is that Smiley

Besides, if I were a beginner and I want to try this out, I wouldn't want to install jython and set it up. I'd just hit import->Console and start right away.

However, thanks for suggesting jython. This looks really interesting! Would be even nicer is if someone can package jython as part of the console and do the alternative solution you suggested.
Re: A text console for Processing
Reply #3 - Jul 3rd, 2008, 4:22pm
 
FWIW, and this is not intended to minimize the cleverness of your solution (which is considerable!), but have you checked out beanshell.org?  It gives you full-featured java (+) syntax and is trivially easy to integrate.
Re: A text console for Processing
Reply #4 - Jul 4th, 2008, 8:28am
 
Smiley i was working on something similar a while ago: OSM (on screen menu) using controlP5. it would just add lot's of sliders / buttons / ... for each variable and function to a hideable menu. never thought of a terminal-style thing, nice!

F
Re: A text console for Processing
Reply #5 - Jul 6th, 2008, 12:57pm
 
Hi! Thanks for the tipoff to beanshell! I'm looking at it as a possible vector of integration. That could be a lot of fun.

Hi fjen
I was working on a same thing at Yahoo!... also with controlP5. However it was with custom variable objects, rather than using reflection to grab a hold of all variables. This could be done still although you get far less control for stuff like slider range, whether or not a variable uses a dial or a slider, textbox, stuff like that.
Re: A text console for Processing
Reply #6 - Jul 17th, 2008, 8:11pm
 
@mflux: Very cool stuff!  It's actually quite bizarre, today I was literally just about to start coding up something like this, but I happened to check out this post and found that you already did it!

I'll join the crowd and mention that I previously did something similar (in spirit, at least) with the JBox2d 2.0 testbed (http://www.jbox2d.org/v2demos/) - if you press 'o' to pop up the options panel, all of those options are dynamically pulled from the settings object via reflection and laid out with sliders/checkboxes based on type and variable name.  At some point I'm meaning to get something like that working for all types of variables (getting string input working has delayed me, alas...), and allowing deep traversal into nested objects, possibly using annotations in code to mark min/max ranges, block variables from showing up in the list, etc.

I think a lot of the restrictions you mention can be overcome, though it might be a bit of work - definitely pushing reflection to the edge of its ability, but could be very cool!
Re: A text console for Processing
Reply #7 - Jul 18th, 2008, 6:41am
 
hi,
interesting discussion, great examples @mflux and @ewjordan. since controlP5 was mentioned here, i was wondering if there is an interest to have a console implemented in controlP5, not as a replacement for mflux's lib but as an add-on to controlP5. i also like the annotation technique for min/max. regarding reflection and accessing variables/methods from any object, i am working on improvements, too.
controlP5 is also getting more open now, you can extend the controller class and write your own controllers, fjen just posted an example in the forum a few days ago. besides custom pixel fonts, pfont will find its way into the lib (although this is a bit more work). anyway, if there is anything that i can add to controlP5, let me know. always thankful for suggestions and improvements.
best,
andi

Re: A text console for Processing
Reply #8 - Jul 19th, 2008, 12:44pm
 
On topic: Yeah, I think a console widget in controlP5 would be great if you're looking for a feature to add.  I'd suggest designing it so that you can pass a function of a String (either via reflection or an interface, maybe ideally both?) that returns a String to process the console commands and spit back any results, that way there would be a lot of flexibility.  If no handler is passed, using the console as a raw log that's visible on screen would be great, especially with a dump-to-file function.

Off topic, re: using reflection to auto-create a control panel to alter active objects.  There's a problem with using annotations to set ranges/types/hiding: while this works great in Eclipse, the @ character bugs out P5's pre-processor, so it wouldn't be useful as a Processing library, at least until (unless?) someone revamps the preprocessor, and even then I'm not positive they'd put all the annotation syntax in.

One possible solution is to look for variables that are labeled max* and min* to set ranges, such as myFloat, maxMyFloat, minMyFloat; generally this should be fine, as these vars usually either already represent the proper range or are not likely to be used.  Lacking these markers, I like to use defaults of 0 to about 5 or 10 times the original value of the variable.

I'm putting together a sample implementation for some of this stuff (using controlP5), but it's getting tricky as I try to reasonably handle arrays of primitives and objects somewhat consistently...reflection is great and all, but man, how I miss pointers sometimes!  I'll let you know if I come up with anything - if I crack this nut, it might help with mflux's library as well, as I'll have to get all the reflection details sorted out.
Page Index Toggle Pages: 1