Skip to content

Replace Boost.Python with pybind11 - #176

Open
CalaW wants to merge 12 commits into
aewallin:masterfrom
CalaW:pybind11
Open

Replace Boost.Python with pybind11#176
CalaW wants to merge 12 commits into
aewallin:masterfrom
CalaW:pybind11

Conversation

@CalaW

@CalaW CalaW commented Apr 19, 2025

Copy link
Copy Markdown

This PR replaces Boost.Python with pybind11, which has become the de facto standard for Python bindings in C++.

Key improvements:

  • Simplified binding code, thanks to pybind11’s automatic handling of standard containers.
  • Easier build process, as pybind11 is a lightweight, header-only library.
  • Additionally, this PR includes fixes for existing CI errors. I'm happy to split those into a separate PR if preferred.

Note: This is still a draft. Documentation updates are pending. Please review the current changes and share your feedback. Once everyone is on board, I’ll proceed with updating the docs.

This should resolve #164.

@CalaW
CalaW marked this pull request as ready for review April 25, 2025 10:02
@CalaW

CalaW commented May 24, 2025

Copy link
Copy Markdown
Author

Hi @aewallin @vespakoen, just following up on this PR. If either of you happens to have some spare time, would you mind giving it a look or testing it out? I’d be happy to make any necessary changes. Thanks in advance!

@SpectralVectors

Copy link
Copy Markdown

Hi @CalaW - not sure what you needed from this lib, but I built a set of wheels from your branch for Python 3.13 and uploaded them here if you want them: https://github.com/SpectralVectors/opencamlib/releases/tag/Python313_Wheels
Thanks!

@vespakoen

Copy link
Copy Markdown
Collaborator

Cool! I will take a look at this next week, I hope to also upgrade Boost to the modern version which should remove a lot of the install script.
I think we still use boost for other stuff, right?
Anyways, i'll take a look soon, thanks for all this effort!

@CalaW

CalaW commented Jul 16, 2026

Copy link
Copy Markdown
Author

Hi @vespakoen, Thanks for taking a look!

Yes, Boost is still required. This PR only replaces Boost.Python with pybind11. We still use Boost.Graph, for example boost/graph/adjacency_list.hpp.

I have already replaced some uses of BOOST_FOREACH, boost::tie, and Boost.Math with the standard library. I can also make another PR to replace the remaining ones.

@vespakoen

Copy link
Copy Markdown
Collaborator

Removing boost completely would be great, it saves a lot of hassle with their somewhat weird build system.

Boost did gain some better compatibility with CMake though, which might help.
And Boost.Python specifically, was a pain to cross compile, so this will already help a lot!

Maybe getting rid of Boost completely should be a follow up PR, I'd focus on getting this to work on all the platforms.

Will look into this soon...

@vespakoen

Copy link
Copy Markdown
Collaborator

Did you also consider nanobind? (https://github.com/wjakob/nanobind) it seems to be very similar in syntax so might be worth a try?

@CalaW

CalaW commented Jul 19, 2026

Copy link
Copy Markdown
Author

Yes, I did consider nanobind. At that time, it was still relatively new, while pybind11 was already the de facto standard and had a much larger user base (3.4k vs 18k star).

From what I have seen, nanobind does have some advantages, especially smaller binaries and lower binding overhead. However, for ocl, I believe most of the heavy computation happens in C++, so I don’t expect the Python binding overhead to be a bottleneck.

@vespakoen

vespakoen commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

I opened a pull request to your fork, If that lands I think I am fine with merging this.
Do you think it's better to do a major release since we changed the binding generation from Boost.Python to pybind11? I guess the API actually stayed the same?
I think it might be a good signal to package maintainers that something bigger changed, since the packagers will have to swap out Boost.Python for pybind11 as well...

Let me know what you all think ;)

EDIT: Just saw that the README also needs some updating

@CalaW

CalaW commented Jul 28, 2026

Copy link
Copy Markdown
Author

Thanks! I will check your PR and update the README as well.

I am not fully sure that the Python API is completely stable, because the project currently does not have a complete test framework for checking API compatibility. I run all python examples and test file and they looked fine. I tried to keep the exposed classes, functions, and method names the same, but there may still be some behavior differences.

For example, AdaptiveWaterline.getXFibers, behaves differently in the old Boost.Python bindings. The old binding drops all empty elements before returning the list:

/// return a list of xfibers to python
boost::python::list getXFibers() const {
boost::python::list flist;
BOOST_FOREACH( Fiber f, xfibers ) {
if (!f.empty()) {
Fiber_py f2(f);
flist.append(f2);
}
}
return flist;
}

However, the underlying C++ implementation inherits the behavior from Waterline, which returns the complete list:

boost::python::list py_getXFibers() const {
boost::python::list flist;
std::vector<Fiber> xfibers = *( subOp[0]->getFibers() );
BOOST_FOREACH( Fiber f, xfibers ) {
Fiber_py f2(f);
flist.append(f2);
}
return flist;
}

In the pybind11 version, I preserved the behavior of the C++ implementation instead but it's still easy to modify.

https://github.com/CalaW/opencamlib/blob/5470a8977e2b0540a863c61a3c7b038edfdcf61a/src/algo/waterline.hpp#L54-L55

Speaking of versioning, I noticed that the current versioning seems to be date-based, with the latest release being 2023.01.11, rather than semantic versioning. In that case, maybe we can make a new dated release and clearly mention that it contains a major binding and packaging change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support python3.12, Blender 4.0.2 uses python3.12

3 participants