1 |
# Copyright (C) 2004-2005 Paul Cochrane |
2 |
# |
3 |
# This program is free software; you can redistribute it and/or |
4 |
# modify it under the terms of the GNU General Public License |
5 |
# as published by the Free Software Foundation; either version 2 |
6 |
# of the License, or (at your option) any later version. |
7 |
# |
8 |
# This program is distributed in the hope that it will be useful, |
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 |
# GNU General Public License for more details. |
12 |
# |
13 |
# You should have received a copy of the GNU General Public License |
14 |
# along with this program; if not, write to the Free Software |
15 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 |
|
17 |
# $Id: scene.py,v 1.23 2005/11/03 05:55:59 paultcochrane Exp $ |
18 |
|
19 |
## @file scene.py |
20 |
|
21 |
""" |
22 |
Class and functions associated with a pyvisi Scene |
23 |
""" |
24 |
|
25 |
# generic imports |
26 |
from pyvisi.common import debugMsg, overrideWarning, fileCheck |
27 |
|
28 |
from pyvisi.renderer import Renderer |
29 |
|
30 |
__revision__ = '$Revision: 1.23 $' |
31 |
|
32 |
class Scene(object): |
33 |
""" |
34 |
The main object controlling the scene. |
35 |
|
36 |
This is the base Scene object. It should be inherited, and then its |
37 |
methods overridden. |
38 |
""" |
39 |
|
40 |
def __init__(self): |
41 |
""" |
42 |
The init function |
43 |
""" |
44 |
object.__init__(self) |
45 |
debugMsg("Called Scene.__init__()") |
46 |
|
47 |
self.renderer = Renderer() |
48 |
|
49 |
self.xSize = 640 |
50 |
self.ySize = 480 |
51 |
|
52 |
def add(self, obj): |
53 |
""" |
54 |
Add a new item to the scene |
55 |
|
56 |
@param obj: The object to add to the scene |
57 |
@type obj: object |
58 |
""" |
59 |
debugMsg("Called Scene.add()") |
60 |
|
61 |
if obj is None: |
62 |
raise ValueError, "You must specify an object to add" |
63 |
|
64 |
# print a warning message if get to here |
65 |
overrideWarning("Scene.add") |
66 |
|
67 |
return |
68 |
|
69 |
def delete(self, obj): |
70 |
""" |
71 |
Delete an item from the scene |
72 |
|
73 |
@param obj: The object to remove |
74 |
@type obj: object |
75 |
""" |
76 |
debugMsg("Called Scene.delete()") |
77 |
|
78 |
if obj is None: |
79 |
raise ValueError, "You must specify an object to delete" |
80 |
|
81 |
# print a warning message if get to here |
82 |
overrideWarning("Scene.delete") |
83 |
|
84 |
return |
85 |
|
86 |
def place(self, obj): |
87 |
""" |
88 |
Place an object within a scene |
89 |
|
90 |
@param obj: The object to place within the scene |
91 |
@type obj: object |
92 |
""" |
93 |
debugMsg("Called Scene.place()") |
94 |
|
95 |
if obj is None: |
96 |
raise ValueError, "You must specify an object to place" |
97 |
|
98 |
# print a warning message if get to here |
99 |
overrideWarning("Scene.place") |
100 |
|
101 |
return |
102 |
|
103 |
def render(self, pause=False, interactive=False): |
104 |
""" |
105 |
Render (or re-render) the scene |
106 |
|
107 |
Render the scene, either to screen, or to a buffer waiting for a save |
108 |
|
109 |
@param pause: Flag to wait at end of script evaluation for user input |
110 |
@type pause: boolean |
111 |
|
112 |
@param interactive: Whether or not to have interactive use of the output |
113 |
@type interactive: boolean |
114 |
""" |
115 |
debugMsg("Called Scene.render()") |
116 |
renderer = self.renderer |
117 |
|
118 |
# I don't yet know where to put this, but just to get stuff going... |
119 |
renderer.runString("# Scene.render()\n") |
120 |
|
121 |
# optionally print out the evaluation stack to make sure we're doing |
122 |
# the right thing |
123 |
debugMsg("Here is the evaluation stack") |
124 |
debugMsg(60*"#") |
125 |
debugMsg(renderer.getEvalStack()) |
126 |
debugMsg(60*"#") |
127 |
|
128 |
# execute the eval stack |
129 |
evalStack = renderer.getEvalStack() |
130 |
exec evalStack in self.renderer.renderDict |
131 |
|
132 |
# flush the evaluation stack |
133 |
debugMsg("Flusing evaluation stack") |
134 |
renderer.resetEvalStack() |
135 |
|
136 |
# this is just to stop lint from complaining that pause and |
137 |
# interactive aren't used |
138 |
if pause is not True or pause is not False: |
139 |
raise ValueError, "\'pause\' must be either True or False" |
140 |
|
141 |
if interactive is not True or pause is not False: |
142 |
raise ValueError, "\'interactive\' must be either True or False" |
143 |
|
144 |
return |
145 |
|
146 |
def save(self, fname, format): |
147 |
""" |
148 |
Save the scene to a file |
149 |
|
150 |
@param fname: The name of the file to save the scene to |
151 |
@type fname: string |
152 |
|
153 |
@param format: The format in which to save the scene |
154 |
@type format: Image object or string |
155 |
""" |
156 |
debugMsg("Called Scene.save()") |
157 |
|
158 |
if fname is None or fname == "": |
159 |
raise ValueError, "You must specify an output filename" |
160 |
|
161 |
if format is None or format == "": |
162 |
raise ValueError, "You must specify an image format" |
163 |
|
164 |
# now check the type of arguments sent in |
165 |
if __debug__: |
166 |
assert isinstance(fname, str), "Incorrect data type; expected str" |
167 |
assert isinstance(format, str), "Incorrect data type; expected str" |
168 |
|
169 |
# do a check to see if the file exists |
170 |
fileCheck(fname) |
171 |
|
172 |
# print a warning message if get to here |
173 |
overrideWarning("Scene.save") |
174 |
|
175 |
return |
176 |
|
177 |
write = save |
178 |
|
179 |
def setBackgroundColor(self, *color): |
180 |
""" |
181 |
Sets the background color of the Scene |
182 |
|
183 |
@param color: The color to set the background to. Can be RGB or CMYK |
184 |
@type color: tuple |
185 |
""" |
186 |
debugMsg("Called Scene.setBackgroundColor()") |
187 |
|
188 |
# print a warning message if get to here |
189 |
overrideWarning("Scene.setBackgroundColor") |
190 |
|
191 |
# pity this code doesn't work.... |
192 |
# need to check on the values given in the *color array. |
193 |
# if they're greater than 1, scale so that the largest is 1 |
194 |
#maxColor = None |
195 |
#for i in range(len(color)): |
196 |
#if color[i] > 1: |
197 |
#maxColor = color[i] |
198 |
#print maxColor |
199 |
|
200 |
## if a maximum colour is found, then scale the colours |
201 |
#if maxColor is not None: |
202 |
#for i in range(len(color)): |
203 |
#color[i] = color[i]/maxColor |
204 |
|
205 |
# if color is of length 3, then we have rgb |
206 |
# if length is 4 then cmyk |
207 |
# if length is 1 then greyscale |
208 |
# otherwise barf |
209 |
if len(color) == 3: |
210 |
# ok, using rgb |
211 |
# probably should use a Color object or something |
212 |
# this will do in the meantime |
213 |
pass |
214 |
else: |
215 |
raise ValueError, "Sorry, only RGB color is supported at present" |
216 |
|
217 |
return |
218 |
|
219 |
def getBackgroundColor(self): |
220 |
""" |
221 |
Gets the current background color setting of the Scene |
222 |
""" |
223 |
debugMsg("Called Scene.getBackgroundColor()") |
224 |
|
225 |
# print a warning message if get to here |
226 |
overrideWarning("Scene.getBackgroundColor") |
227 |
|
228 |
return |
229 |
|
230 |
def setSize(self, xSize, ySize): |
231 |
""" |
232 |
Sets the size of the scene. |
233 |
|
234 |
This size is effectively the renderer window size. |
235 |
|
236 |
@param xSize: the size to set the x dimension |
237 |
@type xSize: int |
238 |
|
239 |
@param ySize: the size to set the y dimension |
240 |
@type ySize: int |
241 |
""" |
242 |
debugMsg("Called Scene.setSize()") |
243 |
|
244 |
# make sure that the arguments are the right kind of thing |
245 |
if __debug__: |
246 |
assert isinstance(xSize, int), "Incorrect data type; expected int" |
247 |
assert isinstance(ySize, int), "Incorrect data type; expected int" |
248 |
|
249 |
self.xSize = xSize |
250 |
self.ySize = ySize |
251 |
|
252 |
return |
253 |
|
254 |
def getSize(self): |
255 |
""" |
256 |
Gets the current size of the scene |
257 |
|
258 |
This size is effectively the renderer window size. Returns a tuple |
259 |
of the x and y dimensions respectively, in pixel units(??). |
260 |
""" |
261 |
debugMsg("Called Scene.getSize()") |
262 |
|
263 |
return (self.xSize, self.ySize) |
264 |
|
265 |
def rendererCommand(self, command): |
266 |
""" |
267 |
Allows the user to run a low-level renderer-specific command directly |
268 |
|
269 |
@param command: The renderer command to run as a string |
270 |
@type command: string |
271 |
""" |
272 |
debugMsg("Called Scene.rendererCommand()") |
273 |
# check that we get a string as input |
274 |
if __debug__: |
275 |
assert isinstance(command, str), "Incorrect data type; expected str" |
276 |
|
277 |
evalString = "%s" % command |
278 |
self.renderer.runString(evalString) |
279 |
return |
280 |
|
281 |
# vim: expandtab shiftwidth=4: |