Rendering a few Lines takes an unreasonable amount of memory

Hello everyone, I ran into this issue while trying to plot a number of segments in Paraview: it needs much more memory than I was expecting.

Consider this script as a MWE:

from random import uniform

for i in range(600):
  l = Line(Point1=(uniform(0,100),uniform(0,100),0),Point2=(uniform(0,100),uniform(0,100),0))

which creates 600 random segments in the plane [0,100]x[0,100].
Now, I pasted it in the Python shell and it runs fine and adds to my pipeline the segments I want, but then if I select them all in the pipeline and click on the eye icon next to any of them to make them visible, Paraview tries to do what I want but eventually needs so much memory [15+GB] that my computer runs out of it and I have to stop it.

Is this behavior correct? Am I doing anything wrong?

Thanks for any help!

Info: I am running Paraview 5.5.0 64-bit with Qt 5.9.1 on Kubuntu 16.04.

ParaView can render very large data sets with millions of cells, points, or in your case line segments. However, data sources like the Line source in ParaView are heavy weight objects, and having 600 of them is not going to go well, as you have seen. What you want to do is add all your geometry in as few sources as possible.

To achieve your goal much more efficiently, it would be better to use a single Programmable Source that generates the 600 line segments and adds them to a single vtkPolyData. Set the Script argument to in the Programmable Source to:

import vtk
from random import uniform

points = vtk.vtkPoints()
lines = vtk.vtkCellArray()
for i in xrange(600):
  pt1 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  pt2 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  lines.InsertNextCell(2, [pt1, pt2])

output.SetPoints(points)
output.SetLines(lines)

This will produce just one ParaView source that generates 600 line segments. It will perform much, much faster and with much less memory.

Hi, thank you for your reply! I will give it a try and get back if I run into any other problem :slight_smile:

1 Like

Thought I’d chime in here and make a slight modification to @cory.quammen’s script.

I often find it necessary to have each line treated as their own entity rather than as different nonconnected cells in a single poly data object. For example, I deal with well logs quite often and need to treat each well as its own line/polyline data object and thus I use composite datasets.

Note: I do it this way because each well has its own spatial configuration and each well might be discretized differently.

An example of tackling this problem where you might want to keep each line/entity separate would be the following modifications to the above script where the Programmable Source’s output is set to vtkMultiBlockDataSet:

import vtk
from random import uniform

n = 600

# set the number of elements in your composite dataset
output.SetNumberOfBlocks(n)

for i in xrange(n):
  # Make your line segment: this could get way more complicated than a two point line
  poly = vtk.vtkPolyData()
  points = vtk.vtkPoints()
  lines = vtk.vtkCellArray()
  pt1 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  pt2 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  lines.InsertNextCell(2, [pt1, pt2])
  poly.SetPoints(points)
  poly.SetLines(lines)
  # Add that line to the composite dataset
  output.SetBlock(i, poly)
  # Give this element of the composite dataset a meaningful name
  output.GetMetaData(i).Set(vtk.vtkCompositeDataSet.NAME(), 'Line%.3d' % i)


Then you can select that composite data set in the Multi-block Inspector to see the individual components by name (View->Multi-block Inspector).

1 Like

I adapted this script to my needs and it worked perfectly, thanks a lot!

Thanks for the extra input, I didn’t need it this time but it might still be helpful in the future.

1 Like

Hi Sir,
I want to do same thing with only one difference: How can I specify a “width” to each line segment with this code?
Your help will be much appreciated,
Best Regards,
Hamid Rajabi.