# Color imported lines by direction

**URL:** https://discourse.paraview.org/t/color-imported-lines-by-direction/4813
**Category:** ParaView Support
**Tags:** vtk
**Created:** [July 10, 2020, 7:08pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813 "2020-07-10T19:08:35Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 10, 2020, 7:08pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/1 "2020-07-10T19:08:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 8:35am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/2 "2020-07-13T08:35:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 14, 2020, 6:19pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/3 "2020-07-14T18:19:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 15, 2020, 9:27pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/4 "2020-07-15T21:27:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 15, 2020, 11:02pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/5 "2020-07-15T23:02:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 16, 2020, 7:33am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/6 "2020-07-16T07:33:06Z")

</div>

[https://www.paraview.org/Wiki/Python\_Programmable\_Filter](https://www.paraview.org/Wiki/Python_Programmable_Filter)

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 16, 2020, 7:47am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/7 "2020-07-16T07:47:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 16, 2020, 7:55am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/8 "2020-07-16T07:55:49Z")

</div>

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

I did not understand your previous question.

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 16, 2020, 4:40pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/9 "2020-07-16T16:40:53Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 17, 2020, 3:43am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/10 "2020-07-17T03:43:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 17, 2020, 7:37am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/11 "2020-07-17T07:37:51Z")

</div>

Yes, rendering is managed by ParaView.

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 25, 2020, 10:01pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/12 "2020-07-25T22:01:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Kenneth\_Moreland](https://discourse.paraview.org/user_avatar/discourse.paraview.org/kenneth_moreland/32/15033_2.png) [@Kenneth\_Moreland](https://discourse.paraview.org/u/Kenneth_Moreland)
#### Post date: [July 27, 2020, 2:19pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/13 "2020-07-27T14:19:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 27, 2020, 11:42pm UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/14 "2020-07-27T23:42:48Z")

</div>

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.

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

---

<div class="post-metadata">

### Author: ![lrfrank](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lrfrank](https://discourse.paraview.org/u/lrfrank)
#### Post date: [July 28, 2020, 12:10am UTC](https://discourse.paraview.org/t/color-imported-lines-by-direction/4813/15 "2020-07-28T00:10:26Z")

</div>

(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.)
