-
Notifications
You must be signed in to change notification settings - Fork 214
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
Python API #640
Comments
We don't currently have Python bindings available for the ROS 2 version of RViz. I've assigned the issue to one of the rviz maintainers in ROS 2 to take a look at the feasibility of doing so. Likely this will need community contribution to do so, as it isn't on our current roadmap. |
I don't know much about the Python bindings in ROS 1, but it looks like we are generating the Python API from the C++ code with shiboken and/or sip. So I think doing something similar should be feasible for ROS 2. For anyone who is willing to tackle this, I'd rather see shiboken used for Python API generation over sip. This is mainly because of licensing (shiboken is LGPL, while sip is GPL). Furthermore, IIUC, Qt has officially adopted shiboken as well. It looks like the shiboken generator was disabled a long time ago in ROS 1, but I guess it doesn't mean much for ROS 2, since the C++ code is very different. Another option for exposing a Python API could be to use pybind11. FWIW, we are using pybind11 for rosbag2 Python bindings and are currently in progress of refactoring rclpy to use pybind11. This needs someone to take a deeper look, but I'd expect we might have an easier time using shiboken considering it actively being used by other Qt-based projects. |
I have made a first attempt with shiboken here: https://github.com/ipa-cmh/rviz/tree/add-python-bindings |
Hello, I want to port one python software to ROS2 and that is required python bindings for RVIZ. Can I help with this PR? what is required for the merge? |
@friackazoid there is #889 which works under circumstances. There is still a build dependency problem which I have only been able to solve for local build with python_qt_binding in same workspace. Also to merge Windows support is required. I have no idea where to start and test that. I have a more recent version that we use in some internal projects - still throws a lot of warnings durign build, but works. Honestly, my funding for this activity got scrapped, so I cannot pursue it further right now. I will try and push the latest working version I have to #889. Also check out ros-visualization/python_qt_binding#114 which documents my effort to update python_qt_bindings. |
Thanks for your nice effort @ipa-cmh. I would like to try VisualizationFramePy in my program. I downloaded the latest PR: https://github.com/ipa-cmh/rviz/tree/f324c63a5035c2351ddf599fe75e1995119ea89c I tried to build it in this environment: But I am stuck at this colcon_cmake exception:
At The colcon code: I have installed python3_pyside2 and python3_qtpy (though not by rosdep, but by apt install Not sure what build environment is further required. Thanks. |
I tried hard locating the problem using the verbose build command: and is able to locate the exception location (caused by I substituted However, there is still a missing cmake function Then I found this: https://github.com/ipa-cmh/python_qt_binding/tree/dd8e43fca94f4cd3f88952d4093270047da4c7cf. After adding the last puzzle, the project can be built. And I found I can further build with iron rviz2, by simply adding VisualizationFrame() to rviz_common (3e03e30) |
@KKSTB sorry mate, I was on vacation and it has been some time I worked on this, currently the funding has gone away again. In general, I had quite some problems with making this build stably, because the build cache and install folders usually contain some stuff that will make you miss some errors when doing changes. In the end, we are not using rviz2 for end-user interfaces anymore. We now switched to nicegui. |
Hi nice to hear from you @ipa-cmh. I try to use the successfully built rviz_python_bindings in a freshly created ROS2 python package with an empty pyside window. It displays an empty window as intended, but it crashes with segmentation fault once I add a line to import rviz_shiboken:
Hope that there would be help on how to solve this issue. |
Try the following for import, that is how I have it in our project. import qtpy
from qtpy.QtGui import *
from qtpy.QtCore import *
try:
from qtpy.QtWidgets import *
except ImportError:
pass
from rviz import YamlConfigReader, VisualizationFrame, Config, rviz_common You can than do the following: with open("parameters.yml", "w") as outfile:
yaml.dump(param_file, outfile, default_flow_style=False)
frame = VisualizationFrame(str(Path("parameters.yml").absolute()))
frame.setApp(app)
frame.setSplashPath("")
frame.initialize()
sleep(3.0)
frame.loadDisplayConfig(file) Finally you'll have to add the frame to your layout. |
Thanks for your quick reply. I found that it is my system installation problem. I found that if I use Qt classes in If I freshly create ROS2 python package without following rqt plugin example, and use After pip uninstalling pyside2 there is no crash when importing. Will continue trying. Thanks @ipa-cmh! |
Your example is very essential. I found that I have to call I think the major trouble is the difficulty to debug the issue. It often crashes with exit code -11 or causes segmentation fault without indicating the reason if configuration is not followed strictly (e.g. 1: pyside compatibility problem with |
There is yet another problem. Even though I can display the VisualizationFrame using my ROS2 package, the 3D screen is flashing. If I Minimal code of my ROS2 package: import os
import sys
import rviz
from qtpy.QtWidgets import QApplication
from ament_index_python.packages import get_package_share_directory
def main():
app = QApplication(sys.argv)
rviz_frame = rviz.VisualizationFrame()
rviz_frame.setApp(app)
rviz_frame.setSplashPath("")
rviz_frame.initialize(os.path.join(get_package_share_directory('test_gui'), 'resource', 'default.rviz'))
rviz_frame.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main() I believe it is not related to python API problem, since it also flashes if I embed rviz using the But in case someone has a solution it is very welcome. |
This is caused by an API change of the underlying c++-Code. There is also the issue that you have to pass in a smart node pointer to the constructor of VisualizationFrame now. Therefore, there is now some helper code necessary.
Overall the whole wrapping part is pretty complex and if you have pyside and pyqt installed there is also often confusion about the version that is used. Generally, I would only use and work on the wrapper if you really need it. Otherwise stick with C++ for now. |
Python is much easier and much faster to develop for average user. Also ROS1 has rviz python binding, so its normal to expect ROS2 to have it too. I tried to embed a C++ version of #include <qt5/QtWidgets/QApplication>
#include <qt5/QtWidgets/QMainWindow>
#include "rclcpp/rclcpp.hpp"
#include <rviz_common/visualization_frame.hpp>
#include "rviz_common/ros_integration/ros_client_abstraction.hpp"
#include "rviz_common/ros_integration/ros_node_abstraction.hpp"
int main(int argc, char* argv[])
{
// rclcpp::init(argc, argv);
// RCLCPP_INFO(rclcpp::get_logger("my_logger"), "start");
QApplication app(argc, argv);
QMainWindow window = QMainWindow();
std::unique_ptr<rviz_common::ros_integration::RosClientAbstraction> ros_client_abstraction_ = std::make_unique<rviz_common::ros_integration::RosClientAbstraction>();
rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node_ = ros_client_abstraction_->init(argc, argv, "rviz", false);
rviz_common::VisualizationFrame rviz_frame(rviz_ros_node_);
rviz_frame.setApp(&app);
rviz_frame.setSplashPath("");
rviz_frame.initialize(rviz_ros_node_);
window.setCentralWidget(&rviz_frame);
window.show();
RCLCPP_INFO(rclcpp::get_logger("my_logger"), "exec");
int ret = app.exec();
ros_client_abstraction_->shutdown();
// rclcpp::shutdown();
return ret;
} I also modified #include "rviz_python_bindings/visualizer_frame_py.hpp"
using namespace rviz_common;
VisualizerFramePy::VisualizerFramePy(QWidget * parent)
: VisualizationFrame(rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr(), parent)
{
ros_client_abstraction_ = std::make_unique<rviz_common::ros_integration::RosClientAbstraction>();
rviz_ros_node_ = ros_client_abstraction_->init(0, nullptr, "rviz", false);
}
VisualizerFramePy::~VisualizerFramePy()
{
ros_client_abstraction_->shutdown();
}
bool VisualizerFramePy::node_ok()
{
return ros_client_abstraction_->ok();
}
void VisualizerFramePy::initialize(
const QString & display_config_file)
{
VisualizationFrame::initialize(rviz_ros_node_, display_config_file);
} I tried with / without nvidia card as well. Since |
I found that in pyside environment, Whereas in C++ environment, If I comment |
The window size problem is related to the if condition inside resize event. After completely removing |
I am trying to build the rviz shiboken bindings on Win10. I am doing it in a VM. After resolving several problems (ROS2 directory path, shiboken2_generator installation, environment variables, typesystem-paths, path splitter, etc) I am still stuck at making shiboken generator to work on Win10.
I notice there are some strange args near the end ( @ipa-cmh are you familiar with shiboken2_generator and libclang? |
I ran shiboken2.exe directly to look into the problem. It was due to compatibility with After installing Finally shiboken2.exe crashes with no output for any #include involving rclcpp / any #include inside rclcpp. It is difficult to proceed any further with no output. So unless rviz adds a wrapper class / abstraction interface to completely hide rclcpp, there is little hope for rviz to support shiboken python binding on Win10 (i.e. only Ubuntu works). |
I want to create a UI using Qt5 and integrate ROS 2 visualization into it. How can I achieve this? |
Hi, im looking to migrate my package to ROS2.
This package uses the python_bindings of rviz in ROS1.
Are these also available somewhere for ROS2? I can't seem to find them.
The text was updated successfully, but these errors were encountered: