Skip to main content

Python package for modelling and executing Behaviour Trees.

Project description

Behaviour Tree Build Status

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-bt-1.2.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

py_bt-1.2.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file py-bt-1.2.1.tar.gz.

File metadata

  • Download URL: py-bt-1.2.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for py-bt-1.2.1.tar.gz
Algorithm Hash digest
SHA256 58bf9b7915ea6d9f6719ea07127257fb429f3669a1d3b1fad92f6755bb8b6bfe
MD5 4d063bea3cb3973835200f8bb3885ad3
BLAKE2b-256 76fc7304238a39f7c7d222928554dc6f95ea98b715bc5565727c8cd22995d586

See more details on using hashes here.

File details

Details for the file py_bt-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: py_bt-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for py_bt-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 197d3f965fc6811ff9f51b0c1971edab66cb31025697b31ddfd0191b43a33d46
MD5 b3b672bf283e90928aca7320fc6dafd0
BLAKE2b-256 479a60742dcf51cfe32935a27fefc72ef2c160617f2aea2233f26c30d0372abb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page