VTK commands

A computer terminal with two monitors in a dark room.

Important VTK methods and functions.

This is article 9 in a series on VTK. The first in this series can be found here. The following are important methods and functions used in this series.

Command Description

Fundamental commands

vtk.vtkConeSource()

Create a cone polygon mesh.

mapper.SetInputConnection(cone.GetOutputPort())

Connect the output port of the cone to the input connection of mapper. This is a common pattern in VTK to connect outputs to inputs and build a pipeline.

actor.SetMapper(mapper)

Set mapper for the vtkActor named actor.

window.SetSize(x, y)

Set the window dimensions in pixels to x wide and y tall.

interactor.SetRenderWindow(window)

Set interactor for use with the render window.

window.AddRenderer(renderer)

Add the renderer to the window.

renderer.SetViewport( (x0, y0, x1, y1) )

Set the renderer to draw in the section of the window with bottom-left corner at (x0, y0) and top-right corner at (x1, y1). These values are ratios in the range [0.0, 1.0]. Default is (0.0, 0.0, 1.0, 1.0).

renderer.SetBackground(R, G, B)

Set the background colour to the RGB colour defined by (R, G, B).

Polygon mesh commands

points.InsertNextPoint( (x, y, z) )

Insert the next point at position (x, y, z) into the vtkPoints instance.

tri.GetPointIds().SetId(i, j)

For the vtkTriangle instance tri, set the local ID i for this triangle to the global ID j of the mesh in which tri is a cell. NOTE: this command is the same for other polygon cells, including vtkQuad.

data.SetNumberOfComponents(1)

For the vtkDoubleArray data set the number of components of each piece of data to 1.

data.InsertNextTuple([val])

For the vtkDoubleArray data set the next tuple value to the float val.

mesh.GetCellData().SetScalars(data) mesh.GetPointData().SetScalars(data)

For the vtkPolyData instance mesh set the cell/point data to the vtkDoubleArray instance data.

Lookup Tables

actor.GetProperty().SetColor(r, g, b)

For actor set the colour of the entire model of that actor to a single colour, specificied by the RGB values (r, g, b).

vtk.vtkPolyDataMapper()

Create an instance of vtkPolyDataMapper, a lookup table that can be applied to a polygon mesh.

lut.SetTableRange(low, high)

Set the range of the lookup table from low to high.

mapper.SetLookupTable(lut)

Connect the lookup table lut to the data mapped by mapper.

mapper.SetUseLookupTableScalarRange(True)

The data range in the lookup table set to mapper is used. In most cases this method should be called when modified lookup tables are used.

lut.Build()

After setting up a lookup table lut this method updates the lookup table to reflect the new parameters. Needs to be called once a lookup table has been setup.

lut.SetAboveRangeColor(colour) lut.SetBelowRangeColor(colour)

Set the colour to use for values that are outside of the range of the lookup table. Here colour can be 4 parameters or a list of length 4. These are the RGB values of the colour and the alpha channel as the 4th parameter. Must use in combination with .UseXXXXXRangeColorOn() to activate use of this colour.

lut.UseBelowRangeColorOn() lut.UseBelowRangeColorOff() lut.UseAboveRangeColorOn() lut.UseAboveRangeColorOff()

For lookup table lut controls whether to use a custom colour for values outside of the range. Set off by default.

lut.SetHueRange(low, high) lut.SetSaturationRange(low, high) lut.SetValueRange(low, high)

For the vtkLookupTable lut set the colour Hue, Saturation, or Value range from low to high. Set low = high to fix at a single value.

ctransfer.AddRGBPoint(val, R, G, B) ctransfer.AddRGBPoint(val, R, G, B, A)

For the vtkColorTransferFunction ctransfer set the colour R, G, B to the float val. An alpha value A can optionally be specified.

ctransfer.GetColor(val)

Get the colour as 3 RGB values that corresponds to float val for the vtkColorTransferFunction ctransfer.

lut.SetTableValue(val, R, G, B)

Manually set the colour (R, G, B) for the value val for the lookup table lut.

Transformations

actor.SetPosition(x, y, z)

Place vtkActor actor at coordinates (x, y, z).

actor.GetPosition()

Get the current (x, y, z) coordinates of actor.

actor.AddPosition(x, y, z)

Displace actor by (x, y, z) from its current position.

actor.SetScale(s) actor.SetScale(x, y, z)

Scale actor either by a factor of s in all directions or a factor of (x, y, z) in the corresponding directions of the local coordinate system of actor.

actor.RotateX(theta) actor.RotateY(theta) actor.RotateZ(theta)

Rotate actor around its local X, Y, or Z axis by theta degrees.