# Context sharing between C/C++ and Python

**URL:** https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998
**Category:** Support
**Created:** [January 14, 2021, 9:29am UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998 "2021-01-14T09:29:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [January 14, 2021, 9:29am UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/1 "2021-01-14T09:29:29Z")

</div>

Dear All,

Suppose that I displayed a 3D image with VTK in Python and I want to manipulate it with C/C++ (Rotate, pick a point etc). How I share VTK context between C/C++ and Python?

Thanks,

Luís Gonçalves

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 14, 2021, 3:17pm UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/2 "2021-01-14T15:17:50Z")

</div>

Assuming that the Python interpreter is embedded in your running C++ process, and thus shares the same address space, you can use the “\_this\_” attribute to get the address (in hexadecimal) of a wrapped vtkObject:

```python
>>> window = vtk.vtkRenderWindow()
>>> window. __this__'_000055ab9b5b50f0_p_vtkXOpenGLRenderWindow'

```

The format is “\_” + address + “\_p\_” + type, where the “p” simply signifies “pointer”.

Another approach, from the C++ end, is to use the [Python wrapper API](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Wrapping/PythonCore/vtkPythonUtil.h):

```auto
vtkObjectBase* vtkPythonUtil::GetPointerFromObject(PyObject* obj, const char* obj_type)

```

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [June 15, 2021, 2:03pm UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/3 "2021-06-15T14:03:14Z")

</div>

There is some way to call a C/C++ program from Python (Paraview Python) with share of the variable space? (another way than system call)

Are the following approaches valid for Paraview Python?

> **[Python Bindings: Calling C or C++ From Python – Real Python](https://realpython.com/python-bindings-overview/)**
>
> What are Python bindings? Should you use ctypes, CFFI, or a different tool? In this step-by-step tutorial, you'll get an overview of some of the options you can use to call C or C++ code from Python.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 15, 2021, 2:30pm UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/4 "2021-06-15T14:30:43Z")

</div>

This is a large and complicated topic. You’re best bet is to read and experiment with different possible solutions.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [June 15, 2021, 4:56pm UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/5 "2021-06-15T16:56:12Z")

</div>

Cython is not supported in Paraview Python. Also not cffi.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [June 18, 2021, 8:58am UTC](https://discourse.vtk.org/t/context-sharing-between-c-c-and-python/4998/6 "2021-06-18T08:58:20Z")

</div>

Ctypes works and with CUDA.

> **[Call C and CUDA from Python (Paraview Python)](https://forums.developer.nvidia.com/t/call-c-and-cuda-from-python-paraview-python/180985)**
>
> Dear All, I have not certain if it is the right topic to post. I want to call C (with CUDA) from Paraview Python with Ctypes package in Linux. I tried with the following command without success. Despite if compiles Ok. nvcc...
