Package DejaVu :: Package scenarioInterface
[hide private]
[frames] | no frames]

Source Code for Package DejaVu.scenarioInterface

  1  try: 
  2      import Tkinter 
  3   
  4      from scenario.director import Director 
  5      from scenario.gui import DirectorGUI 
  6      from scenario.actor import Actor 
  7       
  8      from mglutil.util.callback import CallbackFunction 
  9   
 10      from DejaVu.Geom import Geom 
 11      from DejaVu.Camera import Camera 
 12      from DejaVu.Light import Light 
 13      from DejaVu.Clip import ClippingPlane  
 14      from DejaVu.Viewer import SetCurrentObjectEvent, SetCurrentCameraEvent, SetCurrentLightEvent, SetCurrentClipEvent 
 15   
 16      from actor import RedrawActor, getAnimatableAttributes 
 17   
18 - class DejaVuScenario:
19 """Class binding a DejaVu Viewer to a Scenario Director """ 20 21 ## This class allows to connect a DejaVu Viewer to a scenario Director 22 ## The DejaVu viewer will have a new attribute director. 23 ## a RedrawActor is added automatically to the director 24 ## The director gets a new attribute .needRedraw that the RerawActor uses to 25 ## optimize redraw calls 26 ## A menu buttons are added to the Viewer GUI to allow animation of objects 27 ## A callback function is register in the viewer to handle setting the current( 28 ## Object, Camera, light, ciping plane. 29 ## DejaVu object that are animated get two new attribute: 30 ## .animatedProperties which is a dict { object.name+'.'+propname : Actor instance } 31 ## .object.animatableProps which is an list of 2 dictionaries. 32 ## Both dicts contain {attribute name:{actorDescr}} items. The first 33 ## dict contains attributes explicitely decalared, the second contains 34 ## attributes picked up on the fly 35 ## 36
37 - def __init__(self, vi, director=None):
38 self.vi = vi 39 if director is None: 40 self.addDirector(vi) 41 else: 42 self.setDirector(vi, director)
43 44
45 - def addDirector(self, vi):
46 if hasattr(vi, 'director'): 47 raise RuntimeError, 'Viewer already has an director' 48 49 director = Director() 50 dg = DirectorGUI(director) 51 dg.root.protocol('WM_DELETE_WINDOW', dg.root.withdraw) 52 self.setDirector(vi, director)
53 54
55 - def setDirector(self, vi, director):
56 vi.director = director 57 58 # add needsRedraw attribute 59 director.needsRedraw = False 60 # add RedrawActor actor 61 #actor = RedrawActor(vi) 62 #actor.setKeyframe(50,0) 63 #director.addActor(actor ) 64 director.gui.setDuration(50) 65 66 # add menu button to ViewerGUI 67 # Objects 68 self.animObjMenuB = menuB = Tkinter.Menubutton( 69 vi.GUI.inheritF, text='Animate', relief='raise' ) 70 menuB.menu = Tkinter.Menu(menuB) 71 menuB['menu'] = menuB.menu 72 menuB.pack(fill='x') 73 74 # Cameras 75 self.animCamMenuB = menuB = Tkinter.Menubutton( 76 vi.GUI.CameraProp, text='Animate', relief='raise' ) 77 menuB.menu = Tkinter.Menu(menuB) 78 menuB['menu'] = menuB.menu 79 menuB.pack(fill='x') 80 81 # Lights 82 self.animLightMenuB = menuB = Tkinter.Menubutton( 83 vi.GUI.LightProp, text='Animate', relief='raise' ) 84 menuB.menu = Tkinter.Menu(menuB) 85 menuB['menu'] = menuB.menu 86 menuB.pack(fill='x') 87 88 # Cliping planes 89 self.animClipMenuB = menuB = Tkinter.Menubutton( 90 vi.GUI.ClipProp, text='Animate', relief='raise' ) 91 menuB.menu = Tkinter.Menu(menuB) 92 menuB['menu'] = menuB.menu 93 menuB.pack(fill='x') 94 95 # register interest in setting current(object, camera, light, clip) 96 func = self.setCurrentObjectAttrList_cb 97 vi.registerListener(SetCurrentObjectEvent, func) 98 vi.registerListener(SetCurrentCameraEvent, func) 99 vi.registerListener(SetCurrentLightEvent, func) 100 vi.registerListener(SetCurrentClipEvent, func) 101 102 # call the callback once to fill menus for current object 103 self.setCurrentObjectAttrList(vi.currentObject) 104 self.setCurrentObjectAttrList(vi.currentCamera) 105 self.setCurrentObjectAttrList(vi.currentClip) 106 self.setCurrentObjectAttrList(vi.currentLight)
107 108
109 - def setCurrentObjectAttrList_cb(self, event):
110 111 self.setCurrentObjectAttrList(event.object)
112 113
114 - def setCurrentObjectAttrList(self, object, event=None):
115 116 if not hasattr(object, 'animatableProps'): 117 object.animatableProps = getAnimatableAttributes(object) 118 if not hasattr(object, 'animatedProperties'): 119 object.animatedProperties = {} 120 121 self.createCheckbuttons(object)
122 123
124 - def createCheckbuttons(self, object):
125 # get menu button 126 if isinstance(object, Geom): menuB = self.animObjMenuB 127 elif isinstance(object, Camera): menuB = self.animCamMenuB 128 elif isinstance(object, Light): menuB = self.animLightMenuB 129 elif isinstance(object, ClippingPlane): menuB = self.animClipMenuB 130 else: 131 print 'no menu button for object:', object, object.__class__ 132 return 133 134 # clear menu's content 135 menuB.menu.delete(0, 'end') 136 137 # get list of properties 138 p1 = object.animatableProps[0].keys() 139 p1.sort() 140 propnames = p1 141 #p2 = object.animatableProps[1].keys() 142 #p2.sort() 143 #propnames = p1+p2 144 145 # get dict of currently animated properties for this object 146 aprop = object.animatedProperties 147 148 for i, name in enumerate(propnames): 149 var = Tkinter.IntVar() 150 if aprop.has_key(object.name+'.'+name): 151 var.set(1) 152 aprop[object.name+'.'+name].guiVar = var 153 cb = CallbackFunction( self.toggleAnimatePropVar, name, var, object) 154 menuB.menu.add_checkbutton( 155 label=name, command=cb, variable = var)
156 157
158 - def toggleAnimatePropVar(self, name, var, object):
159 # toggle variable associated with the checkbutton for this 160 # object.name actor 161 value = var.get() 162 if value: 163 self.createActor(object, name, var) 164 else: 165 self.deleteActor(object, name)
166 167
168 - def deleteActor(self, object, propname):
169 # delete the actor for object.name 170 actor = object.animatedProperties[object.name+'.'+propname] 171 self.vi.director.deleteActor(actor) 172 del object.animatedProperties[object.name+'.'+propname]
173
174 - def createActor(self, object, propname, variable = None):
175 # create an actor for object.propname 176 descr = object.animatableProps[0][propname] 177 178 #create the actor 179 actorClass, args, kw = descr['actor'] 180 actor = actorClass( *(propname, object)+args, **kw ) 181 actor.guiVar = variable 182 object.animatedProperties[object.name+'.'+propname] = actor 183 object.viewer.director.addActor(actor)
184 #actor.configure(recording = 'ready') 185 186 187 except ImportError: 188 import traceback 189 traceback.print_exc() 190 traceback.print_stack() 191 print 'WARNING: failed to import package scenario' 192