Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pinocchio_python_parser target #2475

Merged
merged 11 commits into from
Nov 8, 2024
14 changes: 2 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ cmake_dependent_option(
BUILD_PYTHON_BINDINGS_WITH_BOOST_MPFR_SUPPORT
"Build the Python interface with Boost.Multiprecision MPFR support" OFF BUILD_PYTHON_INTERFACE
OFF)
cmake_dependent_option(
BUILD_WITH_LIBPYTHON "Link to libpython to embed an interpreter for the python_parser feature" ON
"BUILD_PYTHON_INTERFACE" OFF)
cmake_dependent_option(BUILD_WITH_LIBPYTHON "Build the library with Python format support" ON
"BUILD_PYTHON_INTERFACE" OFF)
if(APPLE)
option(BUILD_WITH_ACCELERATE_SUPPORT "Build Pinocchio with the Accelerate support" OFF)
else(APPLE)
Expand Down Expand Up @@ -364,15 +363,6 @@ if(BUILD_WITH_EXTRA_SUPPORT)
${${PROJECT_NAME}_BINDINGS_PYTHON_EXTRA_SOURCES})
endif()

# LibPython sources
if(BUILD_WITH_LIBPYTHON)
list(APPEND ${PROJECT_NAME}_CORE_PUBLIC_HEADERS ${${PROJECT_NAME}_LIBPYTHON_PUBLIC_HEADERS})
list(APPEND ${PROJECT_NAME}_BINDINGS_PYTHON_SOURCES
${${PROJECT_NAME}_BINDINGS_PYTHON_LIBPYTHON_SOURCES})
list(APPEND ${PROJECT_NAME}_BINDINGS_PYTHON_PUBLIC_HEADERS
${${PROJECT_NAME}_BINDINGS_PYTHON_LIBPYTHON_PUBLIC_HEADERS})
endif(BUILD_WITH_LIBPYTHON)

# HPP-FCL sources
if(BUILD_WITH_HPP_FCL_SUPPORT)
if(ENABLE_TEMPLATE_INSTANTIATION)
Expand Down
13 changes: 0 additions & 13 deletions bindings/python/parsers/python/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "pinocchio/bindings/python/parsers/python.hpp"

#include <iostream>
#include <Python.h>
#include <boost/version.hpp>
#include <boost/algorithm/string/predicate.hpp>

Expand All @@ -28,11 +26,6 @@ namespace pinocchio
// Get a dict for the global namespace to exec further python code with
bp::dict globals = bp::extract<bp::dict>(main_module.attr("__dict__"));

// We need to link to the pinocchio PyWrap. We delegate the dynamic loading to the python
jcarpent marked this conversation as resolved.
Show resolved Hide resolved
// interpreter.
bp::object cpp_module(
(bp::handle<>(bp::borrowed(PyImport_AddModule("libpinocchio_pywrap")))));
jcarpent marked this conversation as resolved.
Show resolved Hide resolved

// That's it, you can exec your python script, starting with a model you
// can update as you want.
try
Expand Down Expand Up @@ -67,10 +60,6 @@ namespace pinocchio

// close the interpreter
// cf. https://github.com/numpy/numpy/issues/8097
#if PY_MAJOR_VERSION < 3
Py_Finalize();
#else
jcarpent marked this conversation as resolved.
Show resolved Hide resolved

PyObject * poMainModule = PyImport_AddModule("__main__");
PyObject * poAttrList = PyObject_Dir(poMainModule);
PyObject * poAttrIter = PyObject_GetIter(poAttrList);
Expand All @@ -94,8 +83,6 @@ namespace pinocchio
}
Py_DecRef(poAttrIter);
Py_DecRef(poAttrList);
#endif

return model;
}
} // namespace python
Expand Down
9 changes: 3 additions & 6 deletions sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ set(${PROJECT_NAME}_SDF_PUBLIC_HEADERS
set(${PROJECT_NAME}_LIBPYTHON_PUBLIC_HEADERS
${PROJECT_SOURCE_DIR}/include/pinocchio/parsers/python.hpp)

set(${PROJECT_NAME}_LIBPYTHON_SOURCES
${PROJECT_SOURCE_DIR}/bindings/python/parsers/python/model.cpp)

set(${PROJECT_NAME}_EXTRA_SOURCES ${PROJECT_SOURCE_DIR}/src/extra/reachable-workspace.cpp)

set(${PROJECT_NAME}_EXTRA_PUBLIC_HEADERS
Expand Down Expand Up @@ -594,12 +597,6 @@ set(${PROJECT_NAME}_BINDINGS_PYTHON_SOURCES
${PROJECT_SOURCE_DIR}/bindings/python/parsers/mjcf/geometry.cpp
${PROJECT_SOURCE_DIR}/bindings/python/extra/expose-extras.cpp)

set(${PROJECT_NAME}_BINDINGS_PYTHON_LIBPYTHON_SOURCES
${PROJECT_SOURCE_DIR}/bindings/python/parsers/python/model.cpp)

set(${PROJECT_NAME}_BINDINGS_PYTHON_LIBPYTHON_PUBLIC_HEADERS
${PROJECT_SOURCE_DIR}/include/pinocchio/bindings/python/parsers/python.hpp)

set(${PROJECT_NAME}_BINDINGS_PYTHON_HPP_FCL_SOURCES
${PROJECT_SOURCE_DIR}/bindings/python/collision/expose-broadphase.cpp
${PROJECT_SOURCE_DIR}/bindings/python/collision/expose-broadphase-callbacks.cpp
Expand Down
16 changes: 16 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ if(BUILD_WITH_CASADI_SUPPORT)
target_link_libraries(${PROJECT_NAME}_casadi ${CASADI_SCOPE} casadi)
endif()

if(BUILD_WITH_LIBPYTHON)
set(LIBPYTHON_LIB_NAME "${PROJECT_NAME}_python_parser")

pinocchio_target(
${LIBPYTHON_LIB_NAME}
SCALAR default
SOURCES ${${PROJECT_NAME}_LIBPYTHON_SOURCES} ${${PROJECT_NAME}_LIBPYTHON_PUBLIC_HEADERS})
pinocchio_config(python_parser ${LIBPYTHON_LIB_NAME})

target_link_libraries(${LIBPYTHON_LIB_NAME} PUBLIC ${PROJECT_NAME}_default Python3::Python)
target_link_boost_python(${LIBPYTHON_LIB_NAME} PUBLIC)
if(WIN32)
target_link_libraries(${PYTHON_LIB_NAME} PUBLIC ${PYTHON_LIBRARY})
endif()
endif()

# Define main target (default, parsers, extra).
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
Expand Down
15 changes: 10 additions & 5 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function(ADD_PINOCCHIO_UNIT_TEST name)
EXTRA
COLLISION
PARALLEL
PYTHON_PARSER
PARSERS_OPTIONAL
EXTRA_OPTIONAL
COLLISION_OPTIONAL
Expand Down Expand Up @@ -93,6 +94,10 @@ function(ADD_PINOCCHIO_UNIT_TEST name)
target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME}_extra)
endif()

if(unit_test_PYTHON_PARSER)
target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME}_python_parser)
endif()

modernize_target_link_libraries(
${TEST_NAME}
SCOPE PRIVATE
Expand Down Expand Up @@ -223,23 +228,23 @@ if(BUILD_WITH_EXTRA_SUPPORT)
endif()

if(BUILD_WITH_LIBPYTHON)
add_pinocchio_unit_test(python_parser PACKAGES Python3::Python)
add_pinocchio_unit_test(python_parser PYTHON_PARSER)
get_cpp_test_name(python_parser ${CMAKE_CURRENT_SOURCE_DIR} python_parser_target)
target_include_directories(${python_parser_target} SYSTEM PUBLIC ${PYTHON_INCLUDE_DIRS})

target_link_libraries(${python_parser_target} PUBLIC ${PYWRAP}_default)
target_link_libraries(${python_parser_target} PUBLIC ${PYTHON_LIBRARIES})
get_test_property(${python_parser_target} ENVIRONMENT ENV_VARIABLES)
compute_pythonpath(PYTHON_ENV_VARIABLES "bindings/python")
list(APPEND ENV_VARIABLES ${PYTHON_ENV_VARIABLES})
if(WIN32)
# This line is mandatory because of Github action. The test run well on Windowns + Conda. This
# This line is mandatory because of Github action. The test run well on Windows + Conda. This
# hide something wrong. Maybe the test is linking against the wrong Python library or call the
# wrong interpreter.
get_filename_component(_PYTHONHOME ${PYTHON_EXECUTABLE} PATH)
list(APPEND ENV_VARIABLES "PYTHONHOME=${_PYTHONHOME}")
endif()
set_tests_properties(${python_parser_target} PROPERTIES ENVIRONMENT "${ENV_VARIABLES}")
endif()

if(BUILD_PYTHON_INTERFACE)
add_subdirectory(python)
endif()

Expand Down
Loading