Some house cleaning first...
"Boost provides free peer-reviewed portable C++ source libraries". http://www.boost.org/
The Boost C++/Python binding library is just one member of the boost C++ library collection at http://www.boost.org
It is quick and easy to export C++ to Python using Boost. Boost is also designed to be minimally intrusive on your C++ design. In most cases, you should not have to alter your C++ classes in any way in order to use them with Boost Python. The system should simply reflect your C++ classes and functions into Python. Boost Python bindings are written in pure C++, using no tools other than your editor and your C++ compiler.
http://www.boost.org/doc/libs/1_49_0/libs/python/doc/index.html
Documentation at: http://www.boost.org/libs/python/
Find binaries at: http://boost.teeks99.com
Tutorial: http://www.boost.org/doc/libs/1_49_0/libs/python/doc/tutorial/doc/html/python/exposing.html
OK, so now let's see how it looks like: Like in the previous post something simple.
#include <boost/python.hpp>
namespace python =boost::python ;
void say_hello (char *name) {
printf ("Hello %s!\n", name) ;
}
BOOST_PYTHON_MODULE (PyCyrille) {
python::def ("say_hello", say_hello) ;
}
Yes this is it :) nice isn't it?
And now here is a very simple class exposed.
#include <boost/python.hpp>
namespace python =boost::python ;
class CyrilleClass {
private:
int mInt ;
public:
float mFloat ;
CyrilleClass () : mInt(-1), mFloat(-1) {}
int inc () { return (++mInt) ; }
int dec () { return (--mInt) ; }
int _get () { return (mInt) ; }
void _set (int i) { mInt =i ; }
} ;
BOOST_PYTHON_MODULE (PyCyrille) {
python::class_<CyrilleClass>("CyrilleClass")
.def("inc", &CyrilleClass::inc)
.def("dec", &CyrilleClass::dec)
.def("_get", &CyrilleClass::_get)
.def("_set", &CyrilleClass::_set)
.def_readwrite("mFloat", &CyrilleClass::mFloat)
;
}
You see excepted the BOOST_PYTHON_MODULE block, which remains fairly easy to understand, there is nowhere complicated thing like PYINC(), PyInitialize(), etc... and I do not have to care about the GIL state. Wow, I like that !
But there is a price for this - Boost adds an additional memory footprint to Maya, and you better choose (or recompile) Boost using the same compiler Maya used (I.e. Visual Studio 2010 SP1 for the Windows platform). For MotionBuiler, that is probably the way to go as the MotionBuilder Python API was created using Boost - just make sure to use the same Boost libraries.
Next time, we will see how to deploy C++ (or Boost) Python extension into applications.
Comments
You can follow this conversation by subscribing to the comment feed for this post.