Python VTK script to Display 3D xyz data

This Plot3 python matplotlib script is useful. But there are times when all I have is a txt or csv file with the xyz data and I just want to see the 3d view. Firing up python IDE , loading the data and using the plot3 script becomes too many steps. And the output 3d plot’s handling leave much to be desired.

So building on VTK docs python examples, have developed this python script to display 3D xyz data from a file.

The simple command is python xyzviewer.py filename

That’s it and you get a VTK window with interactive display of the data. Of course needs VTK installed in python.

Here’s a short 9 sec demo of the 3D display.

And if you are still curious, you can look at the plot3 here.

Update: If the formatting fails you, download vtkpointsdisplay from here.

And here’s the updated code for anyone interested. Thanks Ralv and Samir.

''' 
Modified Python 3 VTK script to Display 3D xyz data
'''

import vtk
from numpy import random,genfromtxt,size
      
class VtkPointCloud:
    def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e6):
        self.maxNumPoints = maxNumPoints
        self.vtkPolyData = vtk.vtkPolyData()
        self.clearPoints()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputData(self.vtkPolyData)
        mapper.SetColorModeToDefault()
        mapper.SetScalarRange(zMin, zMax)
        mapper.SetScalarVisibility(1)
        self.vtkActor = vtk.vtkActor()
        self.vtkActor.SetMapper(mapper)
 
    def addPoint(self, point):
        if (self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints):
            pointId = self.vtkPoints.InsertNextPoint(point[:])
            self.vtkDepth.InsertNextValue(point[2])
            self.vtkCells.InsertNextCell(1)
            self.vtkCells.InsertCellPoint(pointId)
        else:
            r = random.randint(0, self.maxNumPoints)
            self.vtkPoints.SetPoint(r, point[:])
        self.vtkCells.Modified()
        self.vtkPoints.Modified()
        self.vtkDepth.Modified()
 
    def clearPoints(self):
        self.vtkPoints = vtk.vtkPoints()
        self.vtkCells = vtk.vtkCellArray()
        self.vtkDepth = vtk.vtkDoubleArray()
        self.vtkDepth.SetName('DepthArray')
        self.vtkPolyData.SetPoints(self.vtkPoints)
        self.vtkPolyData.SetVerts(self.vtkCells)
        self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
        self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
 
def load_data(filename,pointCloud):
    data = genfromtxt(filename,dtype=float,usecols=[0,1,2])
     
    for k in range(size(data,0)):
        point = data[k] #20*(random.rand(3)-0.5)
        pointCloud.addPoint(point)
         
    return pointCloud
 
 
if __name__ == '__main__':
    import sys
 
 
    if (len(sys.argv) < 2):
         print ('Usage: xyzviewer.py itemfile')
         sys.exit()
    pointCloud = VtkPointCloud()
    pointCloud=load_data(sys.argv[1],pointCloud)
 
 
# Renderer
    renderer = vtk.vtkRenderer()
    renderer.AddActor(pointCloud.vtkActor)
#renderer.SetBackground(.2, .3, .4)
    renderer.SetBackground(0.0, 0.0, 0.0)
    renderer.ResetCamera()
 
# Render Window
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
 
# Interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
 
# Begin Interaction
    renderWindow.Render()
    renderWindow.SetWindowName("XYZ Data Viewer"+sys.argv[1])
    renderWindowInteractor.Start()
Advertisement

20 thoughts on “Python VTK script to Display 3D xyz data

  1. Hello,

    This seem like a very useful code. During execution it complains about syntax in line 25. Any help will be kindly appreciated.

    Cheers,
    Senu

    Like

      • Sukhbinder – Thanks for sharing the outcome of your efforts with the rest of us. This seems like exactly what I need for some hobby experimentation. I am a Python newbie and just use it for some simple scripting. Grabbing and running the code from your post above required quite some formatting to make it work in my environment, but I think I have it going now. However, it complains with the message : VtkPointCloud instance has no attribute ‘clearPoints’. Not sure where to go from here. Can you email me the source code, so I know the formatting is correct. Thanks again.

        /DC

        Like

      • Ignore earlier message. I got it working. More formatting errors when I cut and pasted from Wordpess. Fixed now. Thanks.

        /DC

        Like

  2. hello!

    I have the XYZ data of 10 particles at 100 time steps (all in a .csv file).
    Now I wish to simulate the movement of these particles using ParaView, but for that I think I would have to first convert my data in a VTK format.
    How do I go about doing this?

    Any help would be great!
    Thanks!
    Rohit.

    Like

  3. Pingback: Displaying Truss | SukhbinderSingh.com

  4. Pingback: Visualizing 3d Triangles With Pure Matplotlib Function | SukhbinderSingh.com

    • Hi Andrey.

      Thanks for reading. The input file looks like the following

      # comment 1
      # comment 2
      0.0 1.0 2.0
      1.0 2.0 3.0

      So the idea was the xyz file has 2 lines of comment.

      You can modify the line using genfromtxt to suit the input you have.

      Hope this helps.

      Like

  5. Please consider updating this post,
    The code provided here must be modified as you can see clearly wordpress replace quotes by the word: &quotes; it replace the less than sign by; &lt
    After reformatting the code, the script fails because it is outdated (some class method no more exist, …etc)
    The link given to dropbox is broken

    Like

  6. I saw your post it is really helpful.
    In case we do not have a vtk file and want to create a vtk dataset file using python. how can we do that ?
    I tried most everything in the web (I presume I am going somewhere wrong if, you can provide a brief understanding regarding how to create a file and then add a scalar value to it would be helpful). Its basic so, need a clear understanding of it.
    Your help will be highly appreciated.

    Like

  7. I keep finding this problem. It says it doesn’t recognize the SetInput attribute of the mapper. I run this in visual studio. The only changes I’ve made is replace the &lt with the correct symbols ” python displayxyz.py caixa1.pcd
    Traceback (most recent call last):
    File “displayxyz.py”, line 59, in
    pointCloud = VtkPointCloud()
    File “displayxyz.py”, line 12, in __init__
    mapper.SetInput(self.vtkPolyData)
    AttributeError: ‘vtkmodules.vtkRenderingOpenGL2.vtkOpenGLPolyDataMa’ object has no attribute ‘SetInput’

    I really need your help, can you tell me whats wrong?
    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s