]>
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(wxScrolledWindow
):
32 def __init__(self
, parent
, id, position
=wxDefaultPosition
,
33 size
=wxDefaultSize
, style
=0):
34 wxScrolledWindow
.__init
__(self
, parent
, id, position
, size
, style
)
36 self
.renderWindow
= vtkRenderWindow()
38 if wxPlatform
!= '__WXMSW__':
39 # We can't get the handle in wxGTK until after the widget
40 # is created, the window create event happens later so we'll
42 EVT_WINDOW_CREATE(self
, self
.OnCreateWindow
)
44 # but in MSW, the window create event happens durring the above
45 # call to __init__ so we have to do it here.
46 hdl
= self
.GetHandle()
47 self
.renderWindow
.SetWindowInfo(str(hdl
))
50 EVT_LEFT_DOWN (self
, self
.SaveClick
)
51 EVT_MIDDLE_DOWN(self
, self
.SaveClick
)
52 EVT_RIGHT_DOWN (self
, self
.SaveClick
)
53 EVT_LEFT_UP (self
, self
.Release
)
54 EVT_MIDDLE_UP (self
, self
.Release
)
55 EVT_RIGHT_UP (self
, self
.Release
)
56 EVT_MOTION (self
, self
.MouseMotion
)
59 def GetRenderer(self
):
60 self
.renderWindow
.GetRenderers().InitTraversal()
61 return self
.renderWindow
.GetRenderers().GetNextItem()
64 def GetRenderWindow(self
):
65 return self
.renderWindow
68 def OnPaint(self
, event
):
70 self
.renderWindow
.Render()
72 def OnCreateWindow(self
, event
):
73 hdl
= self
.GetHandle()
74 self
.renderWindow
.SetWindowInfo(str(hdl
))
76 def OnEraseBackground(self
, event
):
81 def SaveClick(self
, event
):
82 self
.prev_x
, self
.prev_y
= event
.GetPositionTuple()
85 def Release(self
, event
):
88 def MouseMotion(self
, event
):
89 event
.x
, event
.y
= event
.GetPositionTuple()
90 size
= self
.GetClientSize()
91 if event
.LeftIsDown():
93 camera
= self
.GetRenderer().GetActiveCamera()
94 camera
.Azimuth(float(self
.prev_x
- event
.x
) / size
.width
* 360)
95 camera
.Elevation(float(event
.y
- self
.prev_y
) / size
.width
* 360)
96 camera
.OrthogonalizeViewUp()
97 self
.MotionUpdate(event
)
99 elif event
.MiddleIsDown():
101 camera
= self
.GetRenderer().GetActiveCamera()
102 camera
.Yaw(-float(self
.prev_x
- event
.x
) / size
.width
* 30)
103 camera
.Pitch(float(event
.y
- self
.prev_y
) / size
.width
* 30)
104 camera
.OrthogonalizeViewUp()
105 self
.MotionUpdate(event
)
107 elif event
.RightIsDown():
109 camera
= self
.GetRenderer().GetActiveCamera()
110 camera
.Dolly(math
.exp(float((event
.x
- self
.prev_x
) - \
111 (event
.y
- self
.prev_y
))/ \
113 self
.MotionUpdate(event
)
116 def MotionUpdate(self
,event
):
117 renderer
= self
.GetRenderer()
118 renderer
.GetLights().InitTraversal()
119 light
= renderer
.GetLights().GetNextItem()
120 camera
= renderer
.GetActiveCamera()
121 light
.SetPosition(camera
.GetPosition())
122 light
.SetFocalPoint(camera
.GetFocalPoint())
123 self
.renderWindow
.Render()
124 self
.prev_x
= event
.x
125 self
.prev_y
= event
.y