vtk pybinding problem

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:

  1. (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.

@dsa934 Are you trying to wrap VTK with python bindings? VTK has its own code to generate (non-pybind11) bindings. Or are you trying to mix VTK’s python objects with some other code?

The code you reference is from SMTK, not VTK; if you are trying to use SMTK’s VTK support, it might be better to ask on the SMTK discourse site instead of here.

The code you’ve provided doesn’t have enough information for me to understand the problem. Having the error message or a stack trace would also be helpful.

@dcthomp

I am implementing functions using VTK in C++ and trying binding to use these functions in Python.

As you know, if the ‘dataType’ used on the C++ side is not known on the python side, you need to do binding for ‘dataType’.

However, I have a problem when binding a function that uses the VTK dataType as the return type or argument of the function.

// unitFunctionBind.cpp
namespace Test{
vtkSmartPointer<vtkPolyData> get_poly_mesh(const std::filesystem::path &path);
}

// in pybind.cxx
PYBIND11_MODULE(bind_test, m) 
{
  m.def("get_poly_data", &Test::get_poly_mesh);
}

There is no problem building the code structured as above on the C++ side, but when running it on the python side, the data type related to VTK cannot be read.

Didn’t you bind the vtk type using the ‘cppOwned’ structure? (https://gitlab.kitware.com/cmb/smtk/-/blob/master/smtk/extension/vtk/pybind11/PybindVTKTypeCaster.h)

Until now, I have not been able to bind the VTK dataType directly, so I once again wrapped it with a data type known on the Python side.

In conclusion, I am curious about a way to directly bind VTK dataType. Thanks for reading.