/[escript]/trunk/pyvisi/py_src/scene.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/scene.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 861 - (show annotations)
Fri Sep 29 06:54:46 2006 UTC (14 years ago) by jongui
File MIME type: text/x-python
File size: 3511 byte(s)
- Added some simple plane cutting animation and storing them to file.

1 """
2 @author: John Ngui
3 @author: Lutz Gross
4 """
5
6 import vtk
7 from constants import *
8
9 class Scene:
10 """
11 Class that defines a scene in which components are displayed.
12 """
13
14 def __init__(self, renderer, x_size = 800, y_size = 600):
15 """
16 @type renderer: String
17 @param renderer: Type of rendering
18 @type x_size: Number
19 @param x_size: Size of the rendering window on the x-axis
20 @type y_size: Number
21 @param y_size: Size of the rendering window on the y-axis
22 """
23
24 self.renderer = renderer
25 self.x_size = x_size
26 self.y_size = y_size
27 self.vtk_renderer = vtk.vtkRenderer()
28 self.vtk_render_window = vtk.vtkRenderWindow()
29 self.vtk_render_window_interactor = vtk.vtkRenderWindowInteractor()
30
31 self.setRenderWindow()
32
33 def saveImage(self, image_name):
34 """
35 Save the rendered object as an image.
36 @type image_name: String
37 @param image_name: Name of the image file
38 """
39
40 #self.vtk_render_window.Render()
41 #self.vtk_render_window.OffScreenRenderingOn()
42 #self.vtk_render_window.OffScreenRenderingOff()
43
44 # Converts the output of the render window into vtkImageData.
45 vtk_window_to_image = vtk.vtkWindowToImageFilter()
46 #vtk_window_to_image.Update() # Update precaution
47 vtk_window_to_image.SetInput(self.vtk_render_window)
48 vtk_window_to_image.Modified() # Update precaution
49 # Force an update to the of the output image as vtk window's
50 # modification time does not get updated automatically.
51 #vtk_window_to_image.Modified() # Update precaution
52 #vtk_window_to_image.Update() # Update precaution
53
54 # Write the image to file.
55 vtk_image_writer = self.getImageWriter(self.renderer)
56 vtk_image_writer.SetInput(vtk_window_to_image.GetOutput())
57 vtk_image_writer.SetFileName(image_name)
58 vtk_image_writer.Write()
59
60
61 def setRenderWindow(self):
62 """
63 Set up the renderer and rendering window.
64 """
65
66 self.vtk_render_window.AddRenderer(self.vtk_renderer)
67 # Title of the render window.
68 self.vtk_render_window.SetWindowName(
69 "Earth Systems Science Computational Centre Rendering Tool")
70 self.vtk_render_window.SetSize(self.x_size, self.y_size)
71 # Default background color is white.
72 self.vtk_renderer.SetBackground(
73 WHITE[0], WHITE[1], WHITE[2])
74
75 def render(self):
76 """
77 Render the image.
78 """
79
80 # True if not initialized yet.
81 if(self.vtk_render_window_interactor.GetInitialized() == 0):
82 self.vtk_render_window_interactor.SetRenderWindow(
83 self.vtk_render_window)
84 self.vtk_render_window_interactor.Initialize()
85 #self.vtk_render_window.Render()
86 self.vtk_render_window_interactor.GetInitialized()
87 self.vtk_render_window_interactor.Start()
88 else: # True if already initialized.
89 #self.vtk_render_window.Modified()
90 self.vtk_render_window.Render()
91 #self.vtk_render_window.Modified()
92
93
94 def getImageWriter(self, renderer):
95 """
96 Determine the type of renderer and return the correponding image writer.
97 @type renderer: String
98 @param renderer: Type of renderer
99 """
100
101 if(renderer == "vtk_jpeg"):
102 return vtk.vtkJPEGWriter()
103 elif(renderer == "vtk_bmp"):
104 return vtk.vtkBMPWriter()
105 elif(renderer == "vtk_pnm"):
106 return vtk.vtkPNMWriter()
107 elif(renderer == "vtk_png"):
108 return vtk.vtkPNGWriter()
109 elif(renderer == "vtk_tiff"):
110 return vtk.vtkTIFFWriter()
111 elif(renderer == "vtk_ps"):
112 return vtk.vtkPostScriptWriter()
113
114 def getRenderer(self):
115 """
116 Return the renderer.
117
118 @rtype: vtkRenderer
119 @return: VTK renderer that is used to render objects on the window
120 """
121
122 return self.vtk_renderer

  ViewVC Help
Powered by ViewVC 1.1.26