source: development/steve/visualisation/Tutorial/Step6/Cone6.py @ 2229

Last change on this file since 2229 was 2229, checked in by steve, 19 years ago

Moved directories into production and development parent directories

File size: 5.0 KB
RevLine 
[2229]1#!/usr/bin/env python
2#
3# This example introduces 3D widgets. 3D widgets take advantage of the
4# event/observer design pattern introduced previously. They typically
5# have a particular representation in the scene which can be interactively
6# selected and manipulated using the mouse and keyboard. As the widgets
7# are manipulated, they in turn invoke events such as StartInteractionEvent,
8# InteractionEvent, and EndInteractionEvent which can be used to manipulate
9# the scene that the widget is embedded in. 3D widgets work in the context
10# of the event loop which was set up in the previous example.
11#
12# Note: there are more 3D widget examples in VTK/Examples/GUI/.
13#
14
15# First we import the VTK Python package that will make available all
16# of the VTK commands to Python.
17import vtk
18
19# Next we create an instance of vtkConeSource and set some of its
20# properties. The instance of vtkConeSource "cone" is part of a
21# visualization pipeline (it is a source process object); it produces
22# data (output type is vtkPolyData) which other filters may process.
23cone = vtk.vtkConeSource()
24cone.SetHeight(3.0)
25cone.SetRadius(1.0)
26cone.SetResolution(10)
27
28# In this example we terminate the pipeline with a mapper process object.
29# (Intermediate filters such as vtkShrinkPolyData could be inserted in
30# between the source and the mapper.)  We create an instance of
31# vtkPolyDataMapper to map the polygonal data into graphics primitives. We
32# connect the output of the cone souece to the input of this mapper.
33coneMapper = vtk.vtkPolyDataMapper()
34coneMapper.SetInput(cone.GetOutput())
35
36# Create an actor to represent the cone. The actor orchestrates rendering of
37# the mapper's graphics primitives. An actor also refers to properties via a
38# vtkProperty instance, and includes an internal transformation matrix. We
39# set this actor's mapper to be coneMapper which we created above.
40coneActor = vtk.vtkActor()
41coneActor.SetMapper(coneMapper)
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
45# responsible for drawing the actors it has.  We also set the
46# background color here.
47ren1 = vtk.vtkRenderer()
48ren1.AddActor(coneActor)
49ren1.SetBackground(0.1, 0.2, 0.4)
50
51# Finally we create the render window which will show up on the screen
52# We put our renderer into the render window using AddRenderer. We
53# also set the size to be 300 pixels by 300.
54renWin = vtk.vtkRenderWindow()
55renWin.AddRenderer(ren1)
56renWin.SetSize(300, 300)
57
58# The vtkRenderWindowInteractor class watches for events (e.g., keypress,
59# mouse) in the vtkRenderWindow. These events are translated into
60# event invocations that VTK understands (see VTK/Common/vtkCommand.h
61# for all events that VTK processes). Then observers of these VTK
62# events can process them as appropriate.
63iren = vtk.vtkRenderWindowInteractor()
64iren.SetRenderWindow(renWin)
65
66# By default the vtkRenderWindowInteractor instantiates an instance
67# of vtkInteractorStyle. vtkInteractorStyle translates a set of events
68# it observes into operations on the camera, actors, and/or properties
69# in the vtkRenderWindow associated with the vtkRenderWinodwInteractor.
70# Here we specify a particular interactor style.
71style = vtk.vtkInteractorStyleTrackballCamera()
72iren.SetInteractorStyle(style)
73
74
75# Here we use a vtkBoxWidget to transform the underlying coneActor (by
76# manipulating its transformation matrix). Many other types of widgets
77# are available for use, see the documentation for more details.
78#
79# The SetInteractor method is how 3D widgets are associated with the render
80# window interactor. Internally, SetInteractor sets up a bunch of callbacks
81# using the Command/Observer mechanism (AddObserver()). The place factor
82# controls the initial size of the widget with respect to the bounding box
83# of the input to the widget.
84boxWidget = vtk.vtkBoxWidget()
85boxWidget.SetInteractor(iren)
86boxWidget.SetPlaceFactor(1.25)
87
88# Place the interactor initially. The input to a 3D widget is used to
89# initially position and scale the widget. The EndInteractionEvent is
90# observed which invokes the SelectPolygons callback.
91boxWidget.SetProp3D(coneActor)
92boxWidget.PlaceWidget()
93
94# Similar to Step2/Python/Cone2.py, we define a callback for
95# interaction.  As can be seen the callback takes two arguments.  The
96# first being the object that generates the event and the second
97# argument the event name (which is a string).
98def myCallback(widget, event_string):
99    t = vtk.vtkTransform()
100    boxWidget.GetTransform(t)
101    boxWidget.GetProp3D().SetUserTransform(t)
102       
103
104# Now for every interaction event that is generated by the boxWidget,
105# call our callback function.
106boxWidget.AddObserver("InteractionEvent", myCallback)
107
108# Normally the user presses the "i" key to bring a 3D widget to
109# life. Here we will manually enable it so it appears with the cone.
110boxWidget.On()
111
112# Start the event loop.
113iren.Initialize()
114iren.Start()
115
116# There is no explicit need to free any objects at this point.
117# Once Python exits, memory is automatically freed.
Note: See TracBrowser for help on using the repository browser.