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 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 here’s the code for anyone interested.

# -*- coding: utf-8 -*-
Created on Wed Sep 11 17:53:12 2013

@author: Sukhbinder

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()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetScalarRange(zMin, zMax)
        self.vtkActor = vtk.vtkActor()

    def addPoint(self, point):
        if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
            pointId = self.vtkPoints.InsertNextPoint(point[:])
            r = random.randint(0, self.maxNumPoints)
            self.vtkPoints.SetPoint(r, point[:])

    def clearPoints(self):
        self.vtkPoints = vtk.vtkPoints()
        self.vtkCells = vtk.vtkCellArray()
        self.vtkDepth = vtk.vtkDoubleArray()

def load_data(filename,pointCloud):
    data = genfromtxt(filename,dtype=float,skiprows=2,usecols=[0,1,2])
    for k in xrange(size(data,0)):
        point = data[k] #20*(random.rand(3)-0.5)
    return pointCloud

if __name__ == '__main__':
    import sys

    if len(sys.argv) < 2:
         print 'Usage: itemfile'
    pointCloud = VtkPointCloud()

# Renderer
    renderer = vtk.vtkRenderer()
#renderer.SetBackground(.2, .3, .4)
    renderer.SetBackground(0.0, 0.0, 0.0)

# Render Window
    renderWindow = vtk.vtkRenderWindow()

# Interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()

# Begin Interaction
    renderWindow.SetWindowName("XYZ Data Viewer:"+sys.argv[1])

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

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


14 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.



      • 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.



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



  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!


  3. Pingback: Displaying Truss |

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

    • 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.


  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


    • Thanks Samir for bringing this to my notice.. vtk has fallen out of my radar for last couple of years. I will find time and update. Now why isn’t the Dropbox link working, that strange…


  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.


Leave a Reply

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

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

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s