One question about ParaView that comes up from time to time is how to define time-varying data sources with the Programmable Source. I’ll show the basics of doing this here.
Let’s start by looking at a simple example that produces a single static sphere. First, fire up ParaView and create a Programmable Source. The Script property for the Programmable Source in this example should be:
import vtk sphere = vtk.vtkSphereSource() sphere.SetRadius(1.0) sphere.Update() pd = self.GetPolyDataOutput() pd.ShallowCopy(sphere.GetOutput())
This creates a simple sphere source at the origin. Pretty boring, right?
Let’s make the sphere radius vary with time. To do this, we need to tell ParaView that our Programmable Source produces time-varying data. First, we tell the pipeline that the source has time information and define the time values in the Script (RequestInformation) property:
timeSteps = range(100) outInfo = self.GetOutputInformation(0) timeRange = [timeSteps, timeSteps[-1]] outInfo.Set(vtk.vtkStreamingDemandDrivenPipeline.TIME_RANGE(), timeRange, 2) outInfo.Set(vtk.vtkStreamingDemandDrivenPipeline.TIME_STEPS(), timeSteps, len(timeSteps))
Here, we define 100 timesteps [0, 1, 2, … 99]. We need to inform the pipeline about the time range as well as the actual time values themselves. To do this, we set the vtkStreamingDemandDrivenPipeline.TIME_RANGE() and vtkStreamingDemandDrivenPipeline.TIME_STEPS() information keys in the output information.
Next, we adjust our original Script property to modify the radius according to the time value. We retrieve the time value with the vtkStreamingDemandDrivenPipeline.UPDATE_TIME_STEP() key in the output information from the Programmable Filter and derive a radius value from it.
import vtk outInfo = self.GetOutputInformation(0) if outInfo.Has(vtk.vtkStreamingDemandDrivenPipeline.UPDATE_TIME_STEP()): time = outInfo.Get(vtk.vtkStreamingDemandDrivenPipeline.UPDATE_TIME_STEP()) else: time = 0 radius = math.sin(time * 2 * math.pi / 100) + 1.0 sphere = vtk.vtkSphereSource() sphere.SetRadius(radius) sphere.Update() pd = self.GetPolyDataOutput() pd.ShallowCopy(sphere.GetOutput())
When you click the Apply button on the Programmable Filter, you will notice that the number of time steps in the animation controls jumps to 100. If you click the play button, you will see a pulsing sphere.
Obviously, you can do a lot more with the timestep values. See, for example, section 13.2.2 in the ParaView User’s Guide for how to read series of CSV files where each one defines a different timestep.
Update 2016/1/5: To create an XML filter with a time-varying Programmable Source as outlined in this blog post, you will need to add one special property to the XML describing your source. Simply add this XML to your proxy XML description to let ParaView know your source provides time-varying data:
<DoubleVectorProperty information_only="1" name="TimestepValues" repeatable="1"> <Documentation> Available timestep values. </Documentation> </DoubleVectorProperty>