5 #include "pinocchio/parsers/python.hpp" 9 #include <boost/version.hpp> 10 #include <boost/algorithm/string/predicate.hpp> 13 #if BOOST_VERSION / 100 % 1000 == 58 27 bp::object main_module = bp::import(
"__main__");
29 bp::dict globals = bp::extract<bp::dict>(main_module.attr(
"__dict__"));
32 bp::object cpp_module( (bp::handle<>(bp::borrowed(PyImport_AddModule(
"libpinocchio_pywrap")))) );
39 #if BOOST_VERSION / 100 % 1000 == 58 42 std::ifstream
t(filename.c_str());
43 std::stringstream buffer;
45 bp::exec(buffer.str().c_str(), globals);
46 #else // default implementation 47 bp::exec_file((bp::str)filename, globals);
50 catch (bp::error_already_set & e)
58 bp::object obj_model = globals[model_name];
59 model = bp::extract<Model>(obj_model);
61 catch (bp::error_already_set & e)
68 #if PY_MAJOR_VERSION < 3 72 PyObject * poMainModule = PyImport_AddModule(
"__main__");
73 PyObject * poAttrList = PyObject_Dir(poMainModule);
74 PyObject * poAttrIter = PyObject_GetIter(poAttrList);
75 PyObject * poAttrName;
77 while ((poAttrName = PyIter_Next(poAttrIter)) != NULL)
79 std::string oAttrName((bp::extract<char const*>(poAttrName)));
82 if (!boost::starts_with(oAttrName,
"__") || !boost::ends_with(oAttrName,
"__"))
84 PyObject * poAttr = PyObject_GetAttr(poMainModule, poAttrName);
87 if (poAttr && poAttr->ob_type != poMainModule->ob_type)
88 PyObject_SetAttr(poMainModule, poAttrName, NULL);
91 Py_DecRef(poAttrName);
93 Py_DecRef(poAttrIter);
94 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