Skip to content

TransformPolyData

vtk-examples/Python/Filtering/TransformPolyData


Description

This example demonstrates how to apply a transform to a data set. It uses vtkTransformPolyDataFilter, but it can be replaced with vtkTransformFilter for different types of data sets, including vtkUnstructuredGrid and vtkStructuredGrid. vtkTransformFilter will work with vtkPolyData, too).

Other languages

See (Cxx), (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

TransformPolyData.py

#!/usr/bin/env python

import vtk


def main():
    colors = vtk.vtkNamedColors()

    # Create the polydata geometry
    sphereSource = vtk.vtkSphereSource()
    sphereSource.Update()

    # Set up the actor to display the untransformed polydata
    originalMapper = vtk.vtkPolyDataMapper()
    originalMapper.SetInputConnection(sphereSource.GetOutputPort())

    originalActor = vtk.vtkActor()
    originalActor.SetMapper(originalMapper)
    originalActor.GetProperty().SetColor(colors.GetColor3d('Blue'))

    # Set up the transform filter
    translation = vtk.vtkTransform()
    translation.Translate(1.0, 2.0, 3.0)

    transformFilter = vtk.vtkTransformPolyDataFilter()
    transformFilter.SetInputConnection(sphereSource.GetOutputPort())
    transformFilter.SetTransform(translation)
    transformFilter.Update()

    # Set up the actor to display the transformed polydata
    transformedMapper = vtk.vtkPolyDataMapper()
    transformedMapper.SetInputConnection(transformFilter.GetOutputPort())

    transformedActor = vtk.vtkActor()
    transformedActor.SetMapper(transformedMapper)
    transformedActor.GetProperty().SetColor(colors.GetColor3d('Red'))

    # Set up the rest of the visualization pipeline
    renderer = vtk.vtkRenderer()
    renderer.AddActor(originalActor)
    renderer.AddActor(transformedActor)
    renderer.SetBackground(colors.GetColor3d('Green'))

    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    renderWindow.SetWindowName('TransformPolyData')
    renderWindow.Render()

    renderWindowInteractor.Start()


if __name__ == '__main__':
    main()