[2229] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | # |
---|
| 4 | # This example introduces the concepts of user interaction with VTK. |
---|
| 5 | # First, a different interaction style (than the default) is defined. |
---|
| 6 | # Second, the interaction is started. |
---|
| 7 | # |
---|
| 8 | # |
---|
| 9 | |
---|
| 10 | import vtk |
---|
| 11 | |
---|
| 12 | # |
---|
| 13 | # Next we create an instance of vtkConeSource and set some of its |
---|
| 14 | # properties. The instance of vtkConeSource "cone" is part of a visualization |
---|
| 15 | # pipeline (it is a source process object); it produces data (output type is |
---|
| 16 | # vtkPolyData) which other filters may process. |
---|
| 17 | # |
---|
| 18 | cone = vtk.vtkConeSource() |
---|
| 19 | cone.SetHeight( 3.0 ) |
---|
| 20 | cone.SetRadius( 1.0 ) |
---|
| 21 | cone.SetResolution( 10 ) |
---|
| 22 | |
---|
| 23 | # |
---|
| 24 | # In this example we terminate the pipeline with a mapper process object. |
---|
| 25 | # (Intermediate filters such as vtkShrinkPolyData could be inserted in |
---|
| 26 | # between the source and the mapper.) We create an instance of |
---|
| 27 | # vtkPolyDataMapper to map the polygonal data into graphics primitives. We |
---|
| 28 | # connect the output of the cone souece to the input of this mapper. |
---|
| 29 | # |
---|
| 30 | coneMapper = vtk.vtkPolyDataMapper() |
---|
| 31 | coneMapper.SetInput(cone.GetOutput()) |
---|
| 32 | |
---|
| 33 | # |
---|
| 34 | # Create an actor to represent the cone. The actor orchestrates rendering of |
---|
| 35 | # the mapper's graphics primitives. An actor also refers to properties via a |
---|
| 36 | # vtkProperty instance, and includes an internal transformation matrix. We |
---|
| 37 | # set this actor's mapper to be coneMapper which we created above. |
---|
| 38 | # |
---|
| 39 | coneActor = vtk.vtkActor() |
---|
| 40 | coneActor.SetMapper(coneMapper) |
---|
| 41 | |
---|
| 42 | # |
---|
| 43 | # Create the Renderer and assign actors to it. A renderer is like a |
---|
| 44 | # viewport. It is part or all of a window on the screen and it is responsible |
---|
| 45 | # for drawing the actors it has. We also set the background color here. |
---|
| 46 | # |
---|
| 47 | ren1 = vtk.vtkRenderer() |
---|
| 48 | ren1.AddActor(coneActor) |
---|
| 49 | ren1.SetBackground(0.1, 0.2, 0.4) |
---|
| 50 | |
---|
| 51 | # |
---|
| 52 | # Finally we create the render window which will show up on the screen |
---|
| 53 | # We put our renderer into the render window using AddRenderer. We also |
---|
| 54 | # set the size to be 300 pixels by 300. |
---|
| 55 | # |
---|
| 56 | renWin = vtk.vtkRenderWindow() |
---|
| 57 | renWin.AddRenderer(ren1) |
---|
| 58 | renWin.SetSize(300, 300) |
---|
| 59 | |
---|
| 60 | # |
---|
| 61 | # The vtkRenderWindowInteractor class watches for events (e.g., keypress, |
---|
| 62 | # mouse) in the vtkRenderWindow. These events are translated into |
---|
| 63 | # event invocations that VTK understands (see VTK/Common/vtkCommand.h |
---|
| 64 | # for all events that VTK processes). Then observers of these VTK |
---|
| 65 | # events can process them as appropriate. |
---|
| 66 | iren = vtk.vtkRenderWindowInteractor() |
---|
| 67 | iren.SetRenderWindow(renWin) |
---|
| 68 | |
---|
| 69 | # |
---|
| 70 | # By default the vtkRenderWindowInteractor instantiates an instance |
---|
| 71 | # of vtkInteractorStyle. vtkInteractorStyle translates a set of events |
---|
| 72 | # it observes into operations on the camera, actors, and/or properties |
---|
| 73 | # in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. |
---|
| 74 | # Here we specify a particular interactor style. |
---|
| 75 | style = vtk.vtkInteractorStyleTrackballCamera() |
---|
| 76 | iren.SetInteractorStyle(style) |
---|
| 77 | |
---|
| 78 | # |
---|
| 79 | # Unlike the previous scripts where we performed some operations and then |
---|
| 80 | # exited, here we leave an event loop running. The user can use the mouse |
---|
| 81 | # and keyboard to perform the operations on the scene according to the |
---|
| 82 | # current interaction style. |
---|
| 83 | # |
---|
| 84 | |
---|
| 85 | # |
---|
| 86 | # Initialize and start the event loop. Once the render window appears, mouse |
---|
| 87 | # in the window to move the camera. The Start() method executes an event |
---|
| 88 | # loop which listens to user mouse and keyboard events. Note that keypress-e |
---|
| 89 | # exits the event loop. (Look in vtkInteractorStyle.h for a summary of events, or |
---|
| 90 | # the appropriate Doxygen documentation.) |
---|
| 91 | # |
---|
| 92 | iren.Initialize() |
---|
| 93 | iren.Start() |
---|