py_trees.ports module
Typed input/output ports for py_trees behaviours.
- class py_trees.ports.BehaviourWithPorts(name: str, **kwargs: Any)
Bases:
PortsMixin,BehaviourBase class for behaviours with typed input and output ports (see
PortsMixin).Subclassing requirements:
Each subclass must implement the
input_portsandoutput_portsclass methods to specify its input and output ports.Each subclass must implement the
update()method to define its behaviour.Other methods from
py_trees.behaviour.Behaviourmay be overridden as needed.
Example usage:
class ExampleBehaviour(BehaviourWithPorts): @classmethod def input_ports(cls): return {"input_data": PortInformation(data_type=str, required=True)} @classmethod def output_ports(cls): return {"output_data": PortInformation(data_type=str, required=True)} def update(self): # Implementation of the behaviour ...
Status semantics for
update():Returning
FAILUREindicates a technical error (e.g. service call failures, exceptions) that may potentially be handled by nodes such asRetry.If not resolved, a
FAILUREwill cause the entire tree to fail.Do not use
FAILUREas an expected logical outcome, just so that nodes likeRetrycan handle those (e.g. when no objects are detected in an image — that is a valid result, not a failure). Instead, indicate such logical outcomes via node outputs (e.g. an empty list of detected objects).
- exception py_trees.ports.NoDataAvailable
Bases:
ExceptionException raised when a required data has not (yet) been written to the port.
- class py_trees.ports.PortInformation(data_type: Any, required: bool = True, description: str = '')
Bases:
objectStatic declaration for one typed input or output port.
- data_type: Any
- description: str = ''
- required: bool = True
- class py_trees.ports.PortsMixin(*args: Any, behaviour_class_name: str | None = None, **kwargs: Any)
Bases:
ABCMixin class for enabling input and output ports on behaviour tree nodes.
This mixin provides the core infrastructure needed to wire, validate, and execute data-driven behaviour tree nodes in a modular and reusable way. It is designed to be used as a base class for behaviours, composites, and decorators in behaviour trees.
A class using
PortsMixinmust:Inherit from
PortsMixinfirst, followed by a concrete py_trees class (e.g.py_trees.behaviour.Behaviour).Define its input and output ports as class-level information by implementing the
@classmethodsinput_portsandoutput_ports.
A
PortsMixinrepresents a modular unit that interacts with input and output data through well-defined ports. These ports are typed and validated at runtime to ensure consistency and facilitate composability between different nodes.Subclasses must define their input and output ports as class-level information by implementing the
@classmethodsinput_portsandoutput_ports.input_ports(cls): returns a dictionary mapping input port names to port information.output_ports(cls): returns a dictionary mapping output port names to port information.
These methods return the expected port definitions for the class and do not change at runtime. These port definitions are used to:
Register blackboard keys for communication.
Enforce type and presence validation at runtime.
Provide clear contracts for each behaviour’s data dependencies and outputs.
Example usage:
class MyBehaviour(PortsMixin, py_trees.behaviour.Behaviour): @classmethod def input_ports(cls): return {"input": PortInformation(data_type=str, required=True)} @classmethod def output_ports(cls): return {"output": PortInformation(data_type=str, required=True)} def __init__(self, name: str): super().__init__(name=name) def update(self): input_val = self.get_input("input") self._set_output("output", f"Processed({input_val})") return py_trees.common.Status.SUCCESS
Port specification format in
input_ports()andoutput_ports():{ "<port_name>": PortInformation(data_type=<expected_type>, required=<bool>), }
Input and output port names must be unique across both sets; overlapping names are not allowed and will raise a
ValueErrorat instantiation.Subtrees
PortsMixinis designed to be used in complex behaviour trees that may consist of multiple subtrees. Each behaviour operates within a specifiedsubtree_namespace, allowing multiple instances of the same behaviour to run in parallel without interfering with each other’s blackboard keys. The namespace ensures logical separation between behaviours and enables modular composition of behaviour trees.Blackboard access
PortsMixinoperates within a scoped subtree namespace to ensure its blackboard keys don’t clash when used in multiple subtrees. Always use theblackboardproperty, as it provides access to the correctly namespaced client. The class abstracts away direct blackboard access in favour ofget_input()and_set_output()methods. Direct access to the blackboard is discouraged and only permitted through theblackboardproperty aftersetup_ports()has been called. However, be aware that the blackboard is shared with all other behaviours in the same subtree, so care must be taken to avoid key collisions.Blackboard namespace strategy
When a port is not explicitly remapped (via XML or constructor arguments), a dedicated storage key is generated, so that sibling nodes with the same port name do not accidentally share data. This “synthesised” key is derived from:
the current subtree namespace,
the node’s name (sanitised to remove characters that py_trees treats as separators),
and the node’s UUID.
Behaviours can continue to share ports by wiring the same absolute key on purpose (e.g.
/shared/output).Port remapping
During setup, ports may be remapped to alternate blackboard keys using the
port_remappingsargument.Port setup lifecycle
setup_ports()is a separate explicit call rather than part ofBehaviour.setup()because port setup requires the full remapping table, which may require parsing the entire tree to compute. The remapping is the “wiring” of ports — connecting one node’s inputs to another node’s outputs — so it presupposes knowledge of the tree topology.Output write semantics
Output ports are written internally by the node itself (typically inside
update()). External callers should not write to output ports in production code; writing from outside is only expected in unit tests where the blackboard is seeded manually.Composites / decorators scope
PortsMixincan be mixed into anyBehavioursubclass — leaves, composites, and decorators. However, this migration does not ship ports-enabled composite or decorator implementations. Those can be contributed in separate follow-up PRs.Example:
class ConsumerProducer(PortsMixin, py_trees.behaviour.Behaviour): @classmethod def input_ports(cls): return {"input": PortInformation(data_type=str, required=True)} @classmethod def output_ports(cls): return {"output": PortInformation(data_type=str, required=True)} def update(self): input_val = self.get_input("input") self._set_output("output", f"Processed({input_val})") return py_trees.common.Status.SUCCESS
- property behaviour_class_name: str
Return the name under which this behavior class is registered.
This is typically the class name (e.g., “CheckGraspStatus”), but can also be an alias used for partial instantiations or custom registrations in a behavior registry.
- Return:
str: The registered name of this behavior class.
- property blackboard_client: Client
Return the scoped blackboard client.
- Raises:
RuntimeError: If setup_ports() has not been called before accessing the blackboard.
- get_input(port_name: str, default: Any = None) Any
Read the value of the given input port from the blackboard.
- Args:
port_name (str): The name of the input port to read from the blackboard. default (Any): Optional default value to return if the port has no input data. If set to None, no default is accepted.
- Return:
Any: The value retrieved from the blackboard for the specified input port or the default.
- Raises:
KeyError: If the input port name is not defined. TypeError: If the retrieved value does not match the expected type. NoDataAvailable: If no data is available on the input port and no default is given.
- get_last_output(port_name: str) Any
Return the last output which the node wrote at this port.
- Args:
port_name (str): The name of the output port to read from the blackboard.
- Return:
Any: The value retrieved from the blackboard for the specified output port.
- Raises:
KeyError: If the input port name is not defined. TypeError: If the value does not match the expected type. NoDataAvailable: If no data has (yet) been written to the output port.
- get_logger() PortsLogger
Return the logger instance.
- Raises:
RuntimeError: If setup_ports() has not been called before accessing the logger.
- classmethod get_port_type(port_name: str) type
Return the declared type for port_name.
- Args:
port_name (str): The name of the input or output port.
- Return:
type: The expected data type of the specified port.
- Raises:
KeyError: If the port name is not defined in either input or output ports.
- abstractmethod classmethod input_ports() dict[str, PortInformation]
Return a mapping of input port names to port information.
- classmethod is_port_required(port_name: str) bool
Return whether the specified port is marked as required.
- Args:
port_name (str): The name of the input or output port.
- Return:
bool: True if the specified port is marked as required, False otherwise.
- Raises:
KeyError: If the port name is not defined in either input or output ports.
- log(level: LogLevel, msg: str, return_only: bool = False, print_name: bool = True) str
Log a message at the specified severity level and update feedback.
- log_debug(msg: str, return_only: bool = False, print_name: bool = True) str
Log msg at DEBUG level (see
log()).
- log_error(msg: str, return_only: bool = False, print_name: bool = True) str
Log msg at ERROR level (see
log()).
- log_info(msg: str, return_only: bool = False, print_name: bool = True) str
Log msg at INFO level (see
log()).
- log_warning(msg: str, return_only: bool = False, print_name: bool = True) str
Log msg at WARNING level (see
log()).
- abstractmethod classmethod output_ports() dict[str, PortInformation]
Return a mapping of output port names to port information.
- reset_all_output_ports() None
Clear all output ports registered on this node.
Keeps the keys registered (READ/WRITE permissions unaffected), but removes the stored values. Intended for use-cases like “new data epoch” where downstream nodes should not read stale outputs.
This will have the effect that subsequent blackboard_client.exists(port_name) returns False again
- Raises:
KeyError: if any port is unknown or not registered.
- reset_port(port_name: str) None
Clear the value stored for a (usually output) port.
Keeps the key registered (READ/WRITE permissions unaffected), but removes the stored value. Intended for use-cases like “new data epoch” where downstream nodes should not read stale outputs.
This will have the effect that subsequent blackboard_client.exists(port_name) returns False again
- Raises:
KeyError: if the port is unknown or not registered.
- setup_ports(port_remappings: dict | None = None, subtree_namespace: str = '/', logger: PortsLogger | None = None) None
Initialize the ports and prepare the blackboard interface.
Registers all declared input and output ports with the blackboard client, optionally applying custom key remappings, and sets the namespace and logger.
This method must be called before using
get_input(),_set_output(), or accessing theblackboardorloggerproperties.Port remapping rules
If a port is not in
port_remappings, its blackboard key is automatically constructed as/{subtree_namespace}/{port_name}.If a remapped key starts with
/, it is treated as an absolute/global key.If a remapped key does not start with
/, it is treated as a key relative to the given namespace, i.e./{subtree_namespace}/{remapped_key}.
General notes
The underlying data store is currently the
py_trees.blackboard, but this is abstracted.However, the API of
PortsMixinabstracts from the concept of a blackboard, so the underlying implementation could change later.Think of a “data key” as a generic handle to some shared data storage (like a key in a map), which can be remapped to match external system requirements.
- Arguments:
port_remappings (dict): Optional dictionary mapping port names to custom blackboard keys. subtree_namespace (str): Namespace to scope the blackboard client (default:
"/"). logger: Optional logger-like object withdebug()/info()/warning()/error()methods.When
None, falls back toself.logger(the native py_trees logger).- Raises:
KeyError: If a port in
port_remappingsis not declared in the port definitions.
This function must be called before using
get_input,_set_output, or accessing theblackboardproperty.
- property subtree_namespace: str
Return the namespace associated with the subtree, used to scope blackboard keys.
- Raises:
RuntimeError: If setup_ports() has not been called before accessing the namespace.