Color imported lines by direction

Hi,
I’m new to Paraview but have searched for this topic.

I have a set of streamlines already generated by a different program, and save in vtk form. Paraview reads them in correctly. I would like to now use a tube filter and have the lines(tubes) colored by direction. It seems that Paraview can do this if you give it a velocity field and it generates the lines internally, but I don’t see how to do this if I’ve already generated the lines and am importing them.

Thank you!
Larry

If the velocities are not present in your generated streamlines, you are out of luck. ParaView can’t guess them.

You could compute the direction of each line, but that would require to use a programmable filter and implement it in Python.

I’m only interested in coloring by direction, which Paraview can (could) determine from each line segment my vtk file contains. Is that possible?

On the other hand, a programmable filter in Python doesn’t sound to hard - I’m new to Paraview, though, and have never done that. I’ll check the documentation but if there’s a particular place in the documentation you can point me to, that would help.

Thanks,
Larry

Mathieu

I’ve been trying to implement a Programmable Filter. My input is a vtk dataset that has both points and lines (so applying a Tube filter directly in the Paraview interface works).

I’m first trying to just recreate the creation of the tube filter in the programmable filter before I move on to trying to color the lines differently.
However I can’t get that to work. The code below execute without error but I don’t know how to output the result so that paraview displays it. That is, I know there should be something like

pdo.SetPoints(tubes)

but this is not correct (tubeFilter doesn’t produce points. But I’ve searched the web (and looked at paraview docs) and run out of guesses.

Thanks,
Larry

Mathieu,

Never mind - I found an obscure example somewhere that showed the answer to be:

pdo = self.GetOutputDataObject(0)
pdo.ShallowCopy(tubeFilter.GetOutput())

Is there someplace on the Paraview website that has this type of information? I couldn’t find it in any of the Programmable Filter examples or documentation.

Larry

https://www.paraview.org/Wiki/Python_Programmable_Filter

Mathieu

Yes, I saw these examples. I’m able to create the tube and a test coloring scheme.
However, it doesn’t appear that the renderer that I add to the Programmable Filter does anything (code below).

No error message, but no colors either.

Also, how do you save the Programmable Filter? I’ve searched all over for that as well. I will be calling the same filter over and over, and certainly don’t want to retype is each time.
I assume there’s a way to save and recall it easily?

Best,
Larry

You can save it in a custom filter. Tools -> CustomFilter.

I did not understand your previous question.

Thanks, that’s helpful.

My previous question is related to the attached script.

I’m able to load the data, and create a tube filter around it.
And I’m able to access the cell points which will allow me to find the line directions (although I haven’t done that below - I just created a quick colormap to test the color mapping).

But the ‘mapper’ and ‘renderer’ below don’t seem to do anything. The tube have no color still (just white).

Thanks for you help,
Larry

import math
import numpy as np
import vtk

print("\n ************ Programmable Filter ************\n")

pdi = self.GetPolyDataInput()
numPoints=pdi.GetNumberOfPoints()
print(“number of points = %d” %numPoints)
points = pdi.GetPoints()
numLines=pdi.GetNumberOfLines()
print(“number of lines = %d” %numLines)
lines = pdi.GetLines()
ncells = lines.GetNumberOfCells()
print(“number of cells = %d” %ncells)

n=5;
bounds = np.zeros(6)

for i in range(n):
curPt = points.GetPoint(i)
print i, curPt
cell = pdi.GetCell(i);

pdi.GetCellBounds(i,bounds)
print “i,cell,cellBounds=”, i, cell
#cellBounds = cell.GetBounds()
#print “i,cell,cellBounds=”, i, cell, cellBounds

print "bounds = "
for j in range(6):
print j,bounds[j]

RBG array (could add Alpha channel too I guess…)

Varying from blue to red

colors = vtk.vtkUnsignedCharArray()
colors.SetName(“Colors”);
colors.SetNumberOfComponents(3);
colors.SetNumberOfTuples(ncells);
for i in range(ncells):
colors.InsertTuple3(i,
int(255 * i/ (ncells - 1)),
0,
int(255 * (ncells - 1 - i)/(ncells - 1)) );

pdi.GetPointData().AddArray(colors);

Create tube filter

tubeFilter=vtk.vtkTubeFilter()
tubeFilter.SetDebug(1)
tubeFilter.SetInputData(pdi)
tubeFilter.SetRadius(0.25)
tubeFilter.SetNumberOfSides(50)

#tubeFilter.GetPointData().AddArray(colors);

print("\n*** vtkTubeFilter output:\n")
print tubeFilter

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(tubeFilter.GetOutputPort());
mapper.ScalarVisibilityOn();
mapper.SetScalarModeToUsePointFieldData();
mapper.SelectColorArray(“Colors”);

tubeFilter.Update()

actor = vtk.vtkActor()
actor.SetMapper(mapper)

pdo = self.GetOutputDataObject(0)
pdo.ShallowCopy(tubeFilter.GetOutput())
self.renderer = vtk.vtkRenderer()
self.renderer.AddActor(actor)

I see now … the idea is to just add scalars (colors) to the polydata and then they appear in the Coloring properties in the Paraview GUI, and those make from lines to tubes using the standard tube filter AFTER the programmable filter.

1 Like

Yes, rendering is managed by ParaView.

I now have the colored tubes working.

Now I am loading a series of individual netcdf files that are actually consecutive time points. (This is a different type of data than the tube data - I just want to do contouring, which works fine when I load a single file) After reading the documentation, I renamed the files into the form

file_0100.nc
file_0110.nc
….
file_0250.nc

and Paraview now reads them into a group. However, it does not recognize any time coordinate. There are several posts on this and as far as i can tell, what I have done is the correct way to read in a time series. I don’t care about the actual time units so consecutive time points of t=1,2,3, …n in Paraview is fine. But it’s not recognizing time at all.

Thanks,
Larry

I’m not sure that the “general” (CF convention) netCDF reader supports reading in a series of files as a time series. The typical convention is to write out the time series as a dimension in the file (unlike, for example, the vtk format that writes each time step in a separate file).

For now, to support a file series you would have to convert the files to a format that ParaView reads as a file time series such as .vtk. Or write your data into a netCDF file with a time dimension.

It should be possible to convert the netCDF reader to also support a file series for time (i.e. wrap it into a FileSeriesReader object). I think that will work but there might be issues with both the reader and metareader specifying time. We could open an issue request for that.

Hi Ken,
Thanks for the reply. I figured out the problem, although it was not exactly clear from the documentation. It appears that in order for Paraview to read in and display separate netCDF files correctly, a few specific files traits need to be in place:

  1. files need to have the naming structure

file_1.nc
file_2.nc

file_n.nc

so that Paraview will read them as a group.

  1. The time dimension must be

time = UNLIMITED

  1. Time must be a variable with dimensions e.g.

    float time(time) ;
    time:units = “Seconds” ;

  2. The data for the variable time must be different for each file.

eg for file_1.nc
data:
time = 10

eg for file_2.nc
data:
time = 15

etc

Now that this is working, it brings me to the related questions. For my n netCDF files I have n associated vtk files. Paraview appears to be able to read those in as a group automatically and assigns them arbitrary time values of 1,2,3,…n.

Unfortunately, for them to sync (ie display with) the correct netCDF files, they need to have time variables identical to the corresponding netCDF files (eg time = 10,15, etc in the example above). Is there a way to give a vtk file a time variable?

Thanks,
Larry

(Just to be clear, in the last sentence I said “time variables identical” but what I meant is that the vtk files must have time ‘data values’ that are the same as the time ‘data values’ in the netCDF files.)