Hi, VTK users
I would like to ask a question because a problem occurred during the process of pybinding VTK.
Problem: Building pybind.cxx on the C++ side was successful. However, if you call the vtk function with pybinding on the python side, the type becomes None.
error >
TypeError: get_poly_ptr(): incompatible function arguments. The following argument types are supported:
- (self: pybind_test.CppOwned) → vtkPolyData
Invoked with:
(invoked with NOTHING)
code below>
pybind.cxx
// other stuff..
...
#include "vtkDriectBinding.h"
using rvp = py::return_value_policy;
PYBIND11_MODULE(pybind_test, m) {
//vtk poly data
//Use a class to control the lifetime of a VTK object.
py::class_<CppOwned>(m, "CppOwned")
.def(py::init())
.def_readonly("poly", &CppOwned::poly)
.def("get_poly_ptr", &CppOwned::get_poly_ptr,rvp::reference)
.def("get_poly_cptr", &CppOwned::get_poly_cptr,rvp::reference)
.def("get_poly_ref", &CppOwned::get_poly_ref, rvp::reference)
.def("get_poly_cref", &CppOwned::get_poly_cref, rvp::reference);
// test
m.def("get_poly_data", &test::get_poly_data);
using @dcthomp 's code in vtkDrectBinding.h( ref : https://gitlab.kitware.com/cmb/smtk/-/blob/master/smtk/extension/vtk/pybind11/PybindVTKTypeCaster.h )
bind_test.py
import pybind_test as py_test
if __name__ == "__main__":
path = r"path/to/mesh/data"
poly_mesh = py_test.get_poly_data(path)
print(poly_mesh) # return None , why? path's correct
module = py_test.CppOwned
print(module.poly) # return <property object at 0x00000207DDC277E0>
poly_data_ptr = module.get_poly_ptr()
if poly_data_ptr is not None:
print("VTK object loaded successfully")
else:
print("Failed to load VTK object")
Does anyone know a solution to this problem?
I’m currently using version VTK 9.3. The build was successful, but an error occurred when running, so I don’t know what to do.