Q:

PsychoPy: How to change number of elements in DotStim or ElementArrayStim?

Say I generated a dots = psychopy.visual.DotStim. Is it possible to change the number of dots later? dots.nDots = 5 leads to an error on the next dots.draw() because the underlying matrices don't match:
Traceback (most recent call last):
  File "/home/jonas/Documents/projects/work pggcs/experiment/dots.py", line 32, in <module>
    dots_right.draw()
  File "/usr/lib/python2.7/dist-packages/psychopy/visual/dot.py", line 279, in draw
    self._update_dotsXY()
  File "/usr/lib/python2.7/dist-packages/psychopy/visual/dot.py", line 362, in _update_dotsXY
    self._verticesBase[:,0] += self.speed*numpy.reshape(numpy.cos(self._dotsDir),(self.nDots,))
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 218, in reshape
    return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged

The same is true of psychopy.visual.ElementArrayStim for which setting stim.nElements = 5 similarly results in an error on the next draw.
A solution is of course to instantiate a whole new DotStim or ElementArrayStim every time the number of dots/elements should change but this seems too heavy.

A:

It can be fixed for the DotStim:
dots.nDots = 5
dots._dotsDir = [0]*dots.nDots
dots. _verticesBase = dots._newDotsXY(dots.nDots)

This set movement of all dots to 0 but you can change that value to whatever you like or specify for individual dots. This is a hack which will likely break if you modify other aspects of the DotStim.
I haven't figured out a solution for ElementArrayStim.

