5 #include "pinocchio/parsers/python.hpp" 9 #include <boost/shared_ptr.hpp> 10 #include <boost/version.hpp> 11 #include <boost/algorithm/string/predicate.hpp> 14 #if BOOST_VERSION / 100 % 1000 == 58 28 bp::object main_module = bp::import(
"__main__");
30 bp::dict globals = bp::extract<bp::dict>(main_module.attr(
"__dict__"));
33 bp::object cpp_module( (bp::handle<>(bp::borrowed(PyImport_AddModule(
"libpinocchio_pywrap")))) );
40 #if BOOST_VERSION / 100 % 1000 == 58 43 std::ifstream
t(filename.c_str());
44 std::stringstream buffer;
46 bp::exec(buffer.str().c_str(), globals);
47 #else // default implementation 48 bp::exec_file((bp::str)filename, globals);
51 catch (bp::error_already_set & e)
59 bp::object obj_model = globals[model_name];
60 model = bp::extract<Model>(obj_model);
62 catch (bp::error_already_set & e)
69 #if PY_MAJOR_VERSION < 3 73 PyObject * poMainModule = PyImport_AddModule(
"__main__");
74 PyObject * poAttrList = PyObject_Dir(poMainModule);
75 PyObject * poAttrIter = PyObject_GetIter(poAttrList);
76 PyObject * poAttrName;
78 while ((poAttrName = PyIter_Next(poAttrIter)) != NULL)
80 std::string oAttrName = PyUnicode_AS_DATA(poAttrName);
83 if (!boost::starts_with(oAttrName,
"__") || !boost::ends_with(oAttrName,
"__"))
85 PyObject * poAttr = PyObject_GetAttr(poMainModule, poAttrName);
88 if (poAttr && poAttr->ob_type != poMainModule->ob_type)
89 PyObject_SetAttr(poMainModule, poAttrName, NULL);
92 Py_DecRef(poAttrName);
94 Py_DecRef(poAttrIter);
95 Py_DecRef(poAttrList);
Model buildModel(const std::string &filename, const std::string &model_name)
Load a model from a Python script.
Main pinocchio namespace.
JointCollectionTpl & model