[Paraview] Programmable filter from external python program

Simon Couture simon-2.couture at polymtl.ca
Fri Mar 7 17:47:10 EST 2014


I didn't want to use pvpython since my application uses ipython as its
interactive interface to the user, and I'm not sure if I could get ipython to
work with pvpython. Furthermore, I really wanted to embed paraview in my python
program rather than embed my program into pvpython.

In case others stumble on the same problem in the future, I think I figured out
how to make the programmable filter work from an external python session (the
same problem might also apply if you're using pvpython, I didnt test that).

I stumbled on the following bug page for paraview:
http://paraview.org/Bug/view.php?id=6456

and realized that the reason why I was getting the error from my previous
message

"In [9]: UpdatePipeline()
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named paraview"

right after creating the programmable filter was that the filter's own python
script was somehow executing in an environment where the python path did not
contain the paths to the paraview related modules (even though my main python
script does). Therefore if the same pythonpath entries that are present in my
main python program's pythonpath are added to the programmable filter's
PythonPath property (notice the format "'path1';'path2';..."),

In [1]: from paraview.simple import *
paraview version 3.98.1

In [2]: Sphere1 = Sphere( guiName="Sphere1" )

In [3]: ProgrammableFilter1 = ProgrammableFilter( guiName="ProgrammableFilter1",
RequestUpdateExtentScript='', RequestInformationScript='', Script="input =
self.GetInput()\noutput = self.GetOutput()\noutput.ShallowCopy(input)\n\nnp =
output.GetNumberOfPoints()\n\narray =
vtk.vtkFloatArray()\narray.SetName('newArray')\narray.SetNumberOfTuples(np)\nfor
i in range(np):\n    array.SetValue(i,
i)\n\noutput.GetPointData().AddArray(array)\n", PythonPath='' )

In [4]: ProgrammableFilter1.PythonPath =
"'/raid/usr/local/lib/paraview-3.98/site-packages';'/raid/usr/local/lib/paraview-3.98';'/raid/usr/local/lib/paraview-3.98/lib/python2.7';'/raid/usr/local/lib/paraview-3.98/lib/python2.7/plat-linux2';'/raid/usr/local/lib/paraview-3.98/lib/python2.7/lib-tk';'/raid/usr/local/lib/paraview-3.98/lib/python2.7/lib-old';'/raid/usr/local/lib/paraview-3.98/lib/python2.7/lib-dynload';'/raid/usr/local/lib/paraview-3.98/lib/python2.7/site-packages'"

In [5]: ProgrammableFilter1.PointData.GetNumberOfArrays()
Out[5]: 2

then voila, the programmable filter works!

Simon


Quoting Paul Edwards <paul.m.edwards at gmail.com>:

> Can't you just use the pvpython or pvbatch if you need to run on the
> command line?  I'm not sure what could be your problem other than if you
> are using a different python to what paraview is compiled against.
>
> Regards,
> Paul
>
>
> On 6 March 2014 19:46, Simon Couture <simon-2.couture at polymtl.ca> wrote:
>
> > Thank you for your help Paul. I think I narrowed down my problem. Your
> > example
> > (and also my own) indeed work when executed from the paraview GUI's python
> > shell. However, when I try to run your example from an external python
> > shell
> > (outside the paraview GUI), this is what I get:
> >
> > In [5]: from paraview.simple import *
> > paraview version 3.98.1
> >
> > In [6]: paraview
> > Out[6]: <module 'paraview' from
> > '/raid/usr/local/lib/paraview-3.98/site-packages/paraview/__init__.py'>
> >
> > In [7]: Sphere1 = Sphere( guiName="Sphere1" )
> >
> > In [8]: ProgrammableFilter1 = ProgrammableFilter(
> > guiName="ProgrammableFilter1",
> > RequestUpdateExtentScript='', RequestInformationScript='', Script="input =
> > self.GetInput()\noutput =
> > self.GetOutput()\noutput.ShallowCopy(input)\n\nnp =
> > output.GetNumberOfPoints()\n\narray =
> >
> >
>
vtk.vtkFloatArray()\narray.SetName('newArray')\narray.SetNumberOfTuples(np)\nfor
> > i in range(np):\n    array.SetValue(i,
> > i)\n\noutput.GetPointData().AddArray(array)\n", PythonPath='' )
> >
> > In [9]: UpdatePipeline()
> > Traceback (most recent call last):
> >   File "<string>", line 1, in <module>
> > ImportError: No module named paraview
> >
> > In [10]: paraview
> > Out[10]: <module 'paraview' from
> > '/raid/usr/local/lib/paraview-3.98/site-packages/paraview/__init__.py'>
> >
> > In [11]: ProgrammableFilter1.PointData.GetNumberOfArrays()
> > Out[11]: 0
> >
> > I make sure that my python path contains all the directories that are
> > present in
> > the paraview GUI's python shell session. Indeed I can type 'import
> > paraview' or
> > 'import paraview.simple' (as above) with no problem, but somehow I get this
> > weird ImportError when updating the pipeline with the programmable filter.
> > Any
> > ideas on how to get to the bottom of this? This is on Linux CentOS and
> > using
> > either python 2.6 or 2.7 yields this error.
> >
> > Simon
> >
> >
> > Quoting Paul Edwards <paul.m.edwards at gmail.com>:
> >
> > > Are you sure you are updating?  This works for me in pvpython:
> > >
> > > >>> from paraview.simple import *
> > > paraview version 4.1.0-243-gd6a42b6
> > > >>> Sphere1 = Sphere( guiName="Sphere1" )
> > > >>>
> > > >>> ProgrammableFilter1 = ProgrammableFilter(
> > > guiName="ProgrammableFilter1", RequestUpdateExtentScript='',
> > > RequestInformationScript='', Script="input = self.GetInput()\noutput =
> > > self.GetOutput()\noutput.ShallowCopy(input)\n\nnp =
> > > output.GetNumberOfPoints()\n\narray =
> > >
> >
> >
>
vtk.vtkFloatArray()\narray.SetName('newArray')\narray.SetNumberOfTuples(np)\nfor
> > > i in range(np):\n    array.SetValue(i,
> > > i)\n\noutput.GetPointData().AddArray(array)\n", PythonPath='' )
> > > >>> UpdatePipeline()
> > > >>> ProgrammableFilter1.PointData.GetNumberOfArrays()
> > > 2
> > >
> > > Regards,
> > > Paul
> > >
> > >
> > >
> > > On 6 March 2014 01:31, Simon Couture <simon-2.couture at polymtl.ca> wrote:
> > >
> > > > Hi everyone,
> > > >
> > > > I am trying to use paraview from an external python program where I
> > import
> > > > paraview.simple and mostly use python code generated by Paraview's
> > trace
> > > > tool
> > > > to accomplish what I want. In general it works well, but in one
> > instance I
> > > > need
> > > > to generate a new data array from my source/reader, and as far as I
> > know,
> > > > the
> > > > programmable filter is the only way to do this (the actual PointData
> > > > arrays of
> > > > the sources are not directly visible/editable through the python
> > shell).
> > > > Now
> > > > when I try to execute the python code that I get from trace to insert a
> > > > programmable filter with a given script, the PointData array that the
> > > > programmable filter's script is supposed to create is not created (and
> > it
> > > > does
> > > > work when I create the programmable filter using the Paraview GUI).
> > > >
> > > > To be more clear, here is the python code that I run (cutting out
> > camera
> > > > and
> > > > rendering commands):
> > > >
> > > > mysource = AVSUCDReader( FileNames=['/home/myfile.inp'] )
> > > >
> > > > mysource.PointArrayStatus = []
> > > > mysource.CellArrayStatus = []
> > > >
> > > > RenderView1 = GetRenderView()
> > > > DataRepresentation3 = Show()
> > > > ProgrammableFilter1 = ProgrammableFilter()
> > > > DataRepresentation4 = Show()
> > > > ProgrammableFilter1.RequestUpdateExtentScript = ''
> > > > ProgrammableFilter1.PythonPath = ''
> > > > ProgrammableFilter1.RequestInformationScript = ''
> > > > ProgrammableFilter1.Script = "pdi = self.GetInput()\nnbpoints =
> > > > pdi.GetNumberOfPoints()\ninput_pointdata =
> > > > pdi.GetPointData().GetArray('data')
> > > > [... the rest of my script here]"
> > > >
> > > > After running this in either the paraview python shell or from an
> > external
> > > > python program, if I do for instance:
> > > >
> > > > ProgrammableFilter1.PointData.GetNumberOfArrays()
> > > >
> > > > I get 0 meaning that no PointData array was created, whereas if the
> > same
> > > > programmable filter is created in the paraview GUI, I would get 1.
> > Please
> > > > note
> > > > that I am not using the Copy Arrays option of the programmable filter.
> > > >
> > > > Has anyone successfully used the programmable filter from a python
> > script?
> > > >
> > > > _______________________________________________
> > > > Powered by www.kitware.com
> > > >
> > > > Visit other Kitware open-source projects at
> > > > http://www.kitware.com/opensource/opensource.html
> > > >
> > > > Please keep messages on-topic and check the ParaView Wiki at:
> > > > http://paraview.org/Wiki/ParaView
> > > >
> > > > Follow this link to subscribe/unsubscribe:
> > > > http://www.paraview.org/mailman/listinfo/paraview
> > > >
> > >
> >
> >
> >
> >
>




More information about the ParaView mailing list