Package parasol :: Module POV_Scene
[hide private]
[frames] | no frames]

Source Code for Module parasol.POV_Scene

  1  from POV_Basics import * 
  2  from POV_Items import * 
  3  import os, sys 
  4  import win32api 
  5  import tempfile 
  6   
  7   
8 -class POV_Scene(object):
9
10 - def __init__(self, filePrefix="myScene", ambient=0.25, addDefaultLights=1, 11 background="Black", viewMargin=1.06, lookAtOffset=None):
12 13 self.filePrefix = filePrefix 14 self.ambient = ambient # will set all textures ambient to this 15 self.background = background 16 self.viewMargin = viewMargin # multiplier on view bounding box for render 17 self.lookAtOffset = lookAtOffset 18 19 self.addDefaultLights = addDefaultLights 20 21 self.includeL = ["colors","transforms"] 22 self.lightL = [] 23 24 self.manualCamera = 0 25 self.camera = Camera(ang=10.0) 26 27 self.itemL = [] 28 self.transformList = []
29
30 - def translate(self, vectL=[1,1,1]):
31 self.transformList.append('translate '+ str(Vector(vectL)))
32
33 - def rotate(self, vectL=[0,20,0]):
34 self.transformList.append('rotate '+ str(Vector(vectL)))
35
36 - def setCameraAngle(self, angle=10.0):
37 self.camera.setAngle( ang=angle )
38
39 - def addItem(self, item):
40 if type(item) is DeclaredItem: 41 self.itemL.append(item) 42 else: 43 self.itemL.append( DeclaredItem(name='myItem', itemList=[item]) )
44
45 - def addInclude(self, inc="textures"):
46 if inc not in self.includeL: 47 self.includeL.append( inc )
48
49 - def addLight(self, pos=[10,10,-50], colorName="White", rgb=None):
50 ls = Light( pos, colorName ) 51 if rgb: 52 ls.setRGB( rgb ) 53 self.lightL.append( ls )
54
55 - def write(self, view="front", ortho=0, clockY=None, clockX=None, clockZ=None, 56 extraScenesL=None):
57 58 # using clockY or clockX will override the view and ortho inputs 59 60 self.POVFileName = self.filePrefix+'.pov' 61 62 f = open( self.filePrefix+'.pov', 'w') 63 finc = open( self.filePrefix+'.inc', 'w') 64 65 # put my Macros in the include file 66 finc.write( myMacros ) 67 68 # Includes 69 for inc in self.includeL: 70 f.write( '#include "%s.inc"\n'%inc ) 71 f.write( '#include "%s.inc"\n'%self.filePrefix ) 72 f.write( '\n' ) 73 74 # background color 75 f.write('background {%s}\n'%self.background) 76 77 # Light Sources 78 for ls in self.lightL: 79 f.write( str(ls) + '\n' ) 80 f.write( '\n' ) 81 82 # Give Declared Items Their Names, if not initialized 83 # Also check for duplicate names and correct if necessary 84 tempDict = {} #used to check for duplicate names 85 for i,item in enumerate(self.itemL): 86 if type(item) is DeclaredItem: 87 if item.name=='': 88 item.name = "myItem_%i"%(i+1,) 89 while tempDict.has_key(item.name.lower()): 90 item.name = item.name + '%i'%(i+1) 91 tempDict[item.name.lower()] = 1 92 93 for subItem in item.itemList: 94 subItem.texture.ambient = self.ambient 95 96 else: 97 item.texture.ambient = self.ambient 98 99 # Items 100 for item in self.itemL: 101 finc.write( str(item) + '\n\n' ) 102 103 if extraScenesL: 104 for exScene in extraScenesL: 105 for item in exScene.itemL: 106 finc.write( str(item) + '\n\n' ) 107 108 # Declared Items 109 f.write('#declare mainObject = \n') 110 f.write(' union { \n') 111 for item in self.itemL: 112 if type(item) is DeclaredItem: 113 #f.write( item.positionStr() + '\n' ) 114 115 f.write(' object { %s '%item.name ) 116 for ts in self.transformList: 117 f.write( ts + ' ' ) 118 f.write( ' }\n' ) 119 120 if extraScenesL: 121 for exScene in extraScenesL: 122 for item in exScene.itemL: 123 if type(item) is DeclaredItem: 124 #f.write( item.positionStr() ) 125 126 f.write(' object { %s '%item.name ) 127 for ts in exScene.transformList: 128 f.write( ts + ' ' ) 129 f.write( ' }\n' ) 130 131 132 f.write(' }\n\n') 133 134 # Show the collection of objects as "mainObject" and rotate for proper camera view 135 sL = ['#declare finalObject = object { mainObject '] 136 137 #for ts in self.transformList: 138 # sL.append( ts ) 139 140 if clockY or clockX or clockZ: 141 if clockX: 142 sL.append(' rotate <%g,0,0> '%clockX) 143 if clockY: 144 sL.append(' rotate <0,%g,0> '%clockY) 145 if clockZ: 146 sL.append(' rotate <0,0,%g> '%clockZ) 147 else: 148 if view=="top": 149 sL.append(' rotate <-90,0,0> ') 150 elif view=="bottom": 151 sL.append(' rotate <90,0,0> ') 152 elif view=="left": 153 sL.append(' rotate <0,-90,0> ') 154 elif view=="right": 155 sL.append(' rotate <0,90,0> ') 156 elif view=="back": 157 sL.append(' rotate <0,180,0> ') 158 159 if ortho: 160 sL.append(' rotate <45,45,0> ') 161 162 f.write( ''.join(sL) + '}\n\n') 163 164 f.write('finalObject\n\n') 165 166 # Camera 167 if self.manualCamera: 168 f.write( str(self.camera) ) 169 else: 170 # worry about manual camera placement later, camLoc = "<0,0,-100>" 171 camLoc = "LocationForAngleD(finalObject, %g, %g) angle %g "%(self.camera.angle, self.viewMargin,self.camera.angle) 172 f.write('#declare camLoc = LocationForAngleD(finalObject, %g, %g);\n'%(self.camera.angle, self.viewMargin)) 173 174 if self.lookAtOffset: 175 lookOffStr = ' + %s'%(str( Vector( self.lookAtOffset ) ),) 176 else: 177 lookOffStr = '' 178 179 #f.write('#debug vstr(3, LocationForAngleD(mainObject, %g), ",", 0, 1)\n'%self.camera.angle) 180 f.write('camera {location camLoc angle %g look_at ObjCenter(finalObject)%s}\n\n'%(self.camera.angle,lookOffStr)) 181 182 if self.addDefaultLights: 183 lightLoc = "LocationForAngleD(finalObject, %g, %g)"%(self.camera.angle, self.viewMargin) 184 f.write('light_source { LocationForLight1(finalObject, camLoc, 1) color White shadowless}\n') 185 f.write('light_source { LocationForLight1(finalObject, camLoc, 2) color White shadowless}\n') 186 f.write('light_source { LocationForLight1(finalObject, camLoc, 3) color White shadowless}\n') 187 188 f.write( '\n\n' ) 189 190 finc.close() 191 f.close()
192
193 - def findPovrayExe(self):
194 #fdInt ,tempPovFile = tempfile.mkstemp('.pov') 195 196 tempPovFile = tempfile.NamedTemporaryFile(suffix='.pov') 197 #print 'tempPovFile.name=',tempPovFile.name 198 199 povray_exe = win32api.FindExecutable(tempPovFile.name)[1] 200 print 'povray_exe=',povray_exe 201 202 return povray_exe
203
204 - def render(self, width=480, height=360, antialias=1, 205 pngFile = 'temp.png', povray_exe=''):
206 207 ''' 208 if input, the POV Ray executable would be something like: 209 povray_exe = r'"C:\Program Files\POV-Ray for Windows v3.6\bin\pvengine.exe"' 210 otherwise, just let the operating system find the EXE file 211 ''' 212 213 if not povray_exe: 214 povray_exe = self.findPovrayExe() 215 216 if 1:#try: 217 print 'rendering POV file:',self.POVFileName 218 219 if os.path.exists(pngFile): 220 os.unlink(pngFile) 221 222 if antialias: 223 ant="+A" 224 else: 225 ant="" 226 227 # /exit exits the GUI after rendering 228 # /nr suppresses loading of previous edit sessions 229 # +FN makes file format PNG 230 # -d suppresses display of rendering 231 # -GA all console off 232 # -P turns pause at end off 233 os.system("%s +FN +d /nr /exit +I%s +O%s +W%d +H%d %s"%( 234 povray_exe,self.POVFileName,pngFile,width,height,ant)) 235 236 else:#except: 237 print "WARNING... render routine FAILED."
238 239 240 if __name__ == '__main__': 241 242 243 244 scene = POV_Scene(ambient=0.3) 245 246 scene.findPovrayExe() 247 sys.exit() 248 249 #scene.camera.setSky( [1,1,0] ) 250 #scene.camera.setPos( [10,10,-20] ) 251 #scene.camera.setAngle( 10.0 ) 252 253 #scene.addLight( rgb=[1,1,1]) 254 #scene.addLight( [-10,-10,-20]) 255 #scene.addLight( [10,10,0]) 256 scene.addInclude("stones") 257 258 s = Sphere(texture=Texture(name="T_Grnt25",ambient=0.5)) 259 260 #scene.addItem( s ) 261 b1 = Box(corner1=[-3,-3,-3], corner2=[-0.5,-0.5,-0.5], 262 texture=Texture( rgb=[0.4, 0.72, 0.4, 0.6], ambient=0.5, imageMapFile='beer2.gif',imageMapType=0 ) ) 263 264 b1 = CSE_Tank(radius=1.5, ellRatio=1.414, cylLen=4.0, 265 texture=Texture( colorName="Gray50", ambient=0.5 )) 266 267 #scene.addItem( b1 ) 268 b2 = Box(corner1=[3,3,3], corner2=[1.5,1.5,1.5], 269 texture=Texture( colorName="CadetBlue", ambient=0.5 ) ) 270 #scene.addItem( b2 ) 271 d = DeclaredItem(name='SphBox', itemList=[b1,b2,s] ) 272 scene.addItem( d ) 273 274 s = Sphere(texture=Texture(name="T_Grnt25",ambient=0.5),center=[-1,1,1], radius=1.0) 275 276 s2 = Cone(bottom=[-1,1,4], Rbottom=1.5, top=[-1,2,5], Rtop=1.0, 277 texture=Texture(name="T_Grnt25",ambient=0.5)) 278 279 d2 = DeclaredItem(name='SphCone', itemList=[s,s2] ) 280 scene.addItem( d2 ) 281 282 283 d3 = DeclaredItem(name='Saucer', itemList=[\ 284 HollowCylinder(OD=10,texture=Texture( colorName="Pink", ambient=0.5 ))] ) 285 d3.translate([-2,1,0]) 286 scene.addItem( d3 ) 287 288 289 domeTex = Texture(ambient=0.25, colorName="Aquamarine", rgb=None, name=None, 290 imageMapFile='',imageMapType=0, bumpSize=0.01, transmit=0.15, filter=0.0, 291 reflection=0.0) 292 dd = EllDomeCaveDown(radius=10.0, ellRatio=1.414, texture=domeTex, openSOR=1) 293 dd.translate([0,4,0]) 294 scene.addItem( dd ) 295 296 du = EllDomeCaveUp(radius=10.0, ellRatio=1.414, texture=domeTex, openSOR=1) 297 du.translate([0,-2,0]) 298 scene.addItem( du ) 299 300 #d.scale([1.5,1.5,1.5]) 301 #d.rotate([90,0,0]) 302 #d.axisRotate( [1,1,1], 45) 303 #d.pointRotate( rotL=[0,45,0], pointL=[1,1,1]) 304 #d.alignAxes( axis1L=[0,1,0], axis2L=[1,1,1]) 305 #d.translate([5,5,0]) 306 #d.center(axes="x+y+z") ??? doesn't seem to work 307 #d.pointAlign( face="+x+y+z", pointL=[0,1,0]) 308 scene.write(view="", ortho=1, clockX=15., clockY=15.) 309 #scene.write(view="top", ortho=0) 310 scene.render() 311