-
Notifications
You must be signed in to change notification settings - Fork 35
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
Implement a machinery to easily access model locations on C++ and Python #130
Merged
traversaro
merged 5 commits into
robotology:devel
from
GiulioRomualdi:icub-models-library
Feb 16, 2022
+1,206
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d47d7bb
Implement the iCubModels c++ library
GiulioRomualdi b69a438
Implement the python bindings for the iCubModels library
GiulioRomualdi a9ad867
Apply suggestions from code review
GiulioRomualdi 6da40e2
Update the README with the instruction to use the C++ and python libr…
GiulioRomualdi 86e30f0
Update the CHANGELOG
GiulioRomualdi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# GNU Lesser General Public License v2.1 or any later version. | ||
|
||
if(ICUB_MODELS_COMPILE_PYTHON_BINDINGS) | ||
|
||
find_package(pybind11 REQUIRED) | ||
find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
|
||
|
||
# define new line accordingly to the operating system | ||
if (WIN32) | ||
set(NEW_LINE "\n\r") | ||
else() | ||
set(NEW_LINE "\n") | ||
endif() | ||
|
||
option(ICUB_MODELS_DETECT_ACTIVE_PYTHON_SITEPACKAGES | ||
"Do you want icub-models to detect and use the active site-package directory? (it could be a system dir)" | ||
FALSE) | ||
|
||
# Install the resulting Python package for the active interpreter | ||
if(ICUB_MODELS_DETECT_ACTIVE_PYTHON_SITEPACKAGES) | ||
set(PYTHON_INSTDIR ${Python3_SITELIB}/icub_models) | ||
else() | ||
execute_process(COMMAND ${Python3_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(1,0,prefix=''))" | ||
OUTPUT_VARIABLE _PYTHON_INSTDIR) | ||
|
||
string(STRIP ${_PYTHON_INSTDIR} _PYTHON_INSTDIR_CLEAN) | ||
set(PYTHON_INSTDIR ${_PYTHON_INSTDIR_CLEAN}/icub_models) | ||
endif() | ||
|
||
# Folder of the Python package within the build tree. | ||
# It is used for the Python tests. | ||
set(ICUB_MODELS_PYTHON_PACKAGE "${CMAKE_BINARY_DIR}/icub_models") | ||
|
||
# Add the bindings directory | ||
add_subdirectory(python) | ||
|
||
# Create the __init__.py file | ||
file(GENERATE | ||
OUTPUT "${ICUB_MODELS_PYTHON_PACKAGE}/__init__.py" | ||
CONTENT "from icub_models.bindings import *") | ||
|
||
# Install the __init__.py file | ||
install(FILES "${ICUB_MODELS_PYTHON_PACKAGE}/__init__.py" | ||
DESTINATION ${PYTHON_INSTDIR}) | ||
|
||
# Install pip metadata files to ensure that icub_models installed via CMake is listed by pip list | ||
# See https://packaging.python.org/specifications/recording-installed-packages/ | ||
# and https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata | ||
option(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL "Use CMake to install Python pip metadata. Set to off if some other tool already installs it." ON) | ||
mark_as_advanced(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL) | ||
set(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER "cmake" CACHE STRING "Specify the string to identify the pip Installer. Default: cmake, change this if you are using another tool.") | ||
mark_as_advanced(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER) | ||
if(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL) | ||
get_filename_component(PYTHON_METADATA_PARENT_DIR ${PYTHON_INSTDIR} DIRECTORY) | ||
if(WIN32) | ||
set(NEW_LINE "\n\r") | ||
else() | ||
set(NEW_LINE "\n") | ||
endif() | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/METADATA "") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Metadata-Version: 2.1${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Name: icub-models${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Version: ${PROJECT_VERSION}${NEW_LINE}") | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/INSTALLER "${ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER}${NEW_LINE}") | ||
install( | ||
FILES "${CMAKE_CURRENT_BINARY_DIR}/METADATA" "${CMAKE_CURRENT_BINARY_DIR}/INSTALLER" | ||
DESTINATION ${PYTHON_METADATA_PARENT_DIR}/icub_models-${PROJECT_VERSION}.dist-info) | ||
endif() | ||
|
||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# GNU Lesser General Public License v2.1 or any later version. | ||
|
||
pybind11_add_module(pybind11_icub_models MODULE | ||
${CMAKE_CURRENT_SOURCE_DIR}/icub_models.cpp | ||
) | ||
|
||
target_include_directories(pybind11_icub_models PUBLIC "$<BUILD_INTERFACE:${pybind_include_dirs}>") | ||
|
||
target_link_libraries(pybind11_icub_models PRIVATE | ||
icub-models::icub-models) | ||
|
||
# # The generated Python dynamic module must have the same name as the pybind11 | ||
# # module, i.e. `bindings`. | ||
set_target_properties(pybind11_icub_models PROPERTIES | ||
LIBRARY_OUTPUT_DIRECTORY "${ICUB_MODELS_PYTHON_PACKAGE}" | ||
OUTPUT_NAME "bindings") | ||
|
||
|
||
# Output package is: | ||
traversaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# icub_models | ||
# |-- __init__.py (generated from main bindings CMake file) | ||
# `-- bindings.<cpython_extension> | ||
|
||
install(TARGETS pybind11_icub_models DESTINATION ${PYTHON_INSTDIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @file icub_models.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2022 Istituto Italiano di Tecnologia Released under the terms of the Creative Commons Attribution Share Alike 4.0 International | ||
*/ | ||
|
||
#include <iCubModels/iCubModels.h> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
namespace iCubModels | ||
{ | ||
namespace | ||
{ | ||
|
||
namespace py = ::pybind11; | ||
PYBIND11_MODULE(bindings, m) | ||
{ | ||
m.doc() = "Python bindings for the icub-models."; | ||
|
||
m.def("get_models_path", &iCubModels::getModelsPath, | ||
"Get the folder where the models are installed.") | ||
.def("get_robot_names", &iCubModels::getRobotNames, | ||
"Return a set containing the names of the robots installed.") | ||
.def("get_model_file", &iCubModels::getModelFile, py::arg("model_name"), | ||
"Return the path of the model given its name. If the 'model_name' is not in the list " | ||
"of the installed robot an empty path is returned."); | ||
} | ||
|
||
} // namespace | ||
} // namespace iCubModels |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that even if we call python package name
icub-models
, the directory name here should use underscore, see conda-forge/opencv-feedstock#300 (comment) .