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

Source Code for Module DejaVu.scenarioInterface.actor

  1  from scenario.actor import  Actor, CustomActor 
  2  from scenario.datatypes import DataType 
  3  from scenario.interpolators import Interpolator 
  4  from SimPy.Simulation import now 
  5   
  6  ## class ActionWithRedraw(Action): 
  7  ##     # subclass Action to have these actions set a redraw flag in the director 
  8  ##     # so that the redraw process only triggers a redraw if something changed 
  9   
 10  ##     def __init__(self, *args, **kw): 
 11  ##         Action.__init__(self, *args, **kw) 
 12  ##         self.postStep_cb = (self.setGlobalRedrawFlag, (), {}) 
 13   
 14   
 15  ##     def setGlobalRedrawFlag(self): 
 16  ##         #print 'setting needs redraw at', now(), self._actor().name 
 17  ##         self._actor()._director().needsRedraw = True 
 18   
 19   
 20   
21 -class RedrawActor(Actor):
22
23 - def __init__(self, vi, initialValue = 0, datatype = DataType, interp = Interpolator):
24 Actor.__init__(self, "redraw", vi, initialValue, datatype = datatype, interp = interp)
25 #self.addAction( Action() ) 26 27
28 - def callFunction(self, value):
29 director = self._director() 30 if director.needsRedraw: 31 #print 'Redraw at', now() 32 self.object.OneRedraw() 33 director.needsRedraw = False
34 35
36 -class DejaVuActor(CustomActor):
37 """ 38 class for DejaVu actors. 39 initialValue= None - initial value of the object's attribute (object.name), 40 interp = None - interpolator class, 41 setFunction = None - function to set the value on the object, 42 if None, object.Set(name=value) will be used 43 getFunction = None - function that can be called to get the 44 current value of the attribute (object.name) 45 The function and its arguments have to be specified as a 46 3-tuple (func, *args, **kw). It will be called using 47 func(*(object,)+args), **kw) if it is a function 48 or func(*args, **kw) if it is a method; 49 if None, getattr(object, name) will be used to get the value 50 to set the value and getattr(geom, name) to get the value 51 """
52 - def __init__(self, name, object, initialValue=None, datatype=None, interp=None, 53 setFunction=None, getFunction=None):
54 55 assert isinstance(name, str) 56 self.varname = name 57 if not getFunction: 58 if hasattr(object, name): 59 getFunction = (getattr, (name,), {}) 60 CustomActor.__init__(self, object.name+"."+name, object, 61 initialValue, datatype, interp, setFunction=setFunction, getFunction=getFunction) 62 if self.hasGetFunction: 63 self.recording = True 64 self.guiVar = None
65
66 - def setValue(self, value):
67 if self.setFunction: 68 self.setFunction( *(self, value) ) 69 else: 70 self.object.Set( **{self.varname:value} )
71 72 73 74 from interpolators import MaterialInterpolator 75 from scenario.datatypes import FloatType, IntType, BoolType,IntVectorType,\ 76 FloatVectorType, IntVarType, FloatVarType, VarVectorType 77 78
79 -class DejaVuMaterialActor(DejaVuActor):
80
81 - def __init__(self, name, object, initialValue=None, datatype = DataType, interp = MaterialInterpolator):
82 83 DejaVuActor.__init__(self, name, object, initialValue, datatype, 84 interp, getFunction=self.getValue) 85 self.hasGetFunction = True
86
87 - def setValue(self, value):
88 object = self.object 89 i=0 90 for v,name in zip(value, ('ambi', 'emis', 'spec', 'shini')): 91 #if self.activeVars[i]: 92 object.Set(propName=name, materials=v) 93 i +=1
94
95 - def getValue(self):
96 mat = self.object.materials[1028].prop 97 return (mat[0], mat[2], mat[3], mat[4])
98 99 100 101 from scenario.interpolators import FloatVectorInterpolator, VarVectorInterpolator 102
103 -class DejaVuScissorActor(DejaVuActor):
104 """ This actor manages resizing of DejaVu object's scissor""" 105
106 - def __init__(self, name, object, initialValue=None, datatype=FloatVectorType, 107 interp=FloatVectorInterpolator):
108 DejaVuActor.__init__(self, name, object, initialValue, datatype, interp, getFunction=self.getValue) 109 self.hasGetFunction = True 110 self.varnames = ['scissorH', 'scissorW', 'scissorX', 'scissorY'] 111 112 self.activeVars = ['scissorH', 'scissorW', 'scissorX', 'scissorY']
113
114 - def setValue(self, value):
115 object = self.object 116 kw = {} 117 for i, var in enumerate (self.varnames): 118 if var in self.activeVars: 119 kw[var] = value[i] 120 object.Set(**kw)
121
122 - def getValue(self):
123 obj = self.object 124 return [float(obj.scissorH), float(obj.scissorW), 125 float(obj.scissorX), float(obj.scissorY)]
126 127 128 129 import Numeric 130
131 -class DejaVuClipZActor(DejaVuActor):
132 """ This actor manages the near and far camera's atributes""" 133
134 - def __init__(self, name, object, initialValue=None, datatype=FloatVectorType, 135 interp=FloatVectorInterpolator):
136 DejaVuActor.__init__(self, name, object, initialValue, datatype, 137 interp, getFunction=self.getValue) 138 self.hasGetFunction = True 139 self.varnames = ['near', 'far'] 140 141 self.activeVars = ['near', 'far']
142
143 - def setValue(self, value):
144 camera = self.object 145 kw = {} 146 for i, var in enumerate (self.varnames): 147 if var in self.activeVars: 148 kw[var] = value[i] 149 camera.Set(**kw) 150 camera.Redraw()
151
152 - def getValue(self):
153 c = self.object 154 return Numeric.array([c.near, c.far,], "f")
155 156
157 -class DejaVuFogActor(DejaVuActor):
158 """ This actor manages the near and far camera's atributes""" 159
160 - def __init__(self, name, object, initialValue=None, datatype=FloatVectorType, 161 interp=FloatVectorInterpolator):
162 DejaVuActor.__init__(self, name, object, initialValue, datatype, 163 interp, getFunction=self.getValue) 164 self.hasGetFunction = True 165 self.varnames = ['start', 'end'] 166 167 self.activeVars = ['start', 'end']
168
169 - def setValue(self, value):
170 camera = self.object 171 kw = {} 172 for i, var in enumerate (self.varnames): 173 if var in self.activeVars: 174 kw[var] = value[i] 175 camera.fog.Set(**kw) 176 camera.Redraw()
177
178 - def getValue(self):
179 c = self.object 180 return Numeric.array([c.fog.start, c.fog.end,], "f")
181 182 183 from scenario.interpolators import CompositeInterpolator, FloatVarScalarInterpolator 184
185 -class DejaVuLightColorActor(DejaVuActor):
186
187 - def __init__(self, name, object):
188 kw = {'interpolators': [VarVectorInterpolator, # for ambient 189 VarVectorInterpolator, # for diffuse 190 VarVectorInterpolator, # for specular 191 ] 192 } 193 interp = (CompositeInterpolator, (), kw) 194 DejaVuActor.__init__(self, name, object, None, DataType, interp, 195 getFunction=self.getValue) 196 self.varnames = ['ambient', 'diffuse', 'specular'] 197 self.activeVars = ['ambient', 'diffuse', 'specular'] 198 self.hasGetFunction = True
199
200 - def setValue(self, value):
201 object = self.object 202 kw = {} 203 for v,name in zip(value, ('ambient', 'diffuse', 'specular')): 204 if name in self.activeVars: 205 kw[name] = v 206 object.Set(**kw)
207
208 - def getValue(self):
209 obj = self.object 210 return (obj.ambient, obj.diffuse, obj.specular)
211
212 -class DejaVuSperesRadiiActor(DejaVuActor):
213 """ This actor manages the near and far camera's atributes""" 214
215 - def __init__(self, name, object, initialValue=None, datatype=FloatVarType, 216 interp=FloatVarScalarInterpolator):
217 218 DejaVuActor.__init__(self, name, object, initialValue, 219 datatype=datatype, interp=interp, 220 getFunction=self.getValue) 221 self.hasGetFunction = True 222 self.varnames = ['radii'] 223 self.activeVars = ['radii']
224
225 - def setValue(self, value):
226 object = self.object 227 object.Set(radii=value)
228
229 - def getValue(self):
230 object = self.object 231 if object.oneRadius: 232 return object.radius 233 else: 234 return object.vertexSet.radii.array.flat
235 236
237 -def getAllSubclasses(klass):
238 # recursively build a list of all sub-classes 239 bases = klass.__bases__ 240 klassList = list(bases) 241 for k in bases: 242 klassList.extend( getAllSubclasses(k) ) 243 return klassList
244 245 246 import inspect 247 from actorsDescr import actorsDescr 248 from mglutil.gui.BasicWidgets.Tk.thumbwheel import ThumbWheel 249 from scenario.interpolators import FloatScalarInterpolator, IntScalarInterpolator
250 -def getAnimatableAttributes(object):
251 # return two dicts that contain a dict for each attribute of object 252 # that can be animated. The first dict is for attribute foudn in 253 # actorsDescr, the second is for attributes picked up on the fly 254 255 # merge the dict of attribute for all base classes 256 d1 = {} 257 for klass, d2 in actorsDescr.items(): 258 if isinstance(object, klass): 259 d1.update(d2) 260 261 d2 = {} 262 # find all attribute that are float 263 attrs = inspect.getmembers(object, lambda x: isinstance(x, float)) 264 for name,value in attrs: 265 if d1.has_key(name): continue 266 d2[name] = { 267 'interp': FloatScalarInterpolator, 268 'interpKw': {'values':[value, value]}, 269 'valueWidget': ThumbWheel, 270 'valueWidgetKw': {'type': 'float', 'initialValue':value}, 271 } 272 273 # find all attribute that are bool or int 274 attrs = inspect.getmembers(object, lambda x: isinstance(x, int)) 275 for name,value in attrs: 276 if d1.has_key(name): continue 277 d2[name] = { 278 'interp': IntScalarInterpolator, 279 'interpKw': {'values':[value, value]}, 280 'valueWidget': ThumbWheel, 281 'valueWidgetKw': {'type':'int', 'initialValue':value}, 282 } 283 return d1, d2
284 285
286 -def getDejaVuActor(object, attribute):
287 # return a DejaVu Actor given a DejaVu object and attribute name 288 baseClasses = [object.__class__] 289 baseClasses.extend( getAllSubclasses(object.__class__) ) 290 291 print 'getDejaVuActor', object,attribute 292 for klass in baseClasses: 293 d1 = actorsDescr.get(klass, None) 294 if d1: 295 d2 = d1.get(attribute, None) 296 if d2: 297 actorCalss, args, kw = d2['actor'] 298 args = (attribute,object) + args 299 actor = actorCalss( *args, **kw ) 300 301 return actor 302 ## else: # attribute not found in dictionary 303 ## if attribute in object.keywords: # it is setable 304 ## if hasattr(object, attribute): 305 ## actor = DejaVuActorSetGetattr( 306 ## object, name=actorName, setName=attribute, 307 ## getName=attribute) 308 ## else: 309 ## return None # FIXME 310 ## else: 311 312 313 return None
314