MCPcopy Create free account
hub / github.com/vispy/vispy / __setitem__

Method __setitem__

vispy/visuals/shaders/function.py:190–278  ·  view source on GitHub ↗

Setting of replacements through a dict-like syntax. Each replacement can be: * verbatim code: ``fun1['foo'] = '3.14159'`` * a FunctionCall: ``fun1['foo'] = fun2()`` * a Variable: ``fun1['foo'] = Variable(...)`` (can be auto-generated)

(self, key, val)

Source from the content-addressed store, hash-verified

188 self._assignments = OrderedDict()
189
190 def __setitem__(self, key, val):
191 """Setting of replacements through a dict-like syntax.
192
193 Each replacement can be:
194 * verbatim code: ``fun1['foo'] = '3.14159'``
195 * a FunctionCall: ``fun1['foo'] = fun2()``
196 * a Variable: ``fun1['foo'] = Variable(...)`` (can be auto-generated)
197 """
198 # Check the key. Must be Varying, 'gl_X' or a known template variable
199 if isinstance(key, Variable):
200 if key.vtype == 'varying':
201 if self.name != 'main':
202 raise Exception("Varying assignment only alowed in 'main' "
203 "function.")
204 storage = self._assignments
205 else:
206 raise TypeError("Variable assignment only allowed for "
207 "varyings, not %s (in %s)"
208 % (key.vtype, self.name))
209 elif isinstance(key, str):
210 if any(map(key.startswith,
211 ('gl_PointSize', 'gl_Position', 'gl_FragColor'))):
212 storage = self._assignments
213 elif key in self.template_vars or key in ('pre', 'post'):
214 storage = self._expressions
215 else:
216 raise KeyError('Invalid template variable %r' % key)
217 else:
218 raise TypeError('In `function[key]` key must be a string or '
219 'varying.')
220
221 # If values already match, bail out now
222 if eq(storage.get(key), val):
223 return
224
225 # If we are only changing the value (and not the dtype) of a uniform,
226 # we can set that value and return immediately to avoid triggering a
227 # recompile.
228 if val is not None and not isinstance(val, Variable):
229 # We are setting a value. If there is already a variable set here,
230 # try just updating its value.
231 variable = storage.get(key, None)
232 if isinstance(variable, Variable):
233 if np.any(variable.value != val):
234 variable.value = val
235 self.changed(value_changed=True)
236 return
237
238 # Could not set variable.value directly; instead we will need
239 # to create a new ShaderObject
240 val = ShaderObject.create(val, ref=key)
241 if variable is val:
242 # This can happen if ShaderObject.create returns the same
243 # object (such as when setting a Transform).
244 return
245
246 # Remove old references, if any
247 oldval = storage.pop(key, None)

Callers

nothing calls this directly

Calls 9

eqFunction · 0.90
getMethod · 0.80
changedMethod · 0.80
_remove_depMethod · 0.80
_add_depMethod · 0.80
createMethod · 0.45
linkMethod · 0.45
expressionMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected