expose vtkSmartPointer<vtkXYPlotActor> to python

Asked by Chaffra Affouda on 2013-03-13

How can I expose a vtk pointer as a vtk python object? I know I have to add somthing to swig but I dont know where to start.
In c++ I have a function get_actor which returns a vtkSmartPointer<vtkXYPlotActor> to python but I'd like to be ablbe to modify the object from python.

Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
2013-03-14
Last reply:
2013-03-15

This question was reopened

Chaffra Affouda (chaffra) said : #1

On the same token I also need to now how to expose the attributes of let's say GenericVTKPlottable to python

Johan Hake (johan-hake) said : #2

This is beyond me. vtk is not wrapped using SWIG. Combining vtk objects
from PyDOLFIN with pyvtk is therefore not trivial and will not be supported.

Johan

On 03/13/2013 03:31 PM, Chaffra Affouda wrote:
> New question #224167 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/224167
>
> How can I expose a vtk pointer as a vtk python object? I know I have to add somthing to swig but I dont know where to start.
> In c++ I have a function get_actor which returns a vtkSmartPointer<vtkXYPlotActor> to python but I'd like to be ablbe to modify the object from python.
>
> Thanks
>

Chaffra Affouda (chaffra) said : #3

Hum,

I guess this was the main advantage of having viper-dolfin, because you could access the vtk objects in python for customization...

Thanks,
Chaffra

Chaffra Affouda (chaffra) said : #4

I found the way to do it by googling. I added a new file vtk.i in dolfin/swig/typemaps/vtk.i with the content below. I added a target link to libvtkPythonCore.so et voila. I can now access the _actor attribute of VTKPlottableGenericFunction1D in python as a VTK object.

By default how does dolfin know which vtk libs to link to? libvtkPythonCore is not enabled by default.

-------------vtk.i-----------------------
#ifdef HAS_VTK
%{
#include "vtkPythonUtil.h"
%}
%typemap(out) vtkSmartPointer<vtkActor2D> {

PyImport_ImportModule("vtk");

$result = vtkPythonUtil::GetObjectFromPointer ( (vtkSmartPointer<vtkActor2D>)$1 );

}

%typemap(in) vtkSmartPointer<vtkActor2D> {

/*$1 = NULL;*/

$1 = (vtkSmartPointer<vtkActor2D>) vtkPythonUtil::GetPointerFromObject ( $input, "vtkActor2D" );

if ( $1 == NULL ) { SWIG_fail; }

}
#endif

Johan Hake (johan-hake) said : #5

Nice!

Welcome to the wonderful world of SWIG and Python C-API ;)

Not sure this is _the_ correct way to do it though. You import pyvtk
within the typemap. But why not. If that object need to be exposed as a
pyvtk object when it comes out I guess there are no way around it.

VTK is configured from line: 215 in dolfin/CMakeList.txt. We would also
need to have a check for pyvtk being installed for this to work as
expected. Introducing something like HAS_PYVTK.

I am not a VTK viz so I am not sure what objects would need to be
wrapped. Maybe you can come up with some suggestions here?

I would also need comments from Joachim Haga about possible caveats.

Johan

On 03/14/2013 10:41 PM, Chaffra Affouda wrote:
> Question #224167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224167
>
> Status: Solved => Open
>
> Chaffra Affouda is still having a problem:
> I found the way to do it by googling. I added a new file vtk.i in
> dolfin/swig/typemaps/vtk.i with the content below. I added a target link
> to libvtkPythonCore.so et voila. I can now access the _actor attribute
> of VTKPlottableGenericFunction1D in python as a VTK object.
>
> By default how does dolfin know which vtk libs to link to?
> libvtkPythonCore is not enabled by default.
>
>
> -------------vtk.i-----------------------
> #ifdef HAS_VTK
> %{
> #include "vtkPythonUtil.h"
> %}
> %typemap(out) vtkSmartPointer<vtkActor2D> {
>
> PyImport_ImportModule("vtk");
>
> $result = vtkPythonUtil::GetObjectFromPointer (
> (vtkSmartPointer<vtkActor2D>)$1 );
>
> }
>
> %typemap(in) vtkSmartPointer<vtkActor2D> {
>
> /*$1 = NULL;*/
>
> $1 = (vtkSmartPointer<vtkActor2D>) vtkPythonUtil::GetPointerFromObject (
> $input, "vtkActor2D" );
>
> if ( $1 == NULL ) { SWIG_fail; }
>
> }
> #endif
>

Joachim Haga (jobh) said : #6

No specific caveats, but modifying the vtk objects directly isn't
"officially" supported. I.e., things may break.
Den 15. mars 2013 09:35 skrev "Johan Hake" <
<email address hidden>> følgende:

> Question #224167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224167
>
> Status: Open => Answered
>
> Johan Hake proposed the following answer:
> Nice!
>
> Welcome to the wonderful world of SWIG and Python C-API ;)
>
> Not sure this is _the_ correct way to do it though. You import pyvtk
> within the typemap. But why not. If that object need to be exposed as a
> pyvtk object when it comes out I guess there are no way around it.
>
> VTK is configured from line: 215 in dolfin/CMakeList.txt. We would also
> need to have a check for pyvtk being installed for this to work as
> expected. Introducing something like HAS_PYVTK.
>
> I am not a VTK viz so I am not sure what objects would need to be
> wrapped. Maybe you can come up with some suggestions here?
>
> I would also need comments from Joachim Haga about possible caveats.
>
> Johan
>
> On 03/14/2013 10:41 PM, Chaffra Affouda wrote:
> > Question #224167 on DOLFIN changed:
> > https://answers.launchpad.net/dolfin/+question/224167
> >
> > Status: Solved => Open
> >
> > Chaffra Affouda is still having a problem:
> > I found the way to do it by googling. I added a new file vtk.i in
> > dolfin/swig/typemaps/vtk.i with the content below. I added a target link
> > to libvtkPythonCore.so et voila. I can now access the _actor attribute
> > of VTKPlottableGenericFunction1D in python as a VTK object.
> >
> > By default how does dolfin know which vtk libs to link to?
> > libvtkPythonCore is not enabled by default.
> >
> >
> > -------------vtk.i-----------------------
> > #ifdef HAS_VTK
> > %{
> > #include "vtkPythonUtil.h"
> > %}
> > %typemap(out) vtkSmartPointer<vtkActor2D> {
> >
> > PyImport_ImportModule("vtk");
> >
> > $result = vtkPythonUtil::GetObjectFromPointer (
> > (vtkSmartPointer<vtkActor2D>)$1 );
> >
> > }
> >
> > %typemap(in) vtkSmartPointer<vtkActor2D> {
> >
> > /*$1 = NULL;*/
> >
> > $1 = (vtkSmartPointer<vtkActor2D>) vtkPythonUtil::GetPointerFromObject (
> > $input, "vtkActor2D" );
> >
> > if ( $1 == NULL ) { SWIG_fail; }
> >
> > }
> > #endif
> >
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Johan Hake (johan-hake) said : #7

On 03/15/2013 09:47 AM, Joachim Haga wrote:
> Question #224167 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224167
>
> Joachim Haga proposed the following answer:
> No specific caveats, but modifying the vtk objects directly isn't
> "officially" supported. I.e., things may break.

You mean that things from the dolfin_plot interface might break?

Johan

> Den 15. mars 2013 09:35 skrev "Johan Hake" <
> <email address hidden>> følgende:
>
>> Question #224167 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/224167
>>
>> Status: Open => Answered
>>
>> Johan Hake proposed the following answer:
>> Nice!
>>
>> Welcome to the wonderful world of SWIG and Python C-API ;)
>>
>> Not sure this is _the_ correct way to do it though. You import pyvtk
>> within the typemap. But why not. If that object need to be exposed as a
>> pyvtk object when it comes out I guess there are no way around it.
>>
>> VTK is configured from line: 215 in dolfin/CMakeList.txt. We would also
>> need to have a check for pyvtk being installed for this to work as
>> expected. Introducing something like HAS_PYVTK.
>>
>> I am not a VTK viz so I am not sure what objects would need to be
>> wrapped. Maybe you can come up with some suggestions here?
>>
>> I would also need comments from Joachim Haga about possible caveats.
>>
>> Johan
>>
>> On 03/14/2013 10:41 PM, Chaffra Affouda wrote:
>>> Question #224167 on DOLFIN changed:
>>> https://answers.launchpad.net/dolfin/+question/224167
>>>
>>> Status: Solved => Open
>>>
>>> Chaffra Affouda is still having a problem:
>>> I found the way to do it by googling. I added a new file vtk.i in
>>> dolfin/swig/typemaps/vtk.i with the content below. I added a target link
>>> to libvtkPythonCore.so et voila. I can now access the _actor attribute
>>> of VTKPlottableGenericFunction1D in python as a VTK object.
>>>
>>> By default how does dolfin know which vtk libs to link to?
>>> libvtkPythonCore is not enabled by default.
>>>
>>>
>>> -------------vtk.i-----------------------
>>> #ifdef HAS_VTK
>>> %{
>>> #include "vtkPythonUtil.h"
>>> %}
>>> %typemap(out) vtkSmartPointer<vtkActor2D> {
>>>
>>> PyImport_ImportModule("vtk");
>>>
>>> $result = vtkPythonUtil::GetObjectFromPointer (
>>> (vtkSmartPointer<vtkActor2D>)$1 );
>>>
>>> }
>>>
>>> %typemap(in) vtkSmartPointer<vtkActor2D> {
>>>
>>> /*$1 = NULL;*/
>>>
>>> $1 = (vtkSmartPointer<vtkActor2D>) vtkPythonUtil::GetPointerFromObject (
>>> $input, "vtkActor2D" );
>>>
>>> if ( $1 == NULL ) { SWIG_fail; }
>>>
>>> }
>>> #endif
>>>
>>
>> --
>> You received this question notification because you are a member of
>> DOLFIN Team, which is an answer contact for DOLFIN.
>>
>

Chaffra Affouda (chaffra) said : #8

BTW I had to change dolfin/CmakeList.txt like this. If I dont' comment out if (NOT VTK_LIBRARIES) I get a linking problem. Is VTK_LIBRARIES defined elsewhere?

# if (NOT VTK_LIBRARIES)
    # FIXME: Check which of these we actually need.
    set(VTK_LIBRARIES
      vtkCommon
      vtkFiltering
      vtkImaging
      vtkGraphics
      vtkGenericFiltering
      vtkIO
      vtkRendering
      vtkVolumeRendering
      vtkHybrid
      vtkWidgets
      vtkParallel
      vtkInfovis
      vtkGeovis
      vtkViews
      vtkCharts
      vtkPythonCore
      )
# endif()

Can you help with this problem?

Provide an answer of your own, or ask Chaffra Affouda for more information if necessary.

To post a message you must log in.