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.
IndexProgramming Questions & HelpPrograms › scripting capabilities
Page Index Toggle Pages: 1
scripting capabilities? (Read 749 times)
scripting capabilities?
May 6th, 2008, 8:54pm
 
How would I go about integrateing scripting capabilities into my sketch?  Keep in mind, I am not a computer scientist and have no interest in coding an interpreter.  I was just wondering if perhaps someone wrote an object that allows parts of the sketch to be driven by processing code entered into a window.  If this doesn't exist, I think it would be a great idea for someone more experienced than myself, as it would open up many new layers of interactivity.  

Thanks,
Zach
Re: scripting capabilities?
Reply #1 - May 7th, 2008, 12:14am
 
There's been some previous work with integrating scripting support of the processing API.
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1187581621

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1187597413

I took the approach of integrating the JSR 223(https://scripting.dev.java.net/) scripting engines into processing. Which will allow you to write processing applications in a whole variety of languages (javascript, perl, python, scheme, ruby, etc).


I did a quick write-up here:
http://eventhorizongames.com/wiki/doku.php?id=articles:processing:scripting:start

You can try an example of a script enabled Processing applet that lets yo u modify and reinterpret the code from a webpage (this could easily be extended to a full featured online processing sketchbook IDE).
http://jddaniels.googlecode.com/svn/trunk/processing-scripting/dist/index.html


Re: scripting capabilities?
Reply #2 - May 7th, 2008, 10:28pm
 
Wow, thanks for all the info.  I'm actually looking for something much simpler- just manipulating one variable in the sketch by entering code in a text box contained in the sketch itself (not html).  Since it doesn't seem like I can use processing for the scripting language (yet), I may use javascript, python, or even scheme.  

Unfortunately, I'm having a little trouble understanding your applet, as I have little experience with Java.  Essentially I think what I need would just require a few alterations to the Java code, once it has been translated from processing.  I would import the scripting applet, have the sketch run the script through it, and set the variable I want as the one that is returned.  Hmm...I guess I had better start familiarizing myself with what my sketches look like in Java.
Re: scripting capabilities?
Reply #3 - May 7th, 2008, 11:38pm
 
Ohh you just want a simple way to interact with a parameter in your sketch.

Check out some of the Interaction examples that show how to modify your sketch dynamically based on user input from the mouse.

Also there's an easy to use GUI toolkit called Interfascia (there are probably many more) for processing that lets you use textfield's that you can have interact with your sketch.
http://superstable.net/interfascia/download.htm

Here's an example that lets you type in the radius of a circle that will be drawn:

http://ddaniels.net/ddanielsblog/processing/GUISketch/


Code:

import interfascia.*;

GUIController c;
IFTextField radiusTxt;
IFLabel label1;
float radius = 30;

DecimalFormat format;

void setup() {
size(400, 400);
background(200);

c = new GUIController(this);

label1 = new IFLabel("radius = ", 25, 1);
radiusTxt = new IFTextField("radius", 75, 1, 50, radius+"");

radiusTxt.addActionListener(this);

c.add(label1);
c.add(radiusTxt);

format = new DecimalFormat();
format.setMaximumFractionDigits(2);
}

void draw() {
background(200);
if(radius>0) {
ellipse(width/2, height/2, radius, radius);
}
}

void actionPerformed(GUIEvent e) {
float temp;
if (e.getMessage().equals("Modified")) {
if (e.getSource() == radiusTxt) {
try {
radius = Float.parseFloat(radiusTxt.getValue());
} catch (Exception e2) { }
}
}
}

Re: scripting capabilities?
Reply #4 - May 8th, 2008, 4:39pm
 
I'm actually trying for something in between your two examples.  I want to be able to write actual code in the textfield, but only to control one variable.  It is an array that will drive frames of a video- although I'm still waiting for a response on how to control video by frame in processing(http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Video;action=display;num=1210092064).  For example, I may want to type in a script that fills the array based on a cellular automata simulation.
Re: scripting capabilities?
Reply #5 - May 8th, 2008, 6:18pm
 
So you want some way to generate an array of PImage[] using a script and then pushing that into your processing sketch to be displayed and scrolled through in the video editor.

You could pretty easily delegate a part of your sketch to a script. For example you could define a generateData() function that would generate an array of PImage[], and that function would actually use the text in a textfield to interpret a script which would return that PImage[].



Code:


PImage[] generateData() {
return (Pimage[])invoke("generateData");
}

Object invoke(String methodName){
//This is where you "compile" or evaluate your script from the text box
//e.g.
String scriptContents = textField.getValue();
ScriptEngine jsengine;
ScriptEngineManager manager = new ScriptEngineManager();
jsengine = manager.getEngineByName("JavaScript");
//Parse the script so you can access it's functions
jsengine.eval(scriptContents);

Invocable invocable = (Invocable)jsengine;
//Call the method passing in the PApplet as an argument
//in a Object[]
return invocable.invokeMethod(methodName, new Object[]{this});

}


Then in Javascript you can define it as something like:
Code:

importPackage(Packages.processing.core);

function generateData(p) {
var numFrames = 1000;
var images;
//Run your cellular automata logic and return the PImage array

//...

return images;

}


When I get a chance I might try and prototype something like this.
Page Index Toggle Pages: 1