# Making client-side only C++ class accessible from Python

**URL:** https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094
**Category:** Development
**Created:** [June 24, 2019, 9:36am UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094 "2019-06-24T09:36:45Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![BetaRavener](https://discourse.paraview.org/user_avatar/discourse.paraview.org/betaravener/32/1624_2.png) [@BetaRavener](https://discourse.paraview.org/u/BetaRavener)
#### Post date: [June 24, 2019, 9:36am UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/1 "2019-06-24T09:36:45Z")

</div>

**Question:**  
How to access C++ class from python that’s client-side only and has no XML file to wrap?

**Little background:**  
As part of our application, I have implemented (via events) C++ class that allows camera to follow object. I have GUI toolbar with buttons for controlling the behaviour, e.g. to enable/disable tracking. This “behavior” class as well as toolbar exists on client side as I’m only interacting with client-side camera and both are part of one plugin. The behavior class is implemented as singleton which registers all existing render views.

Now, we are using python scripts for project files that get loaded on start-up and I’d like to setup the object tracking behaviour there (like, what object to track). So I need to access my C++ singleton from python and interact with the class. However, I haven’t found a way to do that yet. Could anyone point me in direction, maybe existing plugin or documentation?

One thing I noticed when going through existing implementation of some PV python scripts - they import classes from `paraview.module` namespace. I believe this gives access to any C++ PV class from Python and is basically what I need. However, our custom modules are not listed there and I suppose this is because they come from plugin, not application itself. If there was a way to achieve similar for plugins, that would be perfect.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [June 24, 2019, 2:22pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/2 "2019-06-24T14:22:29Z")

</div>

> [@BetaRavener](#):
>
> So I need to access my C++ singleton from python and interact with the class. However, I haven’t found a way to do that yet.

First question: is your C++ class wrapped in Python?

@ben.boeckel Is it possible to import Python-wrapped classes defined in a plugin? If so, what would the import look like?

---

<div class="post-metadata">

### Author: ![BetaRavener](https://discourse.paraview.org/user_avatar/discourse.paraview.org/betaravener/32/1624_2.png) [@BetaRavener](https://discourse.paraview.org/u/BetaRavener)
#### Post date: [June 24, 2019, 2:31pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/3 "2019-06-24T14:31:17Z")

</div>

> First question: is your C++ class wrapped in Python?

How exactly is this done? Is there CMake rule for that or I need to write the wrapper myself? I guess ParaView doesn’t have hand-coded wrapper for each class, that would be lot of effort.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [June 24, 2019, 2:39pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/4 "2019-06-24T14:39:08Z")

</div>

> [@BetaRavener](#):
>
> How exactly is this done? Is there CMake rule for that or I need to write the wrapper myself? I guess ParaView doesn’t have hand-coded wrapper for each class, that would be lot of effort.

As long as your helper looks mostly like a VTK object, it can be automatically wrapped. I’m not up to date on the particulars of wrapping code in a plugin, though.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [June 24, 2019, 3:47pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/5 "2019-06-24T15:47:38Z")

</div>

There’s the `PYTHON_MODULES` argument for making pure-Python modules available in the plugin (they’re available as if they were placed in the global import namespace). There’s no support for providing Python-wrapped VTK class APIs as part of a plugin; they’d need to be wrapped using the CMake API manually and then installed properly on its own. There’s probably some work necessary to get those visible to the Python import logic too.

---

<div class="post-metadata">

### Author: ![BetaRavener](https://discourse.paraview.org/user_avatar/discourse.paraview.org/betaravener/32/1624_2.png) [@BetaRavener](https://discourse.paraview.org/u/BetaRavener)
#### Post date: [June 24, 2019, 4:13pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/6 "2019-06-24T16:13:16Z")

</div>

The class that provides camera tracking is currently just a Qt subclass, so you helped me realize I would probably also need to create a helper which is a VTK class and forwards the calls to the Qt one. However, we have other classes that follow VTK interface and I couldn’t really find python namespace (like that `modules`) in which I could access them directly - they are only available through proxies defined by XML.

Thanks @ben.boeckel for the update. So I was intrigued about this `modules` namespace which is located under \<ParaView-install/lib/python3.5/site-packages/paraview/modules\> and seem to wrap ParaView/VTK classes. I couldn’t find rule that produces them just yet, but couldn’t those be used? Or is that what you are calling “using the CMake API manually”? You’re right though that we would still need mechanism to consume this pair of python+.so files somehow.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [June 24, 2019, 4:35pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/7 "2019-06-24T16:35:41Z")

</div>

VTK’s module system will create Python modules under the `paraview.modules` package. It’s the same way that (much of) the `vtkmodules` package for VTK itself gets populated. The CMake rules are associated with the `vtk_module_wrap_python` call. Plugins can’t really access this at all, so they need to handle Python wrapping with their own `vtk_module_wrap_python` call.

---

<div class="post-metadata">

### Author: ![BetaRavener](https://discourse.paraview.org/user_avatar/discourse.paraview.org/betaravener/32/1624_2.png) [@BetaRavener](https://discourse.paraview.org/u/BetaRavener)
#### Post date: [June 24, 2019, 5:18pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/8 "2019-06-24T17:18:04Z")

</div>

> [@ben.boeckel](#):
>
> `vtk_module_wrap_python`

Thanks, might be just what I was looking for. I’ve been also thinking in different direction - instead of wrapping C++ class with Python myself, would it be possible to write ServerManager XML that would instantiate client-side object? There is `SourceProxy` tag with `class=vtkClass` attribute which instantiates `vtkClass` on server and uses `SourceProxy` (`vtkSMSourceProxy`) to interact with it. Maybe there are other proxies or tags that I could use, where `vtkClass` is instantiated on client? Or would it be possible to create my own “proxy” that can be consumed in XML?

Edit: I’ve indeed found an example plugin creating “MyElevationProxy”. Will try to explore this option further.

---

<div class="post-metadata">

### Author: ![BetaRavener](https://discourse.paraview.org/user_avatar/discourse.paraview.org/betaravener/32/1624_2.png) [@BetaRavener](https://discourse.paraview.org/u/BetaRavener)
#### Post date: [June 26, 2019, 12:46pm UTC](https://discourse.paraview.org/t/making-client-side-only-c-class-accessible-from-python/2094/9 "2019-06-26T12:46:05Z")

</div>

Just to provide update on this, I have went with the second option.

I’ve created simple subclass of `vtkSMProxy` that sets `Location` in constructor to `vtkProcessModule::CLIENT`. I can then use this subclass in XML in place of `<Proxy>` and the class in `class` attribute will get instantiated only on client. Next, I define properties for this proxy as usual and then leverage ServerManager to load this XML and make the proxy available in Python interpreter, which is sufficient for our usecase.

Thanks for the help and giving me some clarity.
