-
Notifications
You must be signed in to change notification settings - Fork 28
Add python bindings for bonsai #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kmolan
wants to merge
25
commits into
main
Choose a base branch
from
feature/python-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
411c97c
create initial scaffolding
kmolan 1256636
add missing items in gitignore
kmolan c50f103
add status enum, and action args
kmolan 13cd7fd
pickle
kmolan b58e318
add tests
kmolan a1523fe
add behaviors
kmolan 7c9ab29
first attempt at bt.rs
kmolan 04c5f28
cleanup
kmolan 6c5a673
add missing tests
kmolan b44f2ee
first attempt at stubs gen
kmolan f79836b
add stub gen to pre commit checks
kmolan fc7fd40
do not leak bytecode
kmolan 0203c6c
add pytest suite, also add to CI
kmolan 38c5322
IT TOOK ME SO MANY HOURS, BUT IT FINALY WORKS
kmolan 2ddc0c3
generate python wheels action
kmolan 81e5505
pipeline fixes
kmolan 1fcca8f
more pipeline fixes
kmolan 6f50ffe
pr fixes
kmolan bf07d92
more fixes
kmolan 7328a58
async drone
kmolan 7d3ff05
update email
kmolan c3deb8d
refactor tests to remove duplicates
kmolan 41259b1
update readme
kmolan 596ec76
rename to bonsai-bt
kmolan 0baad28
fix error
kmolan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| name: Python wheels | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| tags: ["py-v*", "py-test-*"] | ||
| pull_request: | ||
| branches: [main] | ||
| workflow_dispatch: # manual ad-hoc builds from any branch | ||
|
|
||
| concurrency: | ||
| # Tag pushes get their own group so publishes never get cancelled. | ||
| group: >- | ||
| ${{ github.workflow }}-${{ github.ref }}-${{ startsWith(github.ref, 'refs/tags/') && 'publish' || 'branch' }} | ||
| cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/') }} | ||
|
|
||
| env: | ||
| CARGO_TERM_COLOR: always | ||
|
|
||
| jobs: | ||
| build: | ||
| name: Build wheel (${{ matrix.target.label }}) | ||
| runs-on: ${{ matrix.target.runner }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| target: | ||
| # `manylinux: "2_28"` makes maturin-action run the build inside the | ||
| # official PyPA manylinux_2_28 container (Rocky Linux 8 / glibc 2.28). | ||
| # Without this, the build runs on the host (Ubuntu glibc 2.39) and | ||
| # produces a wheel that fails the auditwheel manylinux_2_28 check. | ||
| - label: linux-x86_64 | ||
| runner: ubuntu-latest | ||
| target: x86_64-unknown-linux-gnu | ||
| manylinux: "2_28" | ||
| - label: macos-universal2 | ||
| runner: macos-latest | ||
| target: universal2-apple-darwin | ||
| manylinux: "auto" | ||
| - label: windows-x86_64 | ||
| runner: windows-latest | ||
| target: x86_64-pc-windows-msvc | ||
| manylinux: "auto" | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.10" # abi3 — any 3.10+ works for building | ||
|
|
||
| - uses: dtolnay/rust-toolchain@stable | ||
| with: | ||
| targets: ${{ matrix.target.label == 'macos-universal2' && 'x86_64-apple-darwin,aarch64-apple-darwin' || '' }} | ||
|
|
||
| - uses: Swatinem/rust-cache@v2 | ||
| with: | ||
| workspaces: bonsai-py | ||
| key: ${{ matrix.target.label }} | ||
|
|
||
| - name: Tag/version guard (tag pushes only) | ||
| if: startsWith(github.ref, 'refs/tags/') | ||
| shell: bash | ||
| run: | | ||
| tag="${GITHUB_REF#refs/tags/}" | ||
| version="${tag#py-v}" | ||
| version="${version#py-test-}" | ||
| cargo_version=$(grep -m1 '^version' bonsai-py/Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/') | ||
| if [ "$version" != "$cargo_version" ]; then | ||
| echo "::error::Tag version '$version' does not match Cargo.toml version '$cargo_version'." | ||
| exit 1 | ||
| fi | ||
|
|
||
| - uses: PyO3/maturin-action@v1 | ||
| with: | ||
| working-directory: bonsai-py | ||
| command: build | ||
| target: ${{ matrix.target.target }} | ||
| manylinux: ${{ matrix.target.manylinux }} | ||
| args: --release --out dist --strip | ||
|
|
||
| - name: Verify wheel (Linux/macOS only — Windows venv quirks) | ||
| if: matrix.target.runner != 'windows-latest' | ||
| shell: bash | ||
| run: | | ||
| python -m venv .venv-test | ||
| source .venv-test/bin/activate | ||
| pip install --upgrade pip | ||
| pip install pytest pytest-timeout mypy | ||
| pip install bonsai-py/dist/*.whl | ||
| python -c "import bonsai_bt; print(bonsai_bt.__version__)" | ||
| pytest bonsai-py/tests/ | ||
|
|
||
| - name: Wheel size sanity (Linux/macOS only) | ||
| if: matrix.target.runner != 'windows-latest' | ||
| shell: bash | ||
| run: | | ||
| size=$(stat -c%s bonsai-py/dist/*.whl 2>/dev/null || stat -f%z bonsai-py/dist/*.whl) | ||
| ceiling=$((5 * 1024 * 1024)) | ||
| if [ "$size" -gt "$ceiling" ]; then | ||
| echo "::error::Wheel size $size exceeds 5MB ceiling." | ||
| exit 1 | ||
| fi | ||
| echo "Wheel size: $size bytes (under 5MB ceiling)." | ||
|
|
||
| - uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: wheel-${{ matrix.target.label }} | ||
| path: bonsai-py/dist/*.whl | ||
| retention-days: ${{ startsWith(github.ref, 'refs/tags/') && 90 || 14 }} | ||
|
|
||
| sdist: | ||
| name: Build sdist | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.10" | ||
|
kmolan marked this conversation as resolved.
|
||
| - uses: PyO3/maturin-action@v1 | ||
| with: | ||
| working-directory: bonsai-py | ||
| command: sdist | ||
| args: --out dist | ||
| - uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: sdist | ||
| path: bonsai-py/dist/*.tar.gz | ||
| retention-days: ${{ startsWith(github.ref, 'refs/tags/') && 90 || 14 }} | ||
|
|
||
| verify-sdist: | ||
| name: Verify sdist builds from source | ||
| needs: sdist | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.10" | ||
| - uses: dtolnay/rust-toolchain@stable | ||
| - uses: actions/download-artifact@v4 | ||
| with: | ||
| name: sdist | ||
| path: dist | ||
| - name: Install + smoke-test from sdist | ||
| run: | | ||
| python -m venv .venv-sdist | ||
| source .venv-sdist/bin/activate | ||
| pip install --upgrade pip | ||
| pip install --no-binary :all: dist/*.tar.gz | ||
| python -c "import bonsai_bt; print(bonsai_bt.__version__)" | ||
|
|
||
| publish: | ||
| name: Publish to PyPI / TestPyPI | ||
| needs: [build, sdist, verify-sdist] | ||
| if: startsWith(github.ref, 'refs/tags/py-v') || startsWith(github.ref, 'refs/tags/py-test-') | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: ${{ startsWith(github.ref, 'refs/tags/py-test-') && 'testpypi' || 'pypi' }} | ||
| permissions: | ||
| id-token: write # OIDC for Trusted Publishing | ||
| steps: | ||
| - uses: actions/download-artifact@v4 | ||
| with: | ||
| path: dist | ||
| pattern: wheel-* | ||
| merge-multiple: true | ||
|
|
||
| - uses: actions/download-artifact@v4 | ||
| with: | ||
| name: sdist | ||
| path: dist | ||
|
|
||
| - name: List artifacts to publish | ||
| run: ls -la dist/ | ||
|
|
||
| - uses: pypa/gh-action-pypi-publish@release/v1 | ||
| with: | ||
| repository-url: >- | ||
| ${{ startsWith(github.ref, 'refs/tags/py-test-') && 'https://test.pypi.org/legacy/' || 'https://upload.pypi.org/legacy/' }} | ||
| packages-dir: dist | ||
| skip-existing: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| [workspace] | ||
| resolver = "2" | ||
| members = ["bonsai", "examples"] | ||
| members = ["bonsai", "examples", "bonsai-py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [package] | ||
| name = "bonsai-py" | ||
| version = "0.12.0" | ||
| edition = "2021" | ||
| rust-version = "1.80.0" | ||
| description = "Python bindings for the bonsai-bt behavior tree library" | ||
| license = "MIT" | ||
| authors = ["Kristoffer Solberg Rakstad <solkristoffer@gmail.com>"] | ||
| repository = "https://github.com/sollimann/bonsai.git" | ||
| homepage = "https://github.com/sollimann/bonsai" | ||
| publish = false | ||
|
|
||
| [lib] | ||
| # Internal Rust crate name — kept as `bonsai_py` to avoid colliding with the | ||
| # workspace's `bonsai-bt` crate at `bonsai/`, which also produces `libbonsai_bt.rlib`. | ||
| # The Python-facing module name is controlled separately by | ||
| # `[tool.maturin] module-name = "bonsai_bt"` in pyproject.toml. | ||
| name = "bonsai_py" | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [[bin]] | ||
| name = "stub_gen" | ||
| path = "src/bin/stub_gen.rs" | ||
|
|
||
| [dependencies] | ||
| # Note: `extension-module` is enabled by maturin via pyproject.toml's | ||
| # `[tool.maturin].features` setting. Keeping it out of the default feature | ||
| # list lets `cargo run --bin stub_gen` link libpython for the regular binary | ||
| # path; maturin still activates it for the wheel build. | ||
| pyo3 = { version = "0.28", features = ["abi3-py310"] } | ||
| bonsai-bt = { path = "../bonsai", version = "0.12", features = ["visualize"] } | ||
| pyo3-stub-gen = "0.22.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # bonsai-bt - Python bindings | ||
|
|
||
| Python bindings for the [bonsai-bt](https://github.com/sollimann/bonsai) | ||
| behavior-tree library. | ||
|
|
||
| ## Installation (dev) | ||
|
|
||
| ```bash | ||
| python -m venv .venv | ||
| source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1 | ||
| pip install maturin | ||
| cd bonsai-py | ||
| maturin develop | ||
| python -c "import bonsai_bt; print(bonsai_bt.__version__)" | ||
| ``` | ||
|
|
||
| ## Same BT in Rust and Python | ||
|
|
||
| A minimal three-node tree (`Hello → Wait(1.0) → Goodbye`) implemented in both languages. Semantics are identical because the Python package is a thin wrapper around the Rust crate; only the API surface differs (Rust requires an `enum` + explicit types; Python uses any hashable object as the action payload). | ||
|
|
||
| ### Rust | ||
|
|
||
| ```rust | ||
| use bonsai_bt::{Behavior, Event, Status, UpdateArgs, BT}; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| enum Greet { Hello, Goodbye } | ||
|
|
||
| fn main() { | ||
| let tree = Behavior::Sequence(vec![ | ||
| Behavior::Action(Greet::Hello), | ||
| Behavior::Wait(1.0), | ||
| Behavior::Action(Greet::Goodbye), | ||
| ]); | ||
|
|
||
| let mut bt: BT<Greet, ()> = BT::new(tree, ()); | ||
|
|
||
| for _ in 0..5 { | ||
| let e: Event = UpdateArgs { dt: 0.5 }.into(); | ||
| bt.tick(&e, &mut |args, _bb| { | ||
| match *args.action { | ||
| Greet::Hello => println!("hello"), | ||
| Greet::Goodbye => println!("goodbye"), | ||
| } | ||
| (Status::Success, args.dt) | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Python | ||
|
|
||
| ```python | ||
| import bonsai_bt as bt | ||
|
|
||
| tree = bt.Sequence([ | ||
| bt.Action("hello"), | ||
| bt.Wait(1.0), | ||
| bt.Action("goodbye"), | ||
| ]) | ||
|
|
||
| tree_bt = bt.BT(tree, None) | ||
|
|
||
| def cb(args, _bb): | ||
| print(args.action) | ||
| return (bt.Status.Success, args.dt) | ||
|
|
||
| for _ in range(5): | ||
| tree_bt.tick(0.5, cb) | ||
| ``` | ||
|
|
||
| Output (both): | ||
|
|
||
| hello | ||
| goodbye | ||
|
|
||
| For richer examples — multi-job orchestration, visualizer integration, parallel agents — see [examples/](examples/). | ||
|
|
||
| ## License | ||
|
|
||
| MIT - see [LICENSE](../LICENSE). |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.