VTK/Examples/Python/vtkUnstructuredGridReader

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to: navigation, search

The following example demonstrates how to read an unstructured grid defined at a (legacy) *.vtk file and how to render it with VTK.

An example input file is uGridEx.vtk (folder Data at VTKData)

UnstructuredGridReader.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# by Panos Mavrogiorgos, email: pmav99 <> gmail
 
from vtk import *
 
# The source file
file_name = "uGridEx.vtk"
 
# Read the source file.
reader = vtkUnstructuredGridReader()
reader.SetFileName(file_name)
reader.Update() # Needed because of GetScalarRange
output = reader.GetOutput()
scalar_range = output.GetScalarRange()
 
# Create the mapper that corresponds the objects of the vtk file
# into graphics elements
mapper = vtkDataSetMapper()
mapper.SetInput(output)
mapper.SetScalarRange(scalar_range)
 
# Create the Actor
actor = vtkActor()
actor.SetMapper(mapper)
 
# Create the Renderer
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1) # Set background to white
 
# Create the RendererWindow
renderer_window = vtkRenderWindow()
renderer_window.AddRenderer(renderer)
 
# Create the RendererWindowInteractor and display the vtk_file
interactor = vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderer_window)
interactor.Initialize()
interactor.Start()