Behaviour Tree 
PyPi: https://pypi.org/project/py-bt/
Tree structure made up of Composite, Decorator and Leaf nodes.
Leaf nodes are where the behaviour happens, for example an "action" or "test".
Decorator nodes can only have one child and are used to wrap the result of this child node (e.g. "retry" or "not").
Composite nodes can have multiple children. Composite nodes can be of 2 types:
- Sequence
- Selector
Sequence nodes return the first failed child node. Similar to the ALL operator.
Selector nodes return the first successful child node. Similar to the OR operator.
Release History
-
1.2.1: Added tree model validation
-
1.2.0: Added the "retry" decorator node
-
1.1.0: Added the "not" decorator node (inverter)
Dependencies
Tested on:
- Python 3.8.1
- Python 3.7.5
Usage
Install:
pip install py-bt
Define a python module for you behaviour tasks (actions & tests). Tasks must return True or False
depending on whether they succeed or fail. Tasks functions receive data and blackboard args.
data is the input data to the tree execution and blackboard is a key value store where
information can be shared between nodes. For example:
def choose_food(data, blackboard):
if data["lunchbox"]:
blackboard["choice"] = random.choice(data["lunchbox"])
return True
return False
def eat(data, blackboard):
print(f"Eating {blackboard['choice']}")
return True
Then, define your desired tree model in JSON or YAML format. For example:
{
"tasks_path": "path.to.tasks.module",
"tree": {
"sequence": [
{
"task": "choose_food"
},
{
"task": "eat"
}
]
}
}
Then initialise and execute a behaviour tree object with some input data:
from bt.behaviour_tree import BehaviourTree
data = {
"lunchbox": ["apple", "orange", "pear"]
}
tree = BehaviourTree("/path/to/tree/model.json")
tree.load()
tree.execute(data)
Example Models
Some example models can be found under the /models directory.
For example /models/football/attacker.json contains a behaviour tree for how a attacking player in a
football simulator might behave.
Tests
Run tests in local Python environment (use a virtualenv):
pip install -r requirements.txt -r requirements-test.txt
pytest
flake8
Run tests with tox:
pip install tox==3.14.5
tox
Upload to PyPi
rm -rf dist/
python3 setup.py sdist bdist_wheel
twine upload dist/*
References
Release files for py-bt 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py-bt-1.2.1.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_bt-1.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / py-bt-1.2.1.tar.gz
| Download URL | py-bt-1.2.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
58bf9b7915ea6d9f6719ea07127257fb429f3669a1d3b1fad92f6755bb8b6bfe
|
|
BLAKE2b-256 checksum How to use checksums |
76fc7304238a39f7c7d222928554dc6f95ea98b715bc5565727c8cd22995d586
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5
|
Release files / py_bt-1.2.1-py3-none-any.whl
| Download URL | py_bt-1.2.1-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
197d3f965fc6811ff9f51b0c1971edab66cb31025697b31ddfd0191b43a33d46
|
|
BLAKE2b-256 checksum How to use checksums |
479a60742dcf51cfe32935a27fefc72ef2c160617f2aea2233f26c30d0372abb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5
|