Skip to content

PythonicAPI Examples

These examples use the improved VTK Python interface. Some information about the improved Python interface can be found here

The examples here are either newly crafted ones or upgraded existing Python examples using the improved VTK Python interface.

See More Pythonic VTK wrapping for the VTK Discourse discussion and Wrap VTK properties to pythonic properties with snake_case names for the merge request.

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.
Importers
Output

VTK Formats

Input
Output

Legacy VTK Formats

Example Name Description Image
ReadLegacyUnstructuredGrid Read an unstructured grid that contains 11 linear cells.

Image Format

Input

Example Name Description Image
HDRReader Read a high-dynamic-range imaging file.

Output

Geometric Objects

Example Name Description Image
GeometricObjectsDemo
PipelineReuse How to reuse a pipeline.
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

Sources

Non Linear

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
DiscreteFlyingEdges3D Generate surfaces from labeled data.
ImplicitQuadric Create an ellipsoid using an implicit quadric
ImplicitSphere Demonstrate sampling of a sphere implicit function
ImplicitSphere1 Demonstrate sampling of a sphere implicit function
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
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.
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.
SmoothMeshGrid Create a terrain with regularly spaced points and smooth it with vtkLoopSubdivisionFilter and vtkButterflySubdivisionFilter.

Data Types

Example Name Description Image
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
DelaunayMesh Two-dimensional Delaunay triangulation of a random set of points. Points and edges are shown highlighted with sphere glyphs and tubes.

Clipping

Example Name Description Image
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

Registration

Medical

Example Name Description Image
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.
ColorMapToLUT_JSON Take a JSON description of a colormap and convert it to a VTK colormap.
ColorMapToLUT_XML Take an XML 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

Arrays

Events

Math Operations

Graphs

Example Name Description Image
SideBySideGraphs Display two graphs side by side.

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.
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.
TransformSphere The addition of a transform filter to ColoredSphere.

Lighting

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.
ColoredAnnotatedCube How to color the individual faces of an annotated cube.
CurvaturesNormalsElevations Gaussian and Mean curvatures of a surface with arrows colored by elevation to display the normals.
QuadricVisualization Visualizing a quadric function.
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.

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.

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

Plotting

Example Name Description Image
MultiplePlots Display multiple plots by using viewports in a single render window.
ScatterPlot Scatter plot.
SpiderPlot Spider 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

PyQt