Passing python vtk_ptr to C++ Failed

Hello Team,

I tried to pass the py_vtk_ptr to cpp_vtk for modifiying the value rapidly.
I used the method from pletzer/vtkpythoncpp, like
addr = int(pdata.GetAddressAsString(‘vtkPolyData’)[5:], 16)

however, the first problem is, the address was clipped by “long”,
python:Addr=00000261BF823AE0->2618589177776(int, 16)

thus, I changed c_long to c_longlong, and the cpp function get the right pointer, but

when I called ptr->GetNumberofPoints(), it meets the error code: OSError: exception: access violation reading 0x0000000000000038.

I am using vtk 8.1(c++13), vtk-9.0(python 3.7), is that the key point?
(I tried vtk-8.1 for python, it did not work too)
Or in new version, there are other solution for that problems?

Thank you!

I think you want ctypes.c_void_p. On Windows, c_long is only 32bits which is where the truncation comes from. That said, I don’t know that this is a supported way to do such things. @dgobbi?

I agree, use ctypes.c_void_p, since it’s the one designed for use with pointers of any type.

Either pdata.GetAddressAsString('vtkPolyData') or pdata.__this__ will return a valid pointer for an object for any version of VTK. The GetAddressAsString() method is used in, which I have tested recently.

Edit: here is a comparison with the pointer shown by PrintSelf():

Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from vtkmodules.vtkCommonCore import vtkVersion
>>> from vtkmodules.vtkCommonDataModel import vtkPolyData
>>> vtkVersion.GetVTKVersion()
>>> o = vtkPolyData()
>>> o.GetAddressAsString('vtkPolyData')
>>> o.__this__
>>> print(o)
vtkPolyData (0xbb5730)
  Debug: Off
  Modified Time: 28
  Reference Count: 1

Sorry, but it failed again. I tried c_void_p in python, and fun(vtkPolyData* ptr) and fun(void* ptr) in c++.
This is the error code:
OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF

Logs before crash:
python logs: addr : 0000022506C9C470
C++ logs: 0000022506C9C470->0000022506C9C470

According to the logs, the c++ get the same ptr as python, but when I call it, it crashes.

How are you obtaining the VTK Python module and how is lib compiled against that same VTK (if it isn’t, this is not going to work)?

I complied the vtk-8.2 using Cmake+vs2017+Qt5 with default setting.
python-vtk is installed by Pycharm-setting(maybe it is same as “pip install vtk-8.2”)

Yeah, those two builds are highly unlikely to be compatible with each other. You’ll need to compile the Python wrapping for the build that everything else is using because the pip install vtk-8.2 comes with its own build of VTK. VTK_WRAP_PYTHON is the CMake flag that should work for 8.2 as well.