src/parsers/python/model.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2023 CNRS INRIA
3 //
4 
6 
7 #include <boost/version.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
9 
10 #include <boost/python.hpp>
11 
12 // Boost 1.58
13 #if BOOST_VERSION / 100 % 1000 == 58
14  #include <fstream>
15 #endif
16 
17 namespace pinocchio
18 {
19  namespace python
20  {
21  namespace bp = boost::python;
22 
23  Model buildModel(const std::string & filename, const std::string & model_name)
24  {
25  Py_Initialize();
26 
27  bp::object main_module = bp::import("__main__");
28  // Get a dict for the global namespace to exec further python code with
29  bp::dict globals = bp::extract<bp::dict>(main_module.attr("__dict__"));
30 
31  // That's it, you can exec your python script, starting with a model you
32  // can update as you want.
33  try
34  {
35 // Boost 1.58
36 #if BOOST_VERSION / 100 % 1000 == 58
37  // Avoid a segv with exec_file
38  // See: https://github.com/boostorg/python/pull/15
39  std::ifstream t(filename.c_str());
40  std::stringstream buffer;
41  buffer << t.rdbuf();
42  bp::exec(buffer.str().c_str(), globals);
43 #else // default implementation
44  bp::exec_file((bp::str)filename, globals);
45 #endif
46  }
47  catch (bp::error_already_set & e)
48  {
49  PyErr_PrintEx(0);
50  }
51 
52  Model model;
53  try
54  {
55  bp::object obj_model = globals[model_name];
56  model = bp::extract<Model>(obj_model);
57  }
58  catch (bp::error_already_set & e)
59  {
60  PyErr_PrintEx(0);
61  }
62 
63  // close the interpreter
64  // cf. https://github.com/numpy/numpy/issues/8097
65  PyObject * poMainModule = PyImport_AddModule("__main__");
66  PyObject * poAttrList = PyObject_Dir(poMainModule);
67  PyObject * poAttrIter = PyObject_GetIter(poAttrList);
68  PyObject * poAttrName;
69 
70  while ((poAttrName = PyIter_Next(poAttrIter)) != NULL)
71  {
72  std::string oAttrName((bp::extract<char const *>(poAttrName)));
73 
74  // Make sure we don't delete any private objects.
75  if (!boost::starts_with(oAttrName, "__") || !boost::ends_with(oAttrName, "__"))
76  {
77  PyObject * poAttr = PyObject_GetAttr(poMainModule, poAttrName);
78 
79  // Make sure we don't delete any module objects.
80  if (poAttr && poAttr->ob_type != poMainModule->ob_type)
81  PyObject_SetAttr(poMainModule, poAttrName, NULL);
82  Py_DecRef(poAttr);
83  }
84  Py_DecRef(poAttrName);
85  }
86  Py_DecRef(poAttrIter);
87  Py_DecRef(poAttrList);
88  return model;
89  }
90  } // namespace python
91 } // namespace pinocchio
boost::python
filename
filename
python
python.hpp
pinocchio::python::buildModel
PINOCCHIO_PYTHON_PARSER_DLLAPI Model buildModel(const std::string &filename, const std::string &var_name="model")
Load a model from a Python script.
Definition: src/parsers/python/model.cpp:23
t
Transform3f t
pinocchio::ModelTpl
Definition: context/generic.hpp:20
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1082
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:31