# vtk pybinding problem

**URL:** https://discourse.vtk.org/t/vtk-pybinding-problem/13943
**Category:** Development
**Tags:** python
**Created:** [May 27, 2024, 7:04am UTC](https://discourse.vtk.org/t/vtk-pybinding-problem/13943 "2024-05-27T07:04:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dsa934](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dsa934/32/8904_2.png) [@dsa934](https://discourse.vtk.org/u/dsa934)
#### Post date: [May 27, 2024, 7:04am UTC](https://discourse.vtk.org/t/vtk-pybinding-problem/13943/1 "2024-05-27T07:04:33Z")

</div>

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\>**

```auto
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](https://gitlab.kitware.com/cmb/smtk/-/blob/master/smtk/extension/vtk/pybind11/PybindVTKTypeCaster.h) )

```auto
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.

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dcthomp/32/60_2.png) [@dcthomp](https://discourse.vtk.org/u/dcthomp)
#### Post date: [May 28, 2024, 2:31pm UTC](https://discourse.vtk.org/t/vtk-pybinding-problem/13943/2 "2024-05-28T14:31:10Z")

</div>

@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](https://smtk.readthedocs.io), not VTK; if you are trying to use SMTK’s VTK support, it might be better to ask on the [SMTK discourse site](https://discourse.kitware.com/c/smtk/) 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.

---

<div class="post-metadata">

### Author: ![dsa934](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dsa934/32/8904_2.png) [@dsa934](https://discourse.vtk.org/u/dsa934)
#### Post date: [May 28, 2024, 5:01pm UTC](https://discourse.vtk.org/t/vtk-pybinding-problem/13943/3 "2024-05-28T17:01:28Z")

</div>

@dcthomp

> [@dcthomp](#):
>
> Or are you trying to mix VTK’s python objects with some other code?

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.

```auto
// 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](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.
