ofxLua


#1

Announcing ofxLua, an Open Frameworks addon for running a Lua embedded scripting interpreter within an OpenFrameworks application. Using the luabind library, C++ functions and classes can be bound to the lua api allowing them to be called within a lua script. This is useful in separating the upper level logic from the lower level application and is utilized in numerous video games and applications.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Luabind is a library that helps you create bindings between C++ and Lua. It has the ability to expose functions and classes, written in C++, to Lua. It will also supply the functionality to define classes in lua and let them derive from other lua classes or C++ classes. Lua classes can override virtual functions from their C++ baseclasses.

https://github.com/danomatika/ofxLua

The current example is written for OF 007. I will add an iOS example soon.

Included is an example OF api binding (maybe 45%) and 4 OF examples implemented as lua scripts:


#2

Since OF 007 will be out soon, I decided not to change the example for 0062 compatibility. It would be pretty messy and the example scripts would have to be replicated etc. ofxLua itself will build and work fine, only the example projects, OF api wrapper, and scripts will not work.


#3

Hello Dan, this oF Lua binding is amazing,
thank you for sharing code! :slight_smile:

Currently I am developing kind of livecoding environment using ofxLua.

I am trying to overlay transparent png with ofEnableAlphaBlending().
But seems not working.
I attach screen shot.
The eye is transparent png.
It “is” transparent.
but it is only transparent to back ground color.
it is not transparent to the image which i place on the screen,
So I cannot see the mountain image through eye image.
I confirmed that this eye image is truly transparent in the usual oF desktop App.

Do you know why the image is not fully transparent?

Thanks!


#4

have you tried drawing the bg before the eye?


#5

Thank you for reply bilderbuchi,

Yea I have tried but mountain img covers eye img, so I cannot see eye…


#6

transparent png problem still not solved though,

I putted ofxGLEditer here
https://github.com/Akira-Hayasaka/ofxGLEditor

I am using ofxGLEditer for Live Coding with ofxLua.
And this should be able to be used with other script language addons.

Cheers


#7

Seems the png transparency problem was caused by img’s z value.
glDisable(GL_DEPTH_TEST) or ofTranslate(0,0,1) makes it transparent.

Thanks

![](http://forum.openframeworks.cc/uploads/default/1717/Screen shot 2011-07-18 at 9.37.43.png)


#8

Great to hear it’s working. I’m currently looking into a script to build boost for iOS so ofxLua is usable on my iPad …


#9

Ok, I have ofxLua working in iOS … I imagine you could port your live coding environment and use a usb keyboard on an iPad!

I will commit the xcode project soon.


#10

I updated all the example project files for xcode 4 and OF 007. Also, the static lib project has been split into mac os and ios versions. This should make things easier.


#11

More updates.

Now you can read and write variables in the lua state and write out values into a lua text file. The main idea is that you can use lua scripts for loading/saving data akin to ofxXmlSettings and uses a similar pushTable/popTable mechanism like xml pushTag/popTag.

Here’s a little bit of the read api:

  
  
// print the variables in the script manually  
ofLog() << "variableTest variables:";  
ofLog() << "	abool: " << lua.getBool("abool");  
ofLog() << "	afloat: " << lua.getFloat("afloat");  
ofLog() << "	astring: " << lua.getString("astring");  
  
// read manually by index, remember lua indices start at 1 not 0!  
lua.pushTable("mixedTable");  
ofLog() << "mixedTable";  
for(int i = 1; i <= lua.tableSize(); ++i) {  
	if(lua.isBool(i)) {  
		ofLogNotice() << i << " b: " << lua.getBool(i);  
	}  
	else if(lua.isFloat(i)) {  
		ofLogNotice() << i << " f: " << lua.getFloat(i);  
	}  
	else if(lua.isString(i)) {  
		ofLogNotice() << i << " s: " << lua.getString(i);  
	}  
}  
lua.popTable();  
  

Check out the api-test-in-the-example for more.


#12

So, I want to call some functions in my testApp and I also need to pass my current testApp to lua, how would I do this??? thanks


#13

There are probably a number of ways to do this, but the simplest would be to write a Luabind wrapper for your testApp class and add a global Lua function which returns the instance of this class.


#14

Yes, this is how I ended up solving it, also, I needed to create a cpp file, in it I called ofGetAppPtr and cast it to my testApp class, then I could call methods in it.


#15

ofxLua is now updated for OF 0.8.0. The previous stable version can be found in the 0.7.4 tag.

lua and luabind have now been moved into the libs folder so you may need to update your projects.


#16

Hi,

first of all thanks for your great work. I came to ofx only recently and I was looking for a lua binding not being c++ one of my favorite language.

I have a question: how does your binding work when I use other addons? Is the bonding to lua automatic or do I have to go in and expose every single class of the addon? Thinking about box 2d right now.

Thanks in advance.

Gianmichele


#17

Nothing is automatic. ofxLua is using Luabind which requires you to specify the bindings in a header file. The ofxLua example comes with a sample binding for some of the OF functions & classes: LuaWrapper.h

ofxLua is not meant to be a Lua api wrapper to OF, but provide an easier interface to roll your own scripting if/when needed.

Check out Gamuza which uses ofxLua as well as some bindings-I’ve-made.


#18

Check this out:

https://github.com/d3cod3/GAmuza/blob/master/src/GAmuza/gamuzaWrapper.h

this is the last GAmuza release (0432) wrapper

includes the almost entire OF 0.7.4 framework + a bunch of other addons + the last GAmuza framework

and this is a video of the software running, enjoy

http://vimeo.com/d3cod3/gamuza


#19

Howdy everyone,

Just announcing I’ve added bindings for the OF API to ofxLua. They are currently in the bindings branch: https://github.com/danomatika/ofxLua/tree/ofbindings


#20

Howdy all,

The bindings branch was merged with master a while ago, so the bindings are live.

Also, I’m looking for someone interested in helping maintain this addon. Any takers?