# extracting (or creating) vtkSmartPointer from Python-wrapped VTK object

**URL:** https://discourse.vtk.org/t/extracting-or-creating-vtksmartpointer-from-python-wrapped-vtk-object/2407
**Category:** Support
**Created:** [January 11, 2020, 4:32am UTC](https://discourse.vtk.org/t/extracting-or-creating-vtksmartpointer-from-python-wrapped-vtk-object/2407 "2020-01-11T04:32:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stefanseefeld](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@stefanseefeld](https://discourse.vtk.org/u/stefanseefeld)
#### Post date: [January 11, 2020, 4:32am UTC](https://discourse.vtk.org/t/extracting-or-creating-vtksmartpointer-from-python-wrapped-vtk-object/2407/1 "2020-01-11T04:32:42Z")

</div>

I’d like to integrate VTK/Python into my own project, which uses a mix of C++ and Python code.  
I need to call functions that take `vtkSmartPointer` arguments with object references I have extracted from VTK/Python wrappers.  
While I understand how to access raw VTK pointers from PyObject pointers by querying the ` __this__ ` attribute, I don’t quite understand how the reference counting is to be done for such pointers. Consider the following example. Given these two functions:

void function\_storing\_vtk\_object\_ptr(vtkObjectBase \*);  
vtkObjectBase \*extract\_vtk\_object\_ptr(PyObject \*);

I can now write my own C++ Python extension and pass VTK/Python arguments to it. But who owns the objects that are passed around that way ? How can I make sure that the references I store will remain valid, even if no-one else is referencing these objects ?  
I found some mention of vtkSmartPointer in the context of Boost.Python wrappers, but not enough details to help me answer the above question.  
Any help is much appreciated !

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [March 16, 2020, 3:33pm UTC](https://discourse.vtk.org/t/extracting-or-creating-vtksmartpointer-from-python-wrapped-vtk-object/2407/2 "2020-03-16T15:33:32Z")

</div>

VTK objects are self-refcounting (intrusively counted). So all objects are basically shared ownership. The `vtkSmartPointer` is an RAII object which manages the refcount nicely in C++. You can just create one from an existing object safely.

---

<div class="post-metadata">

### Author: ![desheng](https://discourse.vtk.org/user_avatar/discourse.vtk.org/desheng/32/2764_2.png) [@desheng](https://discourse.vtk.org/u/desheng)
#### Post date: [December 20, 2020, 2:03am UTC](https://discourse.vtk.org/t/extracting-or-creating-vtksmartpointer-from-python-wrapped-vtk-object/2407/3 "2020-12-20T02:03:04Z")

</div>

Hi Stefan

Could you please take a look at my question here

> [@How to wrap VTK-derived C++ function for Python](https://discourse.vtk.org/t/how-to-wrap-c-vtk-custom-function-for-python/4862):
>
> Is there any place I can find a working example to wrap a C++ function (using VTK) to be used by Python? Searched and found this page [mix VTK and SWIG Python](https://stackoverflow.com/questions/8898041/mix-vtk-and-swig-python) but there is no compiling details, and this simple example on this page [https://public.kitware.com/pipermail/vtkusers/2002-October/013980.html](https://public.kitware.com/pipermail/vtkusers/2002-October/013980.html) But for my system Ubuntu 16.04, vtk 8.2.0, Python 3.6.8, I could not make the example work. I have to modify them to let the compiling done without error. Details: #============simple.cpp=========…

I tried to wrap an extremely simple C++ function for Python but could never make it work.  
Since you mentioned  
“I can now write my own C++ Python extension and pass VTK/Python arguments to it”  
I believe you have much more experience on this. Thanks.

Btw, I found your question while edited my question, the system recommended that to me.
