New addon: ofxPython

Happy new year everyone!

For a project I am involved I needed to embed Python scripts onto an OF application, and surprisingly as it is, there were none! So I have started a new addon, ofxPython, that intends to solve this obvious mistake ;).

It only uses the Python API, the bindings are generated with SWIG, but SWIG is not needed in order to compile. I know about boost::python but (1) generating all the bindings can be a very boring and long process, and (2) compiling boost on some OS is basically a pain in the ass. I don’t discard adding boost::python optional additional support in the future.

You can find it here . Pull requests are welcome, as I will be developping new features and fixing bugs as I need them for my main project.

I hope i’ts useful to you :slight_smile:


this looks great! I love python and super excited to check this out.

On the developer mailing list (where we discuss some of the meta level OF stuff) there’s been recently a big discussion about bindings and I know folks like @danomatika and memo have been thinking about this. I will see if that conversation can be steered onto the forum, but in the meantime, I encourage you to join that list if you are interested in jumping in on that conversation.

Oh I’ll subscribe to the mailing list then!

Howdy, I have a pretty full SWIG binding here:

I managed to get automatic naming working and I have the same functionality with the new SWIG bindings for Lua as I did with my previous Luabind ones. I’m hoping to role this file forward as an general purpose SWIG asset for OF.

Wow thanks! I didn’t realize that there was already a SIWG-based ofxLua. I’ll definitely use some bits from it, as my SWIG knowledge is quite limited. As I am progressing with the Python version, perhaps we can identify the common parts that can be abstracted into an openframeworks general interface file that can be included into individual target language interfaces.

Awesome work!

As it stands now, I can pull the SWIG part out into a separate repo we can then use in our addons as a submodule.

The Makefile is almost generic. I can update it so you could generate the Python bindings using something like: make LANG=python. It then renames and moves the generated files into the src folder into platform specific folders which are desktop and ios right now. There’s no need for desktop OS specific bindings while the iOS ones require some defines to keep ofSerial and ofArduino from being included.

Sure having a separated repo woud be a good idea.

About having OS specific builds, when SWIG and Python (and OF) I had to add some platform-specific defines or code n order to get a valid cpp file (for instance, defining __WIN32__ in order to include only code related to the windows platform, see here), and the resulting cpp is then different. However, may be I am missing some SWIG command line settings that makes this unnecessary.

But we can work out the details as we encounter these potential problems.

As far as I can tell, defines are only needed for platform specific classes and functions that need to be wrapped. I set the defines using the CXXFLAGS env variable in the Makefile. Due to the nature of how OF is coded, the generated cpp desktop wrapper should compile and work on Win, Linux, and OS X with a caveat for embedded Linux since OpenGLES doesn’t support a couple of the enums used in OF. In any case, I don’t think an intermediary *.i file is required.

Ok, I’ve got the repo up:

I’m now using it in ofxLua and I’ll update the readme soon. In the meantime, you could generate desktop python bindings using the Makefile like this:

make desktop LANG=python DEST_DIR=../src/bindings

It generates both the platform-specific .cpp bindings as well as a header if you need for swig binding helper function access and places them in DEST_DIR. I use the swig functions in ofxLua, for instance, to pass the ofTouchEventArgs struct by reference to Lua on iOS.

I added it as a submodule in my main folder using:

git submodule add swig

Ok I have swig-openframeworks documented. It’s now in full use with ofxLua, so would be easy to drop into ofxPython.

Edit: Generating bindings for all platform targets can be done easily using a script which calls the Makefile, like in ofxLua.

Ok, I was working on the Python generation on my fork, and got in working in Linux. I’ll make a pull request after testing it on other platforms.

This looks really great. I’d love to be able to use it in the fall with new freshmen. :slight_smile: I’ll install it and report back using Dan’s git repo.