1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # |
---|
4 | # This example shows how to add an observer to a Python program. It extends |
---|
5 | # the Step1/Python/Cone.py Python example (see that example for information on |
---|
6 | # the basic setup). |
---|
7 | # |
---|
8 | # VTK uses a command/observer design pattern. That is, observers watch for |
---|
9 | # particular events that any vtkObject (or subclass) may invoke on |
---|
10 | # itself. For example, the vtkRenderer invokes a "StartEvent" as it begins |
---|
11 | # to render. Here we add an observer that invokes a command when this event |
---|
12 | # is observed. |
---|
13 | # |
---|
14 | |
---|
15 | import vtk |
---|
16 | import time |
---|
17 | |
---|
18 | # |
---|
19 | # define the callback |
---|
20 | # |
---|
21 | def myCallback(obj,string): |
---|
22 | print "Starting a render" |
---|
23 | |
---|
24 | |
---|
25 | # |
---|
26 | # create the basic pipeline as in Step1 |
---|
27 | # |
---|
28 | cone = vtk.vtkConeSource() |
---|
29 | cone.SetHeight( 3.0 ) |
---|
30 | cone.SetRadius( 1.0 ) |
---|
31 | cone.SetResolution( 10 ) |
---|
32 | |
---|
33 | coneMapper = vtk.vtkPolyDataMapper() |
---|
34 | coneMapper.SetInput( cone.GetOutput() ) |
---|
35 | coneActor = vtk.vtkActor() |
---|
36 | coneActor.SetMapper( coneMapper ) |
---|
37 | |
---|
38 | ren1= vtk.vtkRenderer() |
---|
39 | ren1.AddActor( coneActor ) |
---|
40 | ren1.SetBackground( 0.1, 0.2, 0.4 ) |
---|
41 | |
---|
42 | # |
---|
43 | # Add the observer here |
---|
44 | # |
---|
45 | ren1.AddObserver("StartEvent", myCallback) |
---|
46 | |
---|
47 | renWin = vtk.vtkRenderWindow() |
---|
48 | renWin.AddRenderer( ren1 ) |
---|
49 | renWin.SetSize( 300, 300 ) |
---|
50 | |
---|
51 | # |
---|
52 | # now we loop over 360 degreeees and render the cone each time |
---|
53 | # |
---|
54 | for i in range(0,360): |
---|
55 | time.sleep(0.03) |
---|
56 | renWin.Render() |
---|
57 | ren1.GetActiveCamera().Azimuth( 1 ) |
---|
58 | |
---|