Skip to content

PythonicAPI Examples

Warning

These examples only work with VTK Version: 9.3.20240428 or greater.

These examples:

  • Use the improved VTK Python interface. Some information about the improved Python interface can be found here
  • Are either newly crafted examples or upgrades of existing Python examples

See:

Upgrading an existing example to use the improved VTK Python interface

  1. Copy the example from the src/Python folder into the src/PythonicAPI folder maintaining the same path structure. If there is a corresponding markdown file, copy it.
  2. Copy the corresponding test image from src/Testing/Baseline/Python/ into src/Testing/Baseline/PythonicAPI/
  3. Edit src/PythonicAPI.md, possibly creating a table and headings to match the original example in src/Python.
  4. Upgrade the Python example.
  5. The associated markdown file (if any) may need checking to ensure any links in the document remain valid.
  6. Check everything is working and do a Merge Request.

Adding a new example

Follow the documented procedure ForDevelopers remembering that the folder to use is PythonicAPI.

VTK Classes Summary

This Python script, SelectExamples, will let you select examples based on a VTK Class and language. It requires Python 3.7 or later. The following tables will produce similar information.

Tutorials

Hello World

Example Name Description Image
A hello world example Cylinder example from the VTK Textbook and source code. A hello world example.

Simple Operations

Input and Output

Graph Formats

3D File Formats

Standard Formats

Input
Example Name Description Image
ReadAllPolyDataTypesDemo Read all VTK polydata file types.
ReadExodusData A simple script for reading and viewing ExodusII data interactively.
ReadSLC Read an SLC file.
TransientHDFReader Read transient data written inside a vtkhdf file.
Importers
Example Name Description Image
3DSImporter Import a 3D Studio scene that includes multiple actors.
ImportToExport Import a scene and optionally export the scene.
Output
Example Name Description Image
WritePLY
WriteSTL

VTK Formats

Input
Output

Legacy VTK Formats

Example Name Description Image
ReadLegacyUnstructuredGrid Read an unstructured grid that contains 11 linear cells.
WriteLegacyLinearCells Write each linear cell into a legacy UnstructuredGrid file (.vtk).
WriteXMLLinearCells Write each linear cell into an XML UnstructuredGrid file (.vtu).

Image Format

Input

Example Name Description Image
HDRReader Read a high-dynamic-range imaging file.
ReadDICOM Read DICOM file.
ReadDICOMSeries This example demonstrates how to read a series of DICOM images and scroll through slices

Output

Geometric Objects

Example Name Description Image
Dodecahedron Create a dodecahedron using vtkPolyhedron.
GeometricObjectsDemo
PipelineReuse How to reuse a pipeline.
Planes We create a convex hull of the planes for display purposes.
PlanesIntersection
SourceObjectsDemo Examples of source objects that procedurally generate polygonal models. These nine images represent just some of the capability of VTK. From upper left in reading order: sphere, cone, cylinder, cube, plane, text, random point cloud, disk (with or without hole), and line source. Other polygonal source objects are available; check subclasses of vtkPolyDataAlgorithm.

Cells

Example Name Description Image
CellTypeSource Generate tessellated cells.
ConvexPointSet Generate a ConvexPointSet cell.
LinearCellDemo Linear cell types found in VTK. The numbers define the ordering of the points making the cell.
Polyhedron Create an unstructured grid representation of a polyhedron (cube) and write it out to a file.

Sources

Example Name Description Image
EarthSource Create the Earth.
Frustum
OrientedArrow Orient an arrow along an arbitrary vector.
OrientedCylinder Orient a cylinder along an arbitrary vector.
PlatonicSolids All five platonic solids are displayed.
TessellatedBoxSource Generate a box with tessellated sides.

Non Linear

Example Name Description Image
IsoparametricCellsDemo Nonlinear isoparametric cell types in VTK.

Parametric Objects

Example Name Description Image
ParametricObjectsDemo Demonstrates the Parametric classes added by Andrew Maclean and additional classes added by Tim Meehan. The parametric spline is also included. Options are provided to display single objects, add backface, add normals and print out an image.
ParametricKuenDemo Interactively change the parameters for a Kuen Surface.

Implicit Functions and Iso-surfaces

Example Name Description Image
BooleanOperationImplicitFunctions Demonstrate booleans of two different implicit functions
ContourTriangulator Create a contour from a structured point set (image) and triangulate it.
DiscreteFlyingEdges3D Generate surfaces from labeled data.
ExtractData Implicit functions used to select data: Two ellipsoids are combined using the union operation used to select voxels from a volume. Voxels are shrunk 50 percent.
ExtractLargestIsosurface Extract largest isosurface.
IceCream How to use boolean combinations of implicit functions to create a model of an ice cream cone.
ImplicitQuadric Create an ellipsoid using an implicit quadric
ImplicitSphere Demonstrate sampling of a sphere implicit function
ImplicitSphere1 Demonstrate sampling of a sphere implicit function
Lorenz Visualizing a Lorenz strange attractor by integrating the Lorenz equations in a volume.
MarchingCubes Create a voxelized sphere.
SampleFunction Sample and visualize an implicit function.
SmoothDiscreteFlyingEdges3D Generate smooth surfaces from labeled data.

Working with 3D Data

Example Name Description Image
AlignTwoPolyDatas Align two vtkPolyData's.
BooleanPolyDataFilters This example performs a boolean operation (intersection, union or difference) of two PolyData using either a vtkBooleanOperationPolyDataFilter or a vtkLoopBooleanPolyDataFilter
Bottle Model a rotationally symmetric object.
CappedSphere Rotate an arc to create a capped sphere.
CellsInsideObject Extract cells inside a closed surface.
ConnectivityFilter Color any dataset type based on connectivity.
Curvatures Compute Gaussian, and Mean Curvatures.
CurvaturesAdjustEdges Get the Gaussian and Mean curvatures of a surface with adjustments for edge effects.
ExtractPolyLinesFromPolyData Extract polylines from polydata.
ExtractSelection Extract selected points.
ExtractSelectionCells Extract cell, select cell.
Finance Visualization of multidimensional financial data. The gray/wireframe surface represents the total data population. The red surface represents data points delinquent on loan payment.
FinanceFieldData Visualization of multidimensional financial data. The yellow surface represents the total data population. The red surface represents data points delinquent on loan payment.
Glyph2D
ImplicitPolyDataDistance
LineOnMesh Plot a spline on a terrain-like surface.
MeshLabelImageColor Mesh a single label from a label image. Then smooth and color the vertices according to the displacement error introduced by the smoothing.
PerlinNoise
PolyDataContourToImageData
PolyDataToImageDataStencil
RuledSurfaceFilter
SmoothMeshGrid Create a terrain with regularly spaced points and smooth it with vtkLoopSubdivisionFilter and vtkButterflySubdivisionFilter.
Spring Rotation in combination with linear displacement and radius variation.
WarpTo Deform geometry by warping towards a point.
WarpVector This example warps/deflects a line.

Data Types

Example Name Description Image
CompositePolyDataMapper
OverlappingAMR Demonstrates how to create and populate VTK's Overlapping AMR Grid type with data.

Data Type Conversions

Point Cloud Operations

Working with Meshes

This section includes examples of manipulating meshes.

Example Name Description Image
ClosedSurface Check if a surface is closed.
DeformPointSet Use the vtkDeformPointSet filter to deform a vtkSphereSource with arbitrary polydata.
DelaunayMesh Two-dimensional Delaunay triangulation of a random set of points. Points and edges are shown highlighted with sphere glyphs and tubes.
PointInterpolator Plot a scalar field of points onto a PolyData surface.

Clipping

Example Name Description Image
ClipDataSetWithPolyData Clip a vtkRectilinearGrid with arbitrary polydata. In this example, use a vtkConeSource to generate polydata to slice the grid, resulting in an unstructured grid.
ClipDataSetWithPolyData1 Clip a vtkRectilinearGrid with arbitrary polydata. In this example, use a vtkConeSource to generate polydata to slice the grid, resulting in an unstructured grid.
SolidClip Create a "solid" clip. The "ghost" of the part clipped away is also shown.

Working with Structured 3D Data

vtkImageData

Example Name Description Image
ImageWeightedSum Add two or more images.

vtkExplicitStructuredGrid

Example Name Description Image
CreateESGrid Create an explicit structured grid and convert this to an unstructured grid or vice versa.
LoadESGrid Load a VTU file and convert the dataset to an explicit structured grid.

vtkStructuredGrid

Example Name Description Image
BlankPoint Blank a point of a vtkStructuredGrid.
SGrid Creating a structured grid dataset of a semi-cylinder. Vectors are created whose magnitude is proportional to radius and oriented in tangential direction.

vtkStructuredPoints

Example Name Description Image
Vol Creating a image data dataset. Scalar data is generated from the equation for a sphere. Volume dimensions are 26 x 26 x 26.

vtkRectilinearGrid

Example Name Description Image
RGrid Creating a rectilinear grid dataset. The coordinates along each axis are defined using an instance of vtkDataArray.
VisualizeRectilinearGrid Visualize the cells of a rectilinear grid.

Working with Unstructured 3D Data

This section includes vtkUnstructuredGrid.

vtkUnstructuredGrid

Example Name Description Image
ClipUnstructuredGridWithPlane Clip a UGrid with a plane.
ClipUnstructuredGridWithPlane2 Clip a UGrid with a plane.

Registration

Medical

Example Name Description Image
GenerateCubesFromLabels Create cubes from labeled volume data.
GenerateModelsFromLabels Create models from labeled volume data.
MedicalDemo1 Create a skin surface from volume data.

Surface reconstruction

Utilities

Example Name Description Image
CheckVTKVersion Check the VTK version and provide alternatives for different VTK versions.
ClassesInLang1NotInLang2 Select VTK classes with corresponding examples in one language but not in another.
JSONColorMapToLUT Take a JSON description of a colormap and convert it to a VTK colormap.
ColorMapToLUT Use vtkDiscretizableColorTransferFunction to generate a VTK colormap.
RescaleReverseLUT Demonstrate how to adjust a colormap so that the colormap scalar range matches the scalar range on the object. You can optionally reverse the colors.
ResetCameraOrientation Reset camera orientation to a previously saved orientation.
SaveSceneToFieldData Save a vtkCamera's state in a vtkDataSet's vtkFieldData and restore it.
SaveSceneToFile Save a vtkCamera's state in a file and restore it.
Screenshot
SelectExamples Given a VTK Class and a language, select the matching examples.
ShareCamera Share a camera between multiple renderers.
VTKWithNumpy
XMLColorMapToLUT Take an XML description of a colormap and convert it to a VTK colormap.

Arrays

Example Name Description Image
ArrayToTable Convert a vtkDenseArray to a vtkTable.
ArrayWriter Write a DenseArray or SparseArray to a file.

Events

Math Operations

Graphs

Example Name Description Image
AdjacencyMatrixToEdgeTable Convert an adjacency matrix to an edge table.
ConstructTree Construct a tree.
CreateTree Create a tree and label the vertices and edges.
GraphToPolyData Convert a graph to a PolyData.
LabelVerticesAndEdges Label vertices and edges.
RandomGraphSource Create a random graph.
SideBySideGraphs Display two graphs side by side.
ScaleVertices Size/scale vertices based on a data array.
SelectedVerticesAndEdges
VisualizeDirectedGraph Visualize a directed graph.

Graph Conversions

Data Structures

Timing Demonstrations

KD-Tree

Oriented Bounding Box (OBB) Tree

Octree

Modified BSP Tree

HyperTreeGrid

Example Name Description Image
HyperTreeGridSource Create a vtkHyperTreeGrid.

VTK Concepts

Rendering

Example Name Description Image
ColoredSphere A simple sphere.
GradientBackground Demonstrates the background shading options.
MotionBlur Example of motion blur.
OutlineGlowPass Demonstrates how to render a object in a scene with a glowing outline.
PBR_Anisotropy Render spheres with different anisotropy values.
PBR_Clear_Coat Render a cube with custom texture mapping and a coat normal texture.
PBR_Edge_Tint Render spheres with different edge colors using a skybox as image based lighting.
PBR_HDR_Environment Renders spheres with different materials using a skybox as image based lighting.
PBR_Mapping Render a cube with custom texture mapping.
PBR_Materials Renders spheres with different materials using a skybox as image based lighting.
PBR_Materials_Coat Render spheres with different coat materials using a skybox as image based lighting.
PBR_Skybox Demonstrates physically based rendering, a skybox and image based lighting.
PBR_Skybox_Texturing Demonstrates physically based rendering, a skybox, image based lighting and texturing.
PBR_Skybox_Anisotropy Demonstrates physically based rendering, a skybox, image based lighting, and anisotropic texturing.
StripFran Triangle strip examples. (a) Structured triangle mesh consisting of 134 strips each of 390 triangles. (b) Unstructured triangle mesh consisting of 2227 strips of average length 3.94, longest strip 101 triangles. Images are generated by displaying every other triangle strip.
TransformSphere The addition of a transform filter to ColoredSphere.

Lighting

Example Name Description Image
ShadowsLightsDemo Show lights casting shadows.

Texture Mapping

Example Name Description Image
AnimateVectors One frame from a vector field animation using texture maps.
TextureCutQuadric Cut a quadric with boolean textures.
TextureCutSphere Examples of texture thresholding using a boolean combination of two planes to cut nested spheres.
TexturePlane Example of texture mapping.
TextureThreshold Demonstrate the use of scalar thresholds to show values of flow density on three planes.

Tutorial

If you are new to VTK then these tutorials will help to get you started.

Visualization

See this tutorial for a brief explanation of the VTK terminology of mappers, actors, etc.

Example Name Description Image
AnnotatedCubeActor Annotated cube.
AssignCellColorsFromLUT Demonstrates how to assign colors to cells in a vtkPolyData structure using lookup tables.
BillboardTextActor3D Label points with billboards.
Blow Ten frames from a blow molding finite element analysis.
CameraModel1 Illustrate camera movement around the focal point.
CameraModel2 Illustrate camera movement centered at the camera position.
CaptionActor2D Draw a caption/bubble pointing to a particular point.
ClipSphereCylinder A plane clipped with a sphere and an ellipse. The two transforms place each implicit function into the appropriate position. Two outputs are generated by the clipper.
ColoredAnnotatedCube How to color the individual faces of an annotated cube.
CollisionDetection Collison between two spheres.
CombineImportedActors Combine actors from a scene into one actor.
CreateBFont A scanned image clipped with a scalar value of 1/2 its maximum intensity produces a mixture of quadrilaterals and triangles.
CubeAxesActor Display three orthogonal axes with with labels.
CurvaturesNormalsElevations Gaussian and Mean curvatures of a surface with arrows colored by elevation to display the normals.
DataSetSurface Cutting a hexahedron with a plane. The red line on the surface shows the cut.
DisplacementPlot Show modal lines for a vibrating beam.
FlyingHeadSlice Flying edges used to generate contour lines.
FroggieSurface Construct surfaces from a segmented frog dataset. Up to fifteen different surfaces may be extracted. You can turn on and off surfaces and control the camera position.
FroggieView View surfaces of a segmented frog dataset using preprocessed *.vtk tissue files. You can turn on and off surfaces, control their opacity through the use of sliders and control the camera position.
Hanoi Towers of Hanoi.
HanoiInitial Towers of Hanoi - Initial configuration.
HanoiIntermediate Towers of Hanoi - Intermediate configuration.
HeadBone Marching cubes surface of human bone.
HyperStreamline Example of hyperstreamlines, the four hyperstreamlines shown are integrated along the minor principal stress axis. A plane (colored with a different lookup table) is also shown.
IsosurfaceSampling Demonstrates how to create point data on an isosurface.
Kitchen Demonstrates stream tracing in a kitchen.
Office Using random point seeds to create streamlines.
OfficeA Corresponds to Fig 9-47(a) in the VTK textbook.
OfficeTube The stream polygon. Sweeping a polygon to form a tube.
PineRootConnectivity Applying the connectivity filter to remove noisy isosurfaces.
PineRootConnectivityA The isosurface, with no connectivity filter applied.
PineRootDecimation Applying the decimation and connectivity filters to remove noisy isosurfaces and reduce data size.
PlateVibration Demonstrates the motion of a vibrating beam.
PointDataSubdivision Demonstrates the use of the vtkLinearSubdivisionFilter and vtkButterflySubdivisionFilter.
ProgrammableGlyphFilter Generate a custom glyph at each point.
ProgrammableGlyphs Generate programmable glyphs.
PseudoVolumeRendering Here we use 20 cut planes, each with an opacity of of 0.25. They are then rendered back-to-front to simulate volume rendering.
QuadricVisualization Visualizing a quadric function.
StreamlinesWithLineWidget Using the vtkLineWidget to produce streamlines in the combustor dataset. The StartInteractionEvent turns the visibility of the streamlines on; the InteractionEvent causes the streamlines to regenerate themselves.
TensorEllipsoids Display the scaled and oriented principal axes as tensor ellipsoids representing the stress tensor.
WarpCombustor Carpet plots of combustor flow energy in a structured grid. Colors and plane displacement represent energy values.

Working with vtkImageData

Example Name Description Image
ImageNormalize Normalize an image.
WriteReadVtkImageData Generate, edit and read out vtk image data.

Volume Rendering

User Interaction

Example Name Description Image
CallBack Setting up a callback with client data. Two different methods are demonstrated.
CellPicking Cell Picking.
HighlightPickedActor Pick and highlight an actor based on mouse clicks.
HighlightWithSilhouette Highlight a picked actor by adding a silhouette.
InteractorStyleTrackballActor
InteractorStyleTrackballCamera
MouseEvents Subclass the interactor style.
MouseEventsObserver Use an observer.

Working with Images

Example Name Description Image
BackgroundImage Display an image as the "background" of a scene, and render a superquadric in front of it.
MarkKeypoints Mark keypoints in an image.

Image Processing

Example Name Description Image
Attenuation This MRI image illustrates attenuation that can occur due to sensor position. The artifact is removed by dividing by the attenuation profile determined manually.
EnhanceEdges High-pass filters can extract and enhance edges in an image. Subtraction of the Laplacian (middle) from the original image (left) results in edge enhancement or a sharpening operation (right).
GaussianSmooth Low-pass filters can be implemented as convolution with a Gaussian kernel.
HybridMedianComparison Comparison of median and hybrid-median filters. The hybrid filter preserves corners and thin lines, better than the median filter.
IdealHighPass This figure shows two high-pass filters in the frequency domain. The Butterworth high-pass filter has a gradual attenuation that avoids ringing produced by the ideal high-pass filter with an abrupt transition.
ImageGradient Create an imaging pipeline to visualize gradient information.
IsoSubsample This figure demonstrates aliasing that occurs when a high-frequency signal is subsampled. High frequencies appear as low frequency artifacts. The left image is an isosurface of a skull after subsampling. The right image used a low-pass filter before subsampling to reduce aliasing.
MorphologyComparison This figure demonstrates various binary filters that can alter the shape of segmented regions.
ImageWarp Combine the imaging and visualization pipelines to deform an image in the z-direction. The vtkMergeFilter is used to combine the warped surface with the original color data.
Pad Convolution in frequency space treats the image as a periodic function. A large kernel can pick up features from both sides of the image. The left image has been padded with a constant to eliminate wraparound during convolution. On the right, mirror padding has been used to remove artificial edges introduced by borders.
VTKSpectrum The discrete Fourier transform changes an image from the spatial domain into the frequency domain, where each pixel represents a sinusoidal function. This figure shows an image and its power spectrum displayed using a logarithmic transfer function.

Widgets

Example Name Description Image
AffineWidget Apply an affine transformation interactively.
BalloonWidget Uses a vtkBalloonWidget to draw labels when the mouse stays above an actor.
BorderWidget 2D selection, 2D box.
BoxWidget This 3D widget defines a region of interest that is represented by an arbitrarily oriented hexahedron with interior face angles of 90 degrees (orthogonal faces). The object creates 7 handles that can be moused on and manipulated.
BoxWidget2 This 3D widget defines a region of interest that is represented by an arbitrarily oriented hexahedron with interior face angles of 90 degrees (orthogonal faces). The object creates 7 handles that can be moused on and manipulated. vtkBoxWidget2 and vtkBoxRepresentation are used in this example.
CompassWidget Draws an interactive compass.
ContourWidget Draw a contour (line) which can be deformed by the user.
ImplicitPlaneWidget2 Clip polydata with an implicit plane.
SphereWidget This 3D widget defines a sphere that can be interactively placed in a scene.
SplineWidget This example shows how to use vtkSplineWidget with a callback being used to get the length of the spline widget.

Plotting

Example Name Description Image
MultiplePlots Display multiple plots by using viewports in a single render window.
ScatterPlot Scatter plot.
SpiderPlot Spider plot.
SurfacePlot Surface plot.

Animation

Example Name Description Image
Animation Move a sphere across a scene.

Annotation

Example Name Description Image
TextOrigin This example demonstrates the use of vtkVectorText and vtkFollower. vtkVectorText is used to create 3D annotation.

InfoVis

Example Name Description Image
ParallelCoordinatesView How to use Parallel Coordinates View to plot and compare data set attributes.

PyQt