[Paraview] Use of Python declared variables inside new scripts from ProgrammableSource and/or ProgrammableFilter.
Guillaume Jacquenot
guillaume.jacquenot at gmail.com
Sat Jan 23 11:00:14 EST 2016
I have understood why my solution did not work.
My goal is to access user defined data from a ProgrammableFilter
I know my solution is not recommended,but it works, and I could not figure
other solutions
I have used the RequestInformationScript as a constructor to initiate my
additional variables:
My bug was appening, since I did not know that this script may be called
several times. The first time the additional members were correctly
evaluated. But my additional members became all mixed up the second time
So I added a test to see if my additional members were already created.
Below is an example that shows how to add member to a ProgrammableFilter
from paraview.simple import Sphere
from paraview.simple import ProgrammableFilter
from paraview.simple import UpdatePipeline
sphere = Sphere()
p = ProgrammableFilter(Input=sphere)
p.Script = 'print(self.x)'
p.RequestInformationScript = 'if "x" not in dir(self):\n self.x=12'
UpdatePipeline()
However this solution does not work for ProgrammableSource
from paraview.simple import ProgrammableSource
p=ProgrammableSource()
p.Script = 'print(self.x)'
p.RequestInformationScript = 'if "x" not in dir(self):\n self.x=12'
UpdatePipeline()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files\ParaView
4.4.0\lib\paraview-4.4\site-packages\paraview\servermanager.py", line 302,
in __setattr__
"to add this attribute.")
AttributeError: Attribute RequestInformationScript does not exist. This
class does not allow addition of new attributes to avoid mistakes due to
typos. Use add_attribute() if you really want to add this attribute.
Traceback (most recent call last):
File "<string>", line 19, in <module>
File "<string>", line 2, in RequestData
AttributeError: 'vtkPVClientServerCoreCorePython.vtkPythonProgramma' object
has no attribute 'x'
If I do
from paraview.simple import ProgrammableSource
p=ProgrammableSource()
p.add_attribute('x',666)
I could not find how to use x in my script
2016-01-18 15:35 GMT+01:00 Guillaume Jacquenot <
guillaume.jacquenot at gmail.com>:
> Hello everyone,
>
> Thanks Utkarsh for this answer.
> It works with pvpython.
> Unfortunately, this does not work with paraview
> I have tested manually from a desktop version of Paraview, with Paraview
> 4.4 and 5.0 on Windows.
> I have this error:
> NameError: global name 'meta_data' is not defined
>
> In fact, I have found another solution to do what I want from Paraview
> I declare the new variables as member in the RequestInformationScript
> (self.data=12) and I use them in the Script (print(self.data))
> However,this has some drawbacks. When I update the paraview tree, eg I
> update the size of an unrelated glyph, the value changes, and consequently
> breaks my scene
>
> Moreover, when I test it with pvpython, it clearly tells me that it is not
> from paraview.simple import ProgrammableSource
> from paraview.simple import UpdatePipeline
>
> >>> p = ProgrammableSource();p.Script =
> 'print(self.x)';p.RequestInformationScript = 'self.x=12';UpdatePipeline()
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "C:\Program Files\ParaView
> 5.0.0\lib\paraview-5.0\site-packages\paraview\servermanager.py", line 302,
> in __setattr__ "to add this attribute.")
> AttributeError: Attribute RequestInformationScript does not exist. This
> class does not allow addition of new attributes to avoid mistakes due to
> typos. Use add_attribute() if you really want to add this attribute.
>
> p = ProgrammableSource();p.Script =
> 'print(self.x)';p.add_attribute('x',12);UpdatePipeline()
> But, how to use this attribute?
> May be I need to play with parameters?
>
>
> This finds some echoes here, where apparently the only solution proposed
> is to ascii write the data to pass to the script.
> https://cmake.org/pipermail/paraview/2013-May/028291.html
> This is clearly not a solution for me.
>
>
> 2016-01-04 20:22 GMT+01:00 Utkarsh Ayachit <utkarsh.ayachit at kitware.com>:
>
>> The following works in builtin (non-remote server) mode only.
>>
>> global meta_data
>> meta_data =12
>>
>> p = ProgrammableSource()
>> p.Script="""
>> global meta_data
>> print meta_data
>> """
>>
>> UpdatePipeline()
>>
>> --------------------------------------------
>> Output:
>> --------------------------------------------
>> 12
>>
>> Utkarsh
>>
>> On Wed, Dec 30, 2015 at 9:03 AM, Guillaume Jacquenot
>> <guillaume.jacquenot at gmail.com> wrote:
>> > I would like to use Python declared variables inside new scripts from
>> > ProgrammableSource and/or ProgrammableFilter.
>> >
>> > Why do I want such a thing? I could simply declared these variables in
>> the
>> > code of the script.
>> > But what if these variables are to be used by several scripts from
>> > ProgrammableSource and/or ProgrammableFilter?
>> >
>> > Of course, I can serialize variables and pass the serialized data to the
>> > scripts, taking special care for float. But, what if I have large data,
>> that
>> > are difficult to serialize?
>> >
>> > So, here is my question:
>> > Is there a mechanism to access already defined data inside the scripts
>> of
>> > ProgrammableSource and/or ProgrammableFilter? For example, a global
>> scope
>> > dict?
>> > Do I have to use an external file to store and access data?
>> > Below is the example of the Python file I would like to run , where I
>> have
>> > defined a 'data' that I want to access inside the script. If I try to
>> > directly access 'data', I have a "NameError: global name 'data' is not
>> > defined"
>> >
>> > My data are in fact transformation matrices that will be used to move
>> > objects from a ProgrammableFilter. Using the Animation Keyframes are
>> > terribly slow.
>> >
>> > Guillaume
>> >
>> >
>> >
>> > Python script:
>> >
>> > data = ....
>> >
>> > script="""
>> > instructions to access "data" at run time
>> > ...
>> > """
>> >
>> > scriptRequestInformation="""
>> > ...
>> > """
>> >
>> > source = ProgrammableSource()
>> > source.OutputDataSetType = 'vtkStructuredGrid'
>> > source.Script = script
>> > source.ScriptRequestInformation = scriptRequestInformation
>> > source.PythonPath = ''
>> >
>> > _______________________________________________
>> > 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
>> >
>> > Search the list archives at: http://markmail.org/search/?q=ParaView
>> >
>> > Follow this link to subscribe/unsubscribe:
>> > http://public.kitware.com/mailman/listinfo/paraview
>> >
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20160123/b39a5371/attachment.html>
More information about the ParaView
mailing list