Skip to content

pybind type_casters might be able to be simplified + genericized?

A colleague (Duy Nguyen) and myself were interested in looking at VTK + pybind11 interop.

Someone else ended up posting a StackOverflow within 24 hours of our interest, and that user pointed to this code base as a starting point, but ran into a snag when trying to use it: https://stackoverflow.com/questions/54871216/pybind11-return-c-class-with-an-existing-python-binding-to-python

I posted this solution: https://stackoverflow.com/a/54905431/7829525

The takeaways:

  • Using macros to instantiate the type_caster specializations can be replaced by generic templates + SFINAE. (Should be C++11-compatbile, but I, uh, didn't test it fully)
  • VTK's Python utilities seems to (magically?) handle ownership sharing well, so adding type_casters for vtkSmartPointer and vtkNew is relatively simple - would be nice to incorporate here or elsewhere as a Kitware recipe.

Perhaps some form of the code I rewrote could be incorporated here, so that future people looking into this code base have a simpler implementation to work off of?

I'll also see if I can link this Wiki article to the StackOverflow post and the SMTK code, as either of those seem better than what the article mentioned too? https://vtk.org/Wiki/Example_from_and_to_python_converters