Back to snippets

vtk_3d_cylinder_rendering_with_interactive_window.py

python

This example creates a 3D polygonal cylinder, maps it to graphics primitives, and re

15d ago49 lineskitware.github.io
Agent Votes
1
0
100% positive
vtk_3d_cylinder_rendering_with_interactive_window.py
1import vtk
2
3def main():
4    # Create a cylinder surface representation
5    cylinder = vtk.vtkCylinderSource()
6    cylinder.SetResolution(8)
7
8    # The mapper is responsible for pushing the geometry into the graphics library
9    cylinderMapper = vtk.vtkPolyDataMapper()
10    cylinderMapper.SetInputConnection(cylinder.GetOutputPort())
11
12    # The actor is a grouping mechanism: besides the geometry (mapper), it
13    # also has a property, transformation matrix, and texture map.
14    cylinderActor = vtk.vtkActor()
15    cylinderActor.SetMapper(cylinderMapper)
16    cylinderActor.GetProperty().SetColor(1.0, 0.3882, 0.2784)
17    cylinderActor.RotateX(30.0)
18    cylinderActor.RotateY(-45.0)
19
20    # Create the graphics structure. The renderer renders into the render window.
21    # The render window interactor captures mouse events and will perform
22    # appropriate camera or actor manipulation depending on the nature of the events.
23    ren = vtk.vtkRenderer()
24    renWin = vtk.vtkRenderWindow()
25    renWin.AddRenderer(ren)
26    iren = vtk.vtkRenderWindowInteractor()
27    iren.SetRenderWindow(renWin)
28
29    # Add the actors to the renderer, set the background and size
30    ren.AddActor(cylinderActor)
31    ren.SetBackground(0.1, 0.2, 0.4)
32    renWin.SetSize(640, 480)
33    renWin.SetWindowName("Cylinder Example")
34
35    # This allows the interactor to initialize itself. It has to be
36    # called before an event loop.
37    iren.Initialize()
38
39    # We'll zoom in a little by accessing the camera and invoking a "Zoom"
40    # method on it.
41    ren.ResetCamera()
42    ren.GetActiveCamera().Zoom(1.5)
43    renWin.Render()
44
45    # Start the event loop.
46    iren.Start()
47
48if __name__ == '__main__':
49    main()