bindings/python/parsers/python/model.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2023 CNRS INRIA
3 //
4 
6 
7 #include <iostream>
8 #include <Python.h>
9 #include <boost/version.hpp>
10 #include <boost/algorithm/string/predicate.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  // We need to link to the pinocchio PyWrap. We delegate the dynamic loading to the python
32  // interpreter.
33  bp::object cpp_module(
34  (bp::handle<>(bp::borrowed(PyImport_AddModule("libpinocchio_pywrap")))));
35 
36  // That's it, you can exec your python script, starting with a model you
37  // can update as you want.
38  try
39  {
40 // Boost 1.58
41 #if BOOST_VERSION / 100 % 1000 == 58
42  // Avoid a segv with exec_file
43  // See: https://github.com/boostorg/python/pull/15
44  std::ifstream t(filename.c_str());
45  std::stringstream buffer;
46  buffer << t.rdbuf();
47  bp::exec(buffer.str().c_str(), globals);
48 #else // default implementation
49  bp::exec_file((bp::str)filename, globals);
50 #endif
51  }
52  catch (bp::error_already_set & e)
53  {
54  PyErr_PrintEx(0);
55  }
56 
57  Model model;
58  try
59  {
60  bp::object obj_model = globals[model_name];
61  model = bp::extract<Model>(obj_model);
62  }
63  catch (bp::error_already_set & e)
64  {
65  PyErr_PrintEx(0);
66  }
67 
68  // close the interpreter
69  // cf. https://github.com/numpy/numpy/issues/8097
70 #if PY_MAJOR_VERSION < 3
71  Py_Finalize();
72 #else
73 
74  PyObject * poMainModule = PyImport_AddModule("__main__");
75  PyObject * poAttrList = PyObject_Dir(poMainModule);
76  PyObject * poAttrIter = PyObject_GetIter(poAttrList);
77  PyObject * poAttrName;
78 
79  while ((poAttrName = PyIter_Next(poAttrIter)) != NULL)
80  {
81  std::string oAttrName((bp::extract<char const *>(poAttrName)));
82 
83  // Make sure we don't delete any private objects.
84  if (!boost::starts_with(oAttrName, "__") || !boost::ends_with(oAttrName, "__"))
85  {
86  PyObject * poAttr = PyObject_GetAttr(poMainModule, poAttrName);
87 
88  // Make sure we don't delete any module objects.
89  if (poAttr && poAttr->ob_type != poMainModule->ob_type)
90  PyObject_SetAttr(poMainModule, poAttrName, NULL);
91  Py_DecRef(poAttr);
92  }
93  Py_DecRef(poAttrName);
94  }
95  Py_DecRef(poAttrIter);
96  Py_DecRef(poAttrList);
97 #endif
98 
99  return model;
100  }
101  } // namespace python
102 } // namespace pinocchio
boost::python
python.hpp
filename
filename
python
pinocchio::python::buildModel
Model buildModel(const std::string &filename, const std::string &model_name)
Load a model from a Python script.
Definition: bindings/python/parsers/python/model.cpp:23
t
Transform3f t
pinocchio::ModelTpl
Definition: context/generic.hpp:20
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1116
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Fri Jun 7 2024 02:40:48