RepositoryRustNONorway
Sollimann

Sollimann/bonsai

Rust implementation of behavior trees for deterministic AI (now with Python bindings)

1.1kstars
42forks
8issues
5views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: autonomous-robots

#1 Repository of the week

1
TINIX TRENDING·Topic: behavior-tree

#1 Repository of the week

1
TINIX TRENDING·Topic: piston

#1 Repository of the week

Bonsai 盆栽

Rust implementation of Behavior Trees

Build Status Bonsai crate PyPI minimum rustc 1.72 Docs codecov Maintenance GitHub pull-requests GitHub pull-requests closed ViewCount License: MIT

Contents

Using Bonsai

Rust

Bonsai is available on crates.io. The recommended way to use it is to add a line into your Cargo.toml such as:

[dependencies]
bonsai-bt = "*"

Python

Bonsai is available on PyPI as bonsai-bt:

pip install bonsai-bt

See bonsai-py/ for examples.

Example of use

See Examples folder.

What is a Behavior Tree?

A Behavior Tree (BT) is a data structure in which we can set the rules of how certain behavior's can occur, and the order in which they would execute. BTs are a very efficient way of creating complex systems that are both modular and reactive. These properties are crucial in many applications, which has led to the spread of BT from computer game programming to many branches of AI and Robotics.

How to use a Behavior tree?

A Behavior Tree forms a tree structure where each node represents a process. When the process terminates, it signals Success or Failure. This can then be used by the parent node to select the next process. A signal Running is used to tell the process is not done yet.

For example, if you have a state A and a state B:

  • Move from state A to state B if A succeeds: Sequence([A, B])
  • Try A first and then try B if A fails: Select([A, B])
  • If condition succeedes do A, else do B : If(condition, A, B)
  • If A succeeds, return failure (and vice-versa): Invert(A)
  • Do A, B repeatedly while LoopCondition runs: While(LoopCondition, [A, B]). Checks condition node between nodes A, B.
  • Do A, B forever: While(WaitForever, [A, B])
  • Do A, B repeatedly while LoopCondition runs: WhileAll(LoopCondition, [A, B]). After All nodes A, B are completed successfully, check the condition node.
  • Run A and B in parallell and wait for both to succeed: WhenAll([A, B])
  • Run A and B in parallell and wait for any to succeed: WhenAny([A, B])
  • Run A and B in parallell and wait for any to complete regardless of success or failure: Race([A, B])
  • Run A and B in parallell, but A has to succeed before B: After([A, B])

See the Behavior enum for more information.

Calling long-running tasks in behavior tree

To make sure that the behavior tree is always responsive, it is important that the actions that are created executes instantly so that they do not block the tree traversal. If you have long-running tasks/functions that can take seconds or minutes to execute - either async or sync - then we can dispatch those jobs into background threads, and get status of the task through a channel.

see async drone example in the /examples folder for more details.

Similar Projects

Discover other similar projects and AI models.

RepositoryC++
recastnavigation/recastnavigation

Industry-standard navigation-mesh toolset for games

8.3k
Feb 27, 2026
RepositoryRust
bevyengine/bevy

A refreshingly simple data-driven game engine built in Rust

50.1k
Nov 29, 2026
RepositoryJavaScript
4ian/GDevelop

🎮 Open-source, cross-platform 2D/3D/multiplayer game engine designed for everyone.

29.5k
Dec 1, 2026
RepositoryRust
kitao/pyxel

A retro game engine for Python

18.7k
Nov 28, 2026
RepositoryC++
bulletphysics/bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

14.9k
Oct 9, 2026
RepositoryGo
hajimehoshi/ebiten

A dead simple 2D game engine for Go

13.7k
Nov 30, 2026
RepositoryDart
flame-engine/flame

A Flutter based game engine.

10.8k
Dec 2, 2026
RepositoryC++
endless-sky/endless-sky

Space exploration, trading, and combat game.

7.8k
Nov 29, 2026
RepositoryJava
00-Evan/shattered-pixel-dungeon

Shattered Pixel Dungeon is an open-source traditional roguelike dungeon crawler with randomized levels and enemies, and hundreds of items to collect and use. It's based on the source code of Pixel Dungeon, by Watabou.

6.9k
Nov 25, 2026