Skip to content

Commit

Permalink
Update docs to reflect changed module namespace.
Browse files Browse the repository at this point in the history
Signed-off-by: Rhys Mainwaring <[email protected]>
  • Loading branch information
srmainwaring committed Apr 30, 2024
1 parent f564b68 commit e6465f5
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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.

Expand All @@ -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()
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6465f5

Please sign in to comment.