Skip to content

Commit f8bd18c

Browse files
committed
Original rviz2/add-python-bindings branch by @ipa-cmh (https://github.com/ipa-cmh/rviz/tree/add-python-bindings), excluding /rviz_common/visualization_frame changes
1 parent 22951a2 commit f8bd18c

File tree

10 files changed

+714
-0
lines changed

10 files changed

+714
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
project(rviz_python_bindings)
3+
4+
set(shiboken_qt_components
5+
QtCore
6+
QtGui
7+
QtWidgets
8+
)
9+
10+
find_package(ament_cmake REQUIRED)
11+
find_package(ament_cmake_python REQUIRED)
12+
find_package(python_qt_binding REQUIRED)
13+
include(${python_qt_binding_DIR}/shiboken_helper.cmake) #${CMAKE_CURRENT_SOURCE_DIR}/../../python_qt_binding/cmake/shiboken_helper.cmake)#
14+
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets)
15+
find_package(rviz_common REQUIRED)
16+
find_package(Python3 COMPONENTS Development)
17+
18+
# name of the library to be generated by shiboken
19+
set(shiboken_bindings_library "rviz_shiboken")
20+
# path to the include wrapper used by shiboken for generation
21+
set(shiboken_include_wrapper "${CMAKE_CURRENT_SOURCE_DIR}/conf/global.hpp")
22+
# path to the typesystem file used by shiboken for generation
23+
set(shiboken_typesystem "${CMAKE_CURRENT_SOURCE_DIR}/conf/typesystem.xml")
24+
# sources generated by shiboken depends on typesystem
25+
set(shiboken_generated_sources
26+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_shiboken_module_wrapper.cpp
27+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_wrapper.cpp
28+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_visualizerframepy_wrapper.cpp
29+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_yamlconfigreader_wrapper.cpp
30+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_yamlconfigwriter_wrapper.cpp
31+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_config_wrapper.cpp
32+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_config_mapiterator_wrapper.cpp
33+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_visualizationframe_wrapper.cpp
34+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_visualizationmanager_wrapper.cpp
35+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_displaygroup_wrapper.cpp
36+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_display_wrapper.cpp
37+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_viewmanager_wrapper.cpp
38+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_paneldockwidget_wrapper.cpp
39+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_tool_wrapper.cpp
40+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_toolmanager_wrapper.cpp
41+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_properties_wrapper.cpp
42+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_properties_property_wrapper.cpp
43+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_properties_boolproperty_wrapper.cpp
44+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_common_viewcontroller_wrapper.cpp
45+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_rendering_ogrelogging_wrapper.cpp
46+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/rviz_rendering_wrapper.cpp
47+
${CMAKE_CURRENT_BINARY_DIR}/${shiboken_bindings_library}/ogre_wrapper.cpp
48+
)
49+
50+
ament_get_recursive_properties(deps_include_dirs _ ${rviz_common_TARGETS})
51+
list(APPEND shiboken_include_dirs
52+
${deps_include_dirs})
53+
message(STATUS "ROS inlcude dirs: ${deps_include_dirs}")
54+
message(STATUS "QT5 inlcude dirs: ${Qt5Core_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}")
55+
list(APPEND shiboken_include_dirs
56+
${Qt5Core_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}
57+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
58+
$<INSTALL_INTERFACE:include>
59+
)
60+
61+
add_library(rviz_python SHARED
62+
src/visualizer_frame_py.cpp
63+
)
64+
65+
target_include_directories(rviz_python PUBLIC
66+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
67+
$<INSTALL_INTERFACE:include>)
68+
target_compile_options(rviz_python PUBLIC -Wl,--no-undefined)
69+
ament_target_dependencies(rviz_python
70+
rviz_common
71+
Qt5
72+
)
73+
74+
install(TARGETS rviz_python
75+
EXPORT export_rviz_python
76+
ARCHIVE DESTINATION lib
77+
LIBRARY DESTINATION lib
78+
RUNTIME DESTINATION bin
79+
)
80+
81+
82+
shiboken_generator_ext(
83+
"rviz_shiboken" # TARGET
84+
"${shiboken_include_wrapper}" # INCLUDE WRAPPER
85+
"${shiboken_typesystem}" # TYPSYSTEM
86+
"${CMAKE_CURRENT_BINARY_DIR}" # Working Directory
87+
"${shiboken_generated_sources}" # Generated Sources
88+
"${shiboken_include_dirs}" # INCLUDE DIRS
89+
"${CMAKE_CURRENT_BINARY_DIR}" # BUILD DIR
90+
)
91+
92+
add_library(
93+
${shiboken_bindings_library} MODULE
94+
${shiboken_generated_sources})
95+
96+
shiboken_include_directories(
97+
${shiboken_bindings_library}
98+
"${shiboken_qt_components}")
99+
100+
target_include_directories(
101+
${shiboken_bindings_library} PRIVATE
102+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
103+
$<INSTALL_INTERFACE:include>
104+
${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}
105+
)
106+
107+
target_link_libraries(
108+
${shiboken_bindings_library}
109+
Qt5::Core
110+
Qt5::Gui
111+
Qt5::Widgets
112+
rviz_python
113+
)
114+
ament_target_dependencies(
115+
${shiboken_bindings_library}
116+
rviz_common
117+
)
118+
shiboken_target_link_libraries(${shiboken_bindings_library} ${shiboken_qt_components})
119+
120+
121+
122+
set_target_properties(${shiboken_bindings_library} PROPERTIES
123+
PREFIX "")
124+
set_property(TARGET ${shiboken_bindings_library} PROPERTY OUTPUT_NAME
125+
"${shiboken_bindings_library}.${Python3_SOABI}")
126+
127+
if(WIN32)
128+
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
129+
set_property(TARGET ${shiboken_bindings_library} PROPERTY SUFFIX "_d.pyd")
130+
else()
131+
set_property(TARGET ${shiboken_bindings_library} PROPERTY SUFFIX ".pyd")
132+
endif()
133+
target_link_libraries(
134+
${shiboken_bindings_library}
135+
Python3_LIBRARIES
136+
)
137+
endif()
138+
139+
ament_python_install_package(rviz)
140+
install(TARGETS ${shiboken_bindings_library}
141+
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_DIR}/rviz
142+
)
143+
144+
145+
if(BUILD_TESTING)
146+
find_package(ament_lint_auto REQUIRED)
147+
find_package(ament_cmake_pytest REQUIRED)
148+
# the following line skips the linter which checks for copyrights
149+
# comment the line when a copyright and license is added to all source files
150+
set(ament_cmake_copyright_FOUND TRUE)
151+
# the following line skips cpplint (only works in a git repo)
152+
# comment the line when this package is in a git repo and when
153+
# a copyright and license is added to all source files
154+
set(ament_cmake_cpplint_FOUND TRUE)
155+
156+
ament_lint_auto_find_test_dependencies()
157+
endif()
158+
159+
ament_export_include_directories(
160+
include
161+
)
162+
ament_export_libraries(
163+
rviz_python
164+
)
165+
ament_export_targets(
166+
export_rviz_python
167+
)
168+
169+
ament_package()

rviz_python_bindings/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# RVIZ Pyhon Bindings
2+
3+
## Usage
4+
To use the python bindings, you need to do the following:
5+
6+
```python
7+
import os
8+
os.environ["QT_API"] = "pyside2"
9+
from rviz import rviz_common
10+
```
11+
12+
Currently only pyside2 is supported. No stubs are generated, so pylance does not work on namespaces.
13+
The following classes are wrapped:
14+
* VisualizationFrame
15+
* Config
16+
* VisualizationManager
17+
* ViewController
18+
* ViewManager
19+
* DisplayGroup
20+
* Display
21+
* PanelDockWidget
22+
* Tool
23+
* ToolManager
24+
* YamlConfigReader
25+
* YamlConfigWriter
26+
* Property
27+
* BoolProperty
28+
29+
## Windows specifics
30+
On windows you will need to install the following dependencies manually:
31+
32+
```
33+
pip install \
34+
--index-url=https://download.qt.io/official_releases/QtForPython/ \
35+
--trusted-host download.qt.io \
36+
shiboken2 pyside2 shiboken2_generator
37+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2011, Willow Garage, Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* * Neither the name of the Willow Garage, Inc. nor the names of its
14+
* contributors may be used to endorse or promote products derived from
15+
* this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
*/
29+
30+
#ifndef PYTHON_GLOBAL_H
31+
#define PYTHON_GLOBAL_H
32+
33+
#include <pyside2_global.h>
34+
#include <QtCore/QtCore>
35+
#include <QtGui/QtGui>
36+
#include <QtWidgets/QtWidgets>
37+
#include <rviz_common/visualization_frame.hpp>
38+
#include <rviz_common/visualization_manager.hpp>
39+
#include <rviz_common/display_group.hpp>
40+
#include <rviz_common/display.hpp>
41+
#include <rviz_common/view_manager.hpp>
42+
#include <rviz_common/tool.hpp>
43+
#include <rviz_common/tool_manager.hpp>
44+
#include <rviz_common/panel_dock_widget.hpp>
45+
#include <rviz_common/properties/bool_property.hpp>
46+
#include <rviz_common/properties/property.hpp>
47+
#include <rviz_python_bindings/visualizer_frame_py.hpp>
48+
#include <rviz_common/yaml_config_reader.hpp>
49+
#include <rviz_common/yaml_config_writer.hpp>
50+
#include <rviz_common/config.hpp>
51+
#include <rviz_common/view_controller.hpp>
52+
#include <rviz_rendering/ogre_logging.hpp>
53+
54+
#endif // PYTHON_GLOBAL_H
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0"?>
2+
<typesystem package="rviz_shiboken">
3+
4+
<load-typesystem name="typesystem_core.xml" generate="no"/>
5+
<load-typesystem name="typesystem_gui.xml" generate="no"/>
6+
<load-typesystem name="typesystem_widgets.xml" generate="no"/>
7+
<namespace-type name="Ogre">
8+
<primitive-type name="Vector3"/>
9+
</namespace-type>
10+
<namespace-type name="rviz_common">
11+
<object-type name="VisualizationFrame"/>
12+
<object-type name="VisualizationManager"/>
13+
<object-type name="ViewManager"/>
14+
<object-type name="DisplayGroup"/>
15+
<object-type name="Display"/>
16+
<object-type name="VisualizerFramePy"/>
17+
<object-type name="PanelDockWidget"/>
18+
<object-type name="Tool"/>
19+
<object-type name="ToolManager"/>
20+
<object-type name="YamlConfigReader"/>
21+
<object-type name="YamlConfigWriter"/>
22+
<object-type name="ViewController"/>
23+
<object-type name="Config">
24+
<enum-type name="Type"/>
25+
<object-type name="MapIterator"/>
26+
</object-type>
27+
<namespace-type name="properties">
28+
<object-type name="Property"/>
29+
<object-type name="BoolProperty"/>
30+
</namespace-type>
31+
</namespace-type>
32+
<namespace-type name="rviz_rendering">
33+
<object-type name="OgreLogging"/>
34+
</namespace-type>
35+
</typesystem>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef VISUALIZER_FRAME_PY_HPP
2+
#define VISUALIZER_FRAME_PY_HPP
3+
4+
#include "rviz_common/visualization_frame.hpp"
5+
#include "rviz_common/ros_integration/ros_client_abstraction.hpp"
6+
#include "rviz_common/ros_integration/ros_node_abstraction.hpp"
7+
8+
namespace rviz_common
9+
{
10+
11+
class VisualizerFramePy : public VisualizationFrame
12+
{
13+
protected:
14+
std::unique_ptr<rviz_common::ros_integration::RosClientAbstraction> ros_client_abstraction_;
15+
16+
public:
17+
explicit VisualizerFramePy(
18+
QWidget * parent = nullptr);
19+
20+
~VisualizerFramePy()
21+
{
22+
ros_client_abstraction_->shutdown();
23+
}
24+
25+
bool node_ok();
26+
27+
void initialize(const QString & display_config_file = "");
28+
};
29+
}
30+
31+
#endif

rviz_python_bindings/package.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>rviz_python_bindings</name>
5+
<version>0.0.0</version>
6+
<description>RViz python bindings</description>
7+
<maintainer email="[email protected]">christoph</maintainer>
8+
<license>TODO: License declaration</license>
9+
10+
<buildtool_depend>ament_cmake</buildtool_depend>
11+
12+
<test_depend>ament_lint_auto</test_depend>
13+
<test_depend>ament_lint_common</test_depend>
14+
15+
<depend>rviz_common</depend>
16+
<depend>rviz_rendering</depend>
17+
<depend>python3_pyside2</depend>
18+
<depend>python3_qtpy</depend>
19+
20+
<export>
21+
<build_type>ament_cmake</build_type>
22+
</export>
23+
</package>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Python package 'rviz' initialization.
2+
#
3+
# The actual implementations are defined in C++ and wrapped by
4+
# shiboken or sip. This wrapper finds which binding is available and
5+
# presents it as package rviz.
6+
7+
from qtpy import PYSIDE2
8+
9+
if PYSIDE2:
10+
# Import the shared library generated by the shiboken binding-generator.
11+
from . import rviz_shiboken as bindings
12+
13+
# Expose the contained rviz class as bindings
14+
rviz_common = bindings.rviz_common
15+
VisualizationFrame = bindings.rviz_common.VisualizerFramePy
16+
Config = bindings.rviz_common.Config
17+
VisualizationManager = bindings.rviz_common.VisualizationManager
18+
ViewManager = bindings.rviz_common.ViewManager
19+
DisplayGroup = bindings.rviz_common.DisplayGroup
20+
Display = bindings.rviz_common.Display
21+
PanelDockWidget = bindings.rviz_common.PanelDockWidget
22+
Tool = bindings.rviz_common.Tool
23+
ToolManager = bindings.rviz_common.ToolManager
24+
YamlConfigReader = bindings.rviz_common.YamlConfigReader
25+
YamlConfigWriter = bindings.rviz_common.YamlConfigWriter
26+
Property = bindings.rviz_common.properties.Property
27+
BoolProperty = bindings.rviz_common.properties.BoolProperty
28+
else:
29+
raise ImportError(
30+
"Python Bindings for rviz are only available for PySide2."
31+
"Please set ENV Variable Qt_API to 'pyside2'"
32+
)

0 commit comments

Comments
 (0)