I’d like to integrate VTK/Python into my own project, which uses a mix of C++ and Python code.
I need to call functions that take vtkSmartPointer arguments with object references I have extracted from VTK/Python wrappers.
While I understand how to access raw VTK pointers from PyObject pointers by querying the __this__ attribute, I don’t quite understand how the reference counting is to be done for such pointers. Consider the following example. Given these two functions:
I can now write my own C++ Python extension and pass VTK/Python arguments to it. But who owns the objects that are passed around that way ? How can I make sure that the references I store will remain valid, even if no-one else is referencing these objects ?
I found some mention of vtkSmartPointer in the context of Boost.Python wrappers, but not enough details to help me answer the above question.
Any help is much appreciated !
VTK objects are self-refcounting (intrusively counted). So all objects are basically shared ownership. The vtkSmartPointer is an RAII object which manages the refcount nicely in C++. You can just create one from an existing object safely.
I tried to wrap an extremely simple C++ function for Python but could never make it work.
Since you mentioned
“I can now write my own C++ Python extension and pass VTK/Python arguments to it”
I believe you have much more experience on this. Thanks.
Btw, I found your question while edited my question, the system recommended that to me.