]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/vtk.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.vtk
3 # Purpose: Provides a wrapper around the vtkRenderWindow from the
4 # VTK Visualization Toolkit. Requires the VTK Python
5 # extensions from http://www.kitware.com/
11 # Copyright: (c) 1999 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
17 from vtkpython
import *
19 try: # for Unix. Why they do it this way is anybody's guess...
20 from libVTKCommonPython
import *
21 from libVTKGraphicsPython
import *
22 from libVTKImagingPython
import *
24 raise ImportError, "VTK extension module not found"
26 from wxPython
.wx
import *
29 #----------------------------------------------------------------------
31 class wxVTKRenderWindow(wxWindow
):
32 def __init__(self
, parent
, id, position
=wxDefaultPosition
,
33 size
=wxDefaultSize
, style
=0):
34 wxWindow
.__init
__(self
, parent
, id, position
, size
, style
)
36 self
.renderWindow
= vtkRenderWindow()
38 hdl
= self
.GetHandle()
39 self
.renderWindow
.SetWindowInfo(str(hdl
))
41 EVT_LEFT_DOWN (self
, self
.SaveClick
)
42 EVT_MIDDLE_DOWN(self
, self
.SaveClick
)
43 EVT_RIGHT_DOWN (self
, self
.SaveClick
)
44 EVT_LEFT_UP (self
, self
.Release
)
45 EVT_MIDDLE_UP (self
, self
.Release
)
46 EVT_RIGHT_UP (self
, self
.Release
)
47 EVT_MOTION(self
, self
.MouseMotion
)
50 def GetRenderer(self
):
51 self
.renderWindow
.GetRenderers().InitTraversal()
52 return self
.renderWindow
.GetRenderers().GetNextItem()
55 def GetRenderWindow(self
):
56 return self
.renderWindow
59 def OnPaint(self
, event
):
61 self
.renderWindow
.Render()
64 def SaveClick(self
, event
):
65 self
.prev_x
, self
.prev_y
= event
.GetPositionTuple()
68 def Release(self
, event
):
71 def MouseMotion(self
, event
):
72 event
.x
, event
.y
= event
.GetPositionTuple()
73 size
= self
.GetClientSize()
74 if event
.LeftIsDown():
76 camera
= self
.GetRenderer().GetActiveCamera()
77 camera
.Azimuth(float(self
.prev_x
- event
.x
) / size
.width
* 360)
78 camera
.Elevation(float(event
.y
- self
.prev_y
) / size
.width
* 360)
79 camera
.OrthogonalizeViewUp()
80 self
.MotionUpdate(event
)
82 elif event
.MiddleIsDown():
84 camera
= self
.GetRenderer().GetActiveCamera()
85 camera
.Yaw(-float(self
.prev_x
- event
.x
) / size
.width
* 30)
86 camera
.Pitch(float(event
.y
- self
.prev_y
) / size
.width
* 30)
87 camera
.OrthogonalizeViewUp()
88 self
.MotionUpdate(event
)
90 elif event
.RightIsDown():
92 camera
= self
.GetRenderer().GetActiveCamera()
93 camera
.Dolly(math
.exp(float((event
.x
- self
.prev_x
) - \
94 (event
.y
- self
.prev_y
))/ \
96 self
.MotionUpdate(event
)
99 def MotionUpdate(self
,event
):
100 renderer
= self
.GetRenderer()
101 renderer
.GetLights().InitTraversal()
102 light
= renderer
.GetLights().GetNextItem()
103 camera
= renderer
.GetActiveCamera()
104 light
.SetPosition(camera
.GetPosition())
105 light
.SetFocalPoint(camera
.GetFocalPoint())
106 self
.renderWindow
.Render()
107 self
.prev_x
= event
.x
108 self
.prev_y
= event
.y
112 ## testcode is now in the demo ##
114 ## if __name__ == '__main__':
115 ## class TestFrame(wxFrame):
116 ## def __init__(self, parent):
117 ## wxFrame.__init__(self, parent, -1, "VTK Test", size=(450,450))
119 ## rw = wxVTKRenderWindow(self, -1)
121 ## # Get the render window
122 ## renWin = rw.GetRenderWindow()
124 ## # Next, do the VTK stuff
125 ## ren = vtkRenderer()
126 ## renWin.AddRenderer(ren)
127 ## cone = vtkConeSource()
128 ## cone.SetResolution(80)
129 ## coneMapper = vtkPolyDataMapper()
130 ## coneMapper.SetInput(cone.GetOutput())
131 ## coneActor = vtkActor()
132 ## coneActor.SetMapper(coneMapper)
133 ## ren.AddActor(coneActor)
134 ## coneMapper.GetLookupTable().Build()
136 ## # Create a scalar bar
137 ## scalarBar = vtkScalarBarActor()
138 ## scalarBar.SetLookupTable(coneMapper.GetLookupTable())
139 ## scalarBar.SetTitle("Temperature")
140 ## scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
141 ## scalarBar.GetPositionCoordinate().SetValue(0.1, 0.01)
142 ## scalarBar.SetOrientationToHorizontal()
143 ## scalarBar.SetWidth(0.8)
144 ## scalarBar.SetHeight(0.17)
145 ## ren.AddActor2D(scalarBar)
149 ## class TestApp(wxApp):
151 ## f = TestFrame(None)
152 ## self.SetTopWindow(f)