[Paraview] Programmable sphere source - update position each timestep from csv file
Cory Quammen
cory.quammen at kitware.com
Mon Jul 18 11:07:29 EDT 2016
James,
Thanks for sharing your solution. That seems like a reasonable
approach to achieving what you need.
I was thinking you might be able to use the animation controls to
specify the centers of your sphere, but that does not appear to be
possible with discrete sphere positions. You can do some interpolation
of the x, y, and z components of the clip function center
independently, but I can't think of a way to import discrete
positions.
Best,
Cory
On Fri, Jul 15, 2016 at 5:31 AM, James Avery <j.avery at ucl.ac.uk> wrote:
> Hi Cory,
>
> Thanks for your help. Sorry if the message was rambling, but I had a script
> which made the spheres is called from the python shell as you suggest.
> However, I was looking for a solution that was automatically updated when
> changing the timestep in the GUI - I was not clear on this, apologies. So I
> thought I had to use a programmable source or programmable filter.
>
> The spheres are designed to indicate locations inside a larger dataset, so I
> compromised by having a spherical clip which updates its centre. The code
> for the programmable filter to do this is:
>
> import vtk
> import csv
> csvfilename = '../../resources/vtk/testpos.csv'
> input = self.GetInputDataObject(0, 0)
> output = self.GetOutputDataObject(0)
>
> t =
> self.GetInputDataObject(0,0).GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())
> TimePoint = int(t)
> # read the specific line from the csv file
> Centre = [0.0, 0.0, 0.0]
> count = 0
>
> with open(csvfilename) as f:
> r = csv.reader(f)
> for row in r:
> if count == TimePoint:
> Centre = [float(i) for i in row]
> break
> count += 1
>
> s = vtk.vtkSphere()
> s.SetCenter(Centre)
> s.SetRadius(10)
>
> clip = vtk.vtkClipDataSet()
> clip.SetInputDataObject(input)
> clip.SetClipFunction(s)
> clip.SetValue(0.0)
> clip.InsideOutOn()
> clip.Update()
> print clip
>
> output.ShallowCopy(clip.GetOutputDataObject(0))
>
> Probably not the most elegant solution, but it seems to be doing the job!
>
> Kind regards,
>
> James
>
> On 14/07/2016 21:38, Cory Quammen wrote:
>
> Hi James,
>
> Instead of the Programmable Source, you should be able to use the
> paraview.simple module to do this in a Python script. Here's code to
> create and show a Sphere source:
>
> s = Sphere()
> Show(s)
>
> You can update the sphere position with
>
> s.Center = [1, 2, 3]
>
> Obviously, you can use the above line in your loop over the positions
> from the CSV file. After each Center update, you should re-render with
>
> RenderAllViews()
>
> You can run Python script files through the Python console by clicking
> the "Run Script" button and navigating to your script file.
>
> HTH,
> Cory
>
> On Wed, Jul 13, 2016 at 4:59 AM, James Avery <j.avery at ucl.ac.uk> wrote:
>
> Hi all,
>
> I am trying to create a programmable source to create a sphere, the location
> of which updates per time step based on coordinates in a csv file. However,
> I have sort of got my head around the python library (thanks to tracing what
> I do in the GUI!), but I think one needs to use the vtk library instead? I
> have written a python script which does the job when called from the python
> shell, but this is not updating when the timestep changes. I have tried
> adapting this code to work as a programmable source, but to no avail.
>
> Here is the code for the programmable source I have so far:
>
> import vtk
> import os
> import csv
>
> csvfilename = '../../resources/vtk/testpos.csv'
> csvfilename= os.path.abspath(csvfilename)
>
> animationScene1 = GetAnimationScene()
> target = int(animationScene1.AnimationTime)
> print "Target value is: " + str(target)
>
> pos = [0.0, 0.0, 0.0]
> count=0
> with open(csvfilename) as f:
> r = csv.reader(f)
> for row in r:
> print "Current row :" + str(row)
> if count == target:
> print "found it"
> pos = [float(i) for i in row]
> break
> count += 1
> print "Pos is now : " + str(pos)
>
> s=vtk.vtkSphereSource()
> s.SetCenter(pos)
> s.SetRadius(10)
> s.Update()
>
>
> The csv stuff works as far as I can tell, but it gives an error because
> GetAnimationScene is not defined. However, there is another problem, even if
> I ignore all of the time dependent things and just try to create a sphere in
> this manner:
>
> import vtk
> s=vtk.vtkSphereSource()
> s.SetCenter([0,0,0])
> s.SetRadius(10)
> s.Update()
>
> there are no errors, but no sphere either! So I guess I am missing something
> fundamental about the difference between python scripting and programmable
> sources. Further, I wonder if it would be easier to change the position of
> an already created sphere source with a programmable filter instead?
>
> Has anybody solved a similar problem and can point me in the correct
> direction? Any help would be greatly appreciated.
>
> Kind regards,
>
> James
>
> _______________________________________________
> 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
>
>
>
>
--
Cory Quammen
R&D Engineer
Kitware, Inc.
More information about the ParaView
mailing list