OctoMap Python Bindings (cmeel)
cmeel-octomap provides efficient Python bindings for the OctoMap library, a probabilistic 3D mapping framework based on octrees. It enables users to build, query, and manage 3D occupancy maps in Python. The current version is 1.10.0, with updates typically aligned with upstream OctoMap releases or cmeel improvements.
Warnings
- gotcha OctoMap uses a specific coordinate system convention (e.g., Z-up). Mismatches with other libraries or sensor data (e.g., ROS, custom camera setups) are a common source of bugs. Always verify your coordinate transformations.
- gotcha OctoMaps, especially at fine resolutions and for large environments, can consume significant amounts of RAM. Monitor memory usage to avoid out-of-memory errors in your applications.
- gotcha As a wrapper around a C++ library, some errors might originate from the underlying OctoMap C++ code. These can sometimes manifest as cryptic Python exceptions or segfaults without clear Python tracebacks.
- gotcha There are multiple Python wrappers for OctoMap (e.g., `octomap-python`, `coctomap`). Ensure you are importing from the `octomap` module provided by `cmeel-octomap` to avoid conflicts or unexpected behavior if you have other wrappers installed.
Install
-
pip install cmeel-octomap
Imports
- OcTree
from cmeel_octomap import OcTree
from octomap import OcTree
- Point3D
from octomap import Point3D
Quickstart
from octomap import OcTree, Point3D
# Create an OcTree with a resolution of 0.1 meters
tree = OcTree(0.1)
# Insert some occupied points
tree.insert_point(Point3D(0.0, 0.0, 0.0), occupied=True)
tree.insert_point(Point3D(0.1, 0.0, 0.0), occupied=True)
tree.insert_point(Point3D(0.5, 0.5, 0.5), occupied=True)
# Insert a free point (clears any occupied status at this node)
tree.insert_point(Point3D(0.0, 0.1, 0.0), occupied=False)
# Query occupancy of points
is_occupied_000 = tree.is_node_occupied(Point3D(0.0, 0.0, 0.0))
is_occupied_055 = tree.is_node_occupied(Point3D(0.5, 0.5, 0.5))
is_occupied_001 = tree.is_node_occupied(Point3D(0.0, 0.0, 1.0))
print(f"Point (0,0,0) is occupied: {is_occupied_000}")
print(f"Point (0.5,0.5,0.5) is occupied: {is_occupied_055}")
print(f"Point (0,0,1) is occupied: {is_occupied_001} (outside inserted points)")
# Get the total number of occupied nodes
print(f"Total occupied nodes: {tree.num_occupied_nodes}")
# You can also save and load the map (uncomment to run)
# tree.write_binary("my_octomap.bt")
# loaded_tree = OcTree("my_octomap.bt")
# print(f"Loaded tree has {loaded_tree.num_occupied_nodes} occupied nodes.")