Package Pmv :: Module povrayCommands
[hide private]
[frames] | no frames]

Source Code for Module Pmv.povrayCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # 
 10  # $Header: /opt/cvs/python/packages/share1.5/Pmv/povrayCommands.py,v 1.4 2004/05/28 02:16:18 sanner Exp $ 
 11  # 
 12  # $Id: povrayCommands.py,v 1.4 2004/05/28 02:16:18 sanner Exp $ 
 13  # 
 14   
 15  import Tkinter, os 
 16  from Pmv.mvCommand import MVCommand 
 17  from ViewerFramework.VFCommand import CommandGUI 
 18  ##  from ViewerFramework.gui import InputFormDescr, InputForm 
 19  from mglutil.gui.InputForm.Tk.gui import InputFormDescr, InputForm 
 20   
 21  from mglutil.util.callback import CallBackFunction 
 22   
 23  from mglutil.gui.BasicWidgets.Tk.customizedWidgets import ListChooser 
 24   
 25  import Pmw 
 26   
27 -class Povray(MVCommand):
28 29 PovrayIncDir = '/mgl/apps/src/povray/povray31/include' 30
31 - def onAddCmdToViewer(self):
32 self.form = None 33 self.fileWritten = 0
34 35 ## def addInclude(self, value): 36 ## lb1 = value.entryByName['includes']['widget'] 37 ## #print lb1.get() 38 ## self.fileWritten = 0 39 ## lb2 = value.entryByName['selectedIncludes']['widget'] 40 ## lb2.insert('end', lb1.get()[0]) 41 42 ## def removeInclude(self, value): 43 ## self.fileWritten = 0 44 ## lb2 = value.entryByName['selectedIncludes']['widget'] 45 ## if lb2.get(): 46 ## lb2.remove(lb2.get()[0]) 47 48 ## def addGeometry(self, value): 49 ## self.fileWritten = 0 50 ## #print "value", value 51 ## lb1 = value.entryByName['geometries']['widget'] 52 ## lb2 = value.entryByName['selectedGeometries']['widget'] 53 ## lb2.insert('end', (lb1.get()[0],None)) 54 55 ## def removeGeometry(self, value): 56 ## self.fileWritten = 0 57 ## #print "value", value 58 ## lb2 = value.entryByName['selectedGeometries']['widget'] 59 ## if lb2.get(): 60 ## lb2.remove(lb2.get()[0]) 61
62 - def dismissCB(self):
63 self.form.destroy() 64 self.form = None
65 66
67 - def bindPigment(self, event=None):
68 ## e1 = self.form.descr.entryByName['pigment']['variable'] 69 e1 = self.form.descr.entryByName['pigment']['widget'] 70 prop = e1.get() 71 g = self.form.descr.entryByName['selectedGeometries']['widget'] 72 geom = g.get() 73 if len(geom) and len(prop): 74 ## self.properties[geom[0]]['pigment'] = prop 75 self.properties[geom[0][0]]['pigment'] = prop
76 77
78 - def bindFinish(self, event=None):
79 e1 = self.form.descr.entryByName['finish']['widget'] 80 ## e1 = self.form.descr.entryByName['finish']['variable'] 81 prop = e1.get() 82 g = self.form.descr.entryByName['selectedGeometries']['widget'] 83 geom = g.get() 84 if len(geom) and len(prop): 85 ## self.properties[geom[0]]['finish'] = prop 86 self.properties[geom[0][0]]['finish'] = prop
87 88
89 - def updateProps(self,event=None):
90 self.fileWritten = 0 91 ## e1 = self.form.descr.entryByName['pigment']['variable'] 92 ## e2 = self.form.descr.entryByName['finish']['variable'] 93 ## g = self.form.descr.entryByName['selectedGeometries']['widget'] 94 ## e1.set(self.properties[g.get()[0]]['pigment']) 95 ## e2.set(self.properties[g.get()[0]]['finish']) 96 w1 = self.form.descr.entryByName['pigment']['widget'] 97 w2 = self.form.descr.entryByName['finish']['widget'] 98 g = self.form.descr.entryByName['selectedGeometries']['widget'] 99 w1.setentry(self.properties[g.get()[0]]['pigment']) 100 w2.setentry(self.properties[g.get()[0]]['finish'])
101
102 - def runPovray(self, event=None):
103 if not self.fileWritten: 104 self.writeFile() 105 106 fn = self.filename 107 c = self.vf.GUI.VIEWER.cameras[0] 108 ## bin = self.form.descr.entryByName['povrayBin']['variable'].get() 109 ## args = self.form.descr.entryByName['povrayArgs']['variable'].get() 110 ## s = float(self.form.descr.entryByName['scaleImage']['variable'].get()) 111 ## outf = self.form.descr.entryByName['imageFile']['variable'].get() 112 bin = self.form.descr.entryByName['povrayBin']['widget'].get() 113 args = self.form.descr.entryByName['povrayArgs']['widget'].get() 114 s = float(self.form.descr.entryByName['scaleImage']['widget'].get()) 115 outf = self.form.descr.entryByName['imageFile']['widget'].get() 116 cmd = '%s +L%s +W%d +H%d %s -I%s' % (bin, self.PovrayIncDir, 117 int(s*c.width), 118 int(s*c.height), args, 119 self.filename) 120 if outf!='': 121 cmd = '%s +O%s'%(cmd, outf) 122 print cmd 123 124 import thread 125 def renderIt(c, cmd): 126 os.system(cmd)
127 128 thread.start_new_thread( renderIt, (c,cmd) )
129 130
131 - def writeFile(self, event=None):
132 from DejaVu.povray3 import PovRay 133 p = PovRay() 134 vi = self.vf.GUI.VIEWER 135 136 # handle camera and background checkbutton 137 wbg = self.form.descr.entryByName['wbg']['wcfg']['variable'].get() 138 if wbg==1: 139 col = vi.cameras[0].backgroundColor 140 vi.cameras[0].Set(color=(1.,1.,1.,1.), tagModified=False) 141 ## sl = float(self.form.descr.entryByName['scaleLight']['variable'].get()) 142 ## br = float(self.form.descr.entryByName['bondRadius']['variable'].get()) 143 sl = float(self.form.descr.entryByName['scaleLight']['widget'].get()) 144 br = float(self.form.descr.entryByName['bondRadius']['widget'].get()) 145 p.addCamera(vi.cameras[0], sl) 146 if wbg==1: vi.cameras[0].Set(color=col, tagModified=False) 147 148 # get list of selected geometries and add them 149 g = self.form.descr.entryByName['selectedGeometries']['widget'] 150 ## geoms = g.lb.get(0,'end') 151 geoms = g.get() 152 for ge in geoms: 153 p.addGeom(self.geomList[ge], self.properties[ge], br) 154 155 # get the file namer and write file 156 ## fw = self.form.descr.entryByName['fileName']['textvariable'] 157 fw = self.form.descr.entryByName['fileName']['widget'] 158 fn = fw.get() 159 if fn =='': 160 fn = self.vf.askFileSave(idir='.', ifile=None, 161 types=[('Povray', '*.pov'), ('all files', '*.*')], 162 title='Save Povray scene') 163 ## fw.set(fn) 164 fw.setentry(fn) 165 166 if fn: 167 self.filename=fn 168 p.write(fn) 169 self.fileWritten = 1
170 171
172 - def guiCallback(self):
173 174 if self.form: return 175 self.fileWritten = 0 176 # get a list of povray include files 177 inclist = os.listdir(self.PovrayIncDir) 178 inclist.sort() 179 180 # get a list of geometries 181 self.geomList = {} 182 self.properties = {} 183 for m in self.vf.Mols: 184 for g in m.geomContainer.geoms.values(): 185 if g.visible and g.__class__.__name__ != 'Geom': 186 name = '%s_%s'%(m.name,g.name) 187 self.geomList[name] = g 188 self.properties[name] = {'pigment':'', 189 'finish':'specular 1 roughness 0.001 ambient 0.3'} 190 191 for g in self.vf.GUI.VIEWER.rootObject.children: 192 if g.visible and g.__class__.__name__ != 'Geom': 193 name = g.name 194 self.geomList[name] = g 195 self.properties[name] = {'pigment':'', 196 'finish':'specular 1 roughness 0.001 ambient 0.3'} 197 198 idf = InputFormDescr(title ="PovRay") 199 incEntries = map(lambda x: (x,None),inclist) 200 idf.append({'name':'includes', 201 'widgetType':ListChooser, 202 'wcfg':{'entries': incEntries, 203 'title':'Select include files', 204 'mode':'extended', 205 'lbwcfg':{'height':5,'exportselection':0}}, 206 'gridcfg':{'rowspan':4,'column':0,'row':0}, 207 } ) 208 209 ## idf.append({'name':'includes','title':'Select include files', 210 ## 'widgetType':'ListChooser', 211 ## 'entries': inclist, 212 ## 'mode':'extended', 213 ## 'gridcfg':{'rowspan':4,'column':0,'row':0}, 214 ## 'lbwcfg':{'height':5}} ) 215 216 geomsEntries = map(lambda x: (x,None),self.geomList.keys()) 217 ## idf.append({'name':'geometries', 218 ## 'widgetType':ListChooser, 219 ## 'wcfg':{'title':'Select geometries', 220 ## 'entries': geomsEntries, 221 ## 'lbwcfg':{'height':5}}, 222 ## 'gridcfg':{'rowspan':4,'column':1,'row':0}} 223 ## ) 224 ## idf.append({'name':'geometries','title':'Select geometries', 225 ## 'widgetType':'ListChooser', 226 ## 'entries': self.geomList.keys(), 227 ## 'gridcfg':{'rowspan':4,'column':1,'row':0}, 228 ## 'lbwcfg':{'height':5}} ) 229 230 ## idf.append({'name':'AddIncButton', 231 ## 'widgetType':Tkinter.Button, 232 ## 'wcfg':{'text':'->', 233 ## 'command': CallBackFunction( self.addGeometry, 234 ## (idf))}, 235 ## 'gridcfg':{'column':2,'row':1}}) 236 237 ## idf.append({'name':'RemoveIncButton', 238 ## 'widgetType':Tkinter.Button, 239 ## 'wcfg':{'text':'<-', 240 ## 'command': CallBackFunction( self.removeGeometry, 241 ## (idf))}, 242 ## 'gridcfg':{'column':2,'row':2}}) 243 244 ## idf.append({'name':'selectedGeometries','title':'Selected geometries', 245 ## 'widgetType':'ListChooser', 246 ## 'entries': self.geomList.keys(), 247 ## 'lbwcfg':{'height':5}, 248 ## 'gridcfg':{'rowspan':4, 'column':3, 'row':0}}) 249 idf.append({'name':'selectedGeometries', 250 'widgetType':ListChooser, 251 'wcfg':{'title':'Selected geometries', 252 'entries': geomsEntries, 253 'mode':'single', 254 'lbwcfg':{'height':5,'exportselection':0}}, 255 'gridcfg':{'rowspan':4, 'column':3, 'row':0}}) 256 257 ## idf.append({'name':'pigment','label':'Pigment ', 258 ## 'widgetType':Tkinter.Entry, 'wcfg':{'width':40}, 259 ## 'gridcfg':{'columnspan':2, 'column':0, 260 ## 'row':4, 'sticky':'w'}}) 261 idf.append({'widgetType':Pmw.EntryField, 262 'name':'pigment', 263 'wcfg':{'labelpos':'w', 264 'label_text':'Pigment ', 265 'validate':None, 266 'entry_width':40}, 267 'gridcfg':{'columnspan':2, 'column':0, 268 'row':4, 'sticky':'w'}}) 269 270 ## idf.append({'name':'finish','label':'Finish ', 271 ## 'widgetType':Tkinter.Entry, 'wcfg':{'width':40}, 272 ## 'gridcfg':{'columnspan':2, 'column':0, 273 ## 'row':5, 'sticky':'w'}}) 274 idf.append({'widgetType':Pmw.EntryField, 275 'name':'finish', 276 'wcfg':{'labelpos':'w', 277 'label_text':'Finish ', 278 'validate':None, 279 'entry_width':40}, 280 'gridcfg':{'columnspan':2, 'column':0, 281 'row':5, 'sticky':'w'}}) 282 283 ## idf.append({'name':'scaleLight', 284 ## 'label':'Light scale factor', 'type':float, 285 ## 'widgetType':Tkinter.Entry, 'wcfg':{'width':5}, 286 ## 'defaultValue': '2.0', 287 ## 'gridcfg':{'column':3, 'row':4, 'sticky':'e'}}) 288 idf.append({'name':'scaleLight', 289 'widgetType':Pmw.EntryField, 290 'type':float, 291 'wcfg':{'labelpos':'w', 292 'label_text':'Light scale factor', 293 'validate':'real', 294 'value':2.0, 295 'entry_width':5}, 296 'gridcfg':{'column':3, 'row':4, 'sticky':'e'}}) 297 298 ## idf.append({'name':'scaleImage', 299 ## 'label':'scale Image', 'type':float, 300 ## 'widgetType':Tkinter.Entry, 'wcfg':{'width':5}, 301 ## 'defaultValue': '1.0', 302 ## 'gridcfg':{'column':3, 'row':5, 'sticky':'e'}}) 303 idf.append({'name':'scaleImage', 304 'widgetType':Pmw.EntryField, 305 'type':float, 306 'wcfg':{'labelpos':'w', 307 'label_text':'scale Image', 308 'entry_width':5, 309 'validate':'real', 310 'value':1.0}, 311 'gridcfg':{'column':3, 'row':5, 'sticky':'e'}}) 312 313 ## idf.append({'name':'bondRadius','label':'bond Radius', 'type':float, 314 ## 'widgetType':Tkinter.Entry, 'wcfg':{'width':5}, 315 ## 'defaultValue': '0.15', 316 ## 'gridcfg':{'column':3, 'row':6, 'sticky':'e'}}) 317 idf.append({'name':'bondRadius', 318 'widgetType':Pmw.EntryField, 319 'type':float, 320 'wcfg':{'labelpos':'w', 321 'label_text':'bond Radius', 322 'entry_width':5, 323 'validate':'real', 324 'value': 0.15}, 325 'gridcfg':{'column':3, 'row':6, 'sticky':'e'}}) 326 327 ## idf.append({'name':'fileName','label':'File Name', 328 ## 'widgetType':Tkinter.Entry, 329 ## 'defaultValue':'test.pov', 330 ## 'wcfg':{'width':40}, 331 ## 'gridcfg':{'columnspan':2,'column':0,'row':6,'sticky':'w'}}) 332 idf.append({'name':'fileName', 333 'widgetType':Pmw.EntryField, 334 'wcfg':{'labelpos':'w', 335 'label_text':'File Name', 336 'entry_width':40, 337 'value':'test.pov',}, 338 'gridcfg':{'columnspan':2,'column':0,'row':6,'sticky':'w'}}) 339 340 ## idf.append({'name':'povrayBin','label':'Povray binary', 341 ## 'widgetType':Tkinter.Entry, 342 ## 'defaultValue':'povray', 343 ## 'wcfg':{'width':40}, 344 ## 'gridcfg':{'columnspan':2,'column':0,'row':7,'sticky':'w'}}) 345 idf.append({'name':'povrayBin', 346 'widgetType':Pmw.EntryField, 347 'wcfg':{'labelpos':'w', 348 'label_text':'Povray binary', 349 'value':'povray', 350 'entry_width':40}, 351 'gridcfg':{'columnspan':2,'column':0, 352 'row':7,'sticky':'w'}}) 353 354 idf.append({'name':'wbg', 355 'widgetType':Tkinter.Checkbutton, 356 'defaultValue':1, 357 'wcfg':{'text':'White Background', 358 'variable': Tkinter.IntVar()}, 359 'gridcfg':{'column':3, 'row':7,'sticky':'e'}}) 360 361 ## idf.append({'name':'povrayArgs','label':'cmdline arguments', 362 ## 'widgetType':Tkinter.Entry, 363 ## 'defaultValue':'+D0 +P +X -V +FN +QR', 364 ## 'wcfg':{'width':40}, 365 ## 'gridcfg':{'columnspan':2,'column':0,'row':8,'sticky':'w'}}) 366 idf.append({'name':'povrayArgs', 367 'widgetType':Pmw.EntryField, 368 'wcfg':{'labelpos':'w', 369 'label_text':'cmdline arguments', 370 'value':'+D0 +P +X -V +FN +QR', 371 'entry_width':40}, 372 'gridcfg':{'columnspan':2,'column':0, 373 'row':8,'sticky':'w'}}) 374 375 ## idf.append({'name':'imageFile','label':'image file', 376 ## 'widgetType':Tkinter.Entry, 377 ## 'wcfg':{'width':40}, 378 ## 'gridcfg':{'columnspan':2,'column':0,'row':9,'sticky':'w'}}) 379 idf.append({'name':'imageFile', 380 'widgetType':Pmw.EntryField, 381 'wcfg':{'labelpos':'w', 382 'label_text':'image file', 383 'entry_width':40}, 384 'gridcfg':{'columnspan':2,'column':0, 385 'row':9,'sticky':'w'}}) 386 387 idf.append({'name':'WriteFile', 388 'widgetType':Tkinter.Button, 389 'wcfg':{'text':'WriteFile','command': self.writeFile}, 390 'gridcfg':{'columnspan':1,'column':0, 391 'row':10,'sticky':'ew'}}) 392 393 idf.append({'name':'Render', 394 'widgetType':Tkinter.Button, 395 'wcfg':{'text':'Render', 396 'command': CallBackFunction( self.runPovray, 397 (idf))}, 398 'gridcfg':{'columnspan':2,'column':1, 399 'row':10,'sticky':'ew'}}) 400 401 idf.append({'name':'Dismiss', 402 'widgetType':Tkinter.Button, 403 'wcfg':{'text':'Dismiss', 404 'command':self.dismissCB}, 405 'gridcfg':{'column':3, 'row':10,'sticky':'ew'}}) 406 407 self.form = self.vf.getUserInput(idf, modal = 0, blocking = 0) 408 lb = idf.entryByName['selectedGeometries']['widget'].lb 409 lb.bind("<ButtonRelease-1>", self.updateProps, '+') 410 411 e1 = idf.entryByName['pigment']['widget'] 412 e1.bind("<Return>", self.bindPigment) 413 414 e2 = idf.entryByName['finish']['widget'] 415 e2.bind("<Return>", self.bindFinish)
416 417 povrayGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 418 'menuButtonName':'File', 419 'menuEntryLabel':'Povray', 'index':0} 420 421 PovrayGUI = CommandGUI() 422 PovrayGUI.addMenuCommand('menuRoot', 'File', 'Povray', index = 0) 423 424 commandList = [ 425 {'name':'povray','cmd': Povray(), 'gui': PovrayGUI}, 426 ] 427 428
429 -def initModule(viewer):
430 for dict in commandList: 431 viewer.addCommand(dict['cmd'], dict['name'], dict['gui'])
432