diff --git a/README.md b/README.md index b1c82b6..b28f443 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This project provides Python bindings for [`gz-msgs`](https://github.com/gazebos ### Install Gazebo -Follow the installation instructions for [Gazebo Garden](https://gazebosim.org/docs/garden). +Follow the installation instructions for [Gazebo Harmonic](https://gazebosim.org/docs/harmonic). This project depends directly on [`gz-msgs`](https://github.com/gazebosim/gz-msgs) and [`gz-transport`](https://github.com/gazebosim/gz-transport). These may be either available as system installs or in a source install in a local workspace folder which we assume is `~/gz_ws`. ### Install `gz-python` @@ -29,10 +29,10 @@ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python ``` Set the environment variable determing the Gazebo version. -The default is `garden`: +The default is `harmonic`, although `garden` is also supported: ```bash -export GAZEBO_VERSION=garden +export GAZEBO_VERSION=harmonic ``` Then create a build directory, configure and make: @@ -46,23 +46,42 @@ make ### Update the Python environment -Update the PYTHONPATH to include the location of the extension modules and the -generated Python protobuf bindings. +Update the `PYTHONPATH` to include the location of the extension modules: ```bash cd ~/gz_ws/src/gz-python export PYTHONPATH=${PYTHONPATH}:$(pwd)/build/python ``` +As `gz-msgs` now generates Python bindings for its `*.proto` definitions +this project no longer generates them, so ensure that the `PYTHONPATH` also includes the path to the Gazebo Python bindings. + + ## Usage ### `gz-msg` bindings The Python bindings for `gz-msgs` are the standard generated protobuf code -for Python. For example `gz-msgs/proto/gz/msgs/time.proto` may be used as follows: +for Python. + +Gazebo uses versioned module names, to use unversioned module names add the following before importing messages: + +```python +import os + +# alias gz.msgs{GZ_MSGS_VER} to gz.msgs +if os.environ["GZ_VERSION"] == "garden": + from gz import msgs9 as msgs +elif os.environ["GZ_VERSION"] == "harmonic": + from gz import msgs10 as msgs +else: + raise Exception(f"Invalid GZ_VERSION: {os.environ['GZ_VERSION']}") +``` + +In the remaining examples the unversioned imports for messages are assumed. +For example `gz-msgs/proto/gz/msgs/time.proto` may be used as follows: ```python -# example.py from gz.msgs.time_pb2 import Time msg = Time() @@ -73,7 +92,7 @@ print(msg) ### `gz-transport` bindings -The Python bindings for `gz-transport` are contained in a module called `transport`. +The Python bindings for `gz-transport` are contained in a module called `gz.python.transport`. The object naming and usage for the most part follows the C++ interface, so the C++ Gazebo Tutorials are a good guide on how to use the library. @@ -82,8 +101,8 @@ Publish: ```python from gz.msgs.stringmsg_pb2 import StringMsg -from gz.transport import AdvertiseMessageOptions -from gz.transport import Node +from gz.python.transport import AdvertiseMessageOptions +from gz.python.transport import Node # Create a transport node node = Node() @@ -108,8 +127,8 @@ import typing from gz.msgs.stringmsg_pb2 import StringMsg -from gz.transport import SubscribeOptions -from gz.transport import Node +from gz.python.transport import SubscribeOptions +from gz.python.transport import Node def cb(msg: StringMsg) -> None: print("Msg: [{}] from Python".format(msg.data)) @@ -386,7 +405,7 @@ On macOS Bazel can be installed with `brew`: brew install bazel ``` -The [Google protocol buffers compiler](https://github.com/protocolbuffers/protobuf) version `3.19` is also required and can be installed with: +The [Google protocol buffers compiler](https://github.com/protocolbuffers/protobuf) version `25.0` is also required and can be installed with: ```bash @@ -502,11 +521,11 @@ py_binary( ### Protobuf compiler version -The project depends on `protoc` version `3.19.1`. You must ensure that the version of `protoc` installed by `brew` matches (otherwise the examples will segfault). +The project depends on `protoc` version `25.0`. You must ensure that the version of `protoc` installed by `brew` matches (otherwise the examples will segfault). ```bash $ protoc --version -libprotoc 3.19.1 +libprotoc 25.0 ``` ### Protobuf generated Python libraries