Window

A class representing a window for displaying one or more stimuli.

class psychopy.visual.Window(size=(800, 600), pos=None, color=(0, 0, 0), colorSpace='rgb', backgroundImage=None, backgroundFit='cover', rgb=None, dkl=None, lms=None, fullscr=None, allowGUI=None, monitor=None, bitsMode=None, winType=None, units=None, gamma=None, blendMode='avg', screen=0, viewScale=None, viewPos=None, viewOri=0.0, waitBlanking=True, allowStencil=False, multiSample=False, numSamples=2, stereo=False, name='window1', title='PsychoPy', checkTiming=True, useFBO=False, useRetina=True, autoLog=True, gammaErrorPolicy='raise', bpc=(8, 8, 8), depthBits=8, stencilBits=8, backendConf=None, infoMsg=None)[source]

Used to set up a context in which to draw objects, using either pyglet, pygame, or glfw.

The pyglet backend allows multiple windows to be created, allows the user to specify which screen to use (if more than one is available, duh!) and allows movies to be rendered.

The GLFW backend is a new addition which provides most of the same features as pyglet, but provides greater flexibility for complex display configurations.

Pygame may still work for you but it’s officially deprecated in this project (we won’t be fixing pygame-specific bugs).

These attributes can only be set at initialization. See further down for a list of attributes which can be changed after initialization of the Window, e.g. color, colorSpace, gamma etc.

Parameters:
  • size (array-like of int) – Size of the window in pixels [x, y].

  • pos (array-like of int) – Location of the top-left corner of the window on the screen [x, y].

  • color (array-like of float) – Color of background as [r, g, b] list or single value. Each gun can take values between -1.0 and 1.0.

  • fullscr (bool or None) – Create a window in ‘full-screen’ mode. Better timing can be achieved in full-screen mode.

  • allowGUI (bool or None) – If set to False, window will be drawn with no frame and no buttons to close etc., use None for value from preferences.

  • winType (str or None) – Set the window type or back-end to use. If None then PsychoPy will revert to user/site preferences.

  • monitor (Monitor or None) – The monitor to be used during the experiment. If None a default monitor profile will be used.

  • units (str or None) – Defines the default units of stimuli drawn in the window (can be overridden by each stimulus). Values can be None, ‘height’ (of the window), ‘norm’ (normalised), ‘deg’, ‘cm’, ‘pix’. See Units for the window and stimuli for explanation of options.

  • screen (int) – Specifies the physical screen that stimuli will appear on (‘pyglet’ and ‘glfw’ winType only). Values can be >0 if more than one screen is present.

  • viewScale (array-like of float or None) – Scaling factors [x, y] to apply custom scaling to the current units of the Window instance.

  • viewPos (array-like of float or None) – If not None, redefines the origin within the window, in the units of the window. Values outside the borders will be clamped to lie on the border.

  • viewOri (float) – A single value determining the orientation of the view in degrees.

  • waitBlanking (bool or None) – After a call to flip() should we wait for the blank before the script continues.

  • bitsMode (bool) – DEPRECATED in 1.80.02. Use BitsSharp class from pycrsltd instead.

  • checkTiming (bool) – Whether to calculate frame duration on initialization. Estimated duration is saved in monitorFramePeriod. The message displayed on the screen can be set with the infoMsg argument.

  • allowStencil (bool) – When set to True, this allows operations that use the OpenGL stencil buffer (notably, allowing the Aperture to be used).

  • multiSample (bool) – If True and your graphics driver supports multisample buffers, multiple color samples will be taken per-pixel, providing an anti-aliased image through spatial filtering. This setting cannot be changed after opening a window. Only works with ‘pyglet’ and ‘glfw’ winTypes, and useFBO is False.

  • numSamples (int) – A single value specifying the number of samples per pixel if multisample is enabled. The higher the number, the better the image quality, but can delay frame flipping. The largest number of samples is determined by GL_MAX_SAMPLES, usually 16 or 32 on newer hardware, will crash if number is invalid.

  • stereo (bool) – If True and your graphics card supports quad buffers then this will be enabled. You can switch between left and right-eye scenes for drawing operations using setBuffer().

  • title (str) – Name of the Window according to your Operating System. This is the text which appears on the title sash.

  • useRetina (bool) – In PsychoPy >1.85.3 this should always be True as pyglet (or Apple) no longer allows us to create a non-retina display. NB when you use Retina display the initial win size request will be in the larger pixels but subsequent use of units='pix' should refer to the tiny Retina pixels. Window.size will give the actual size of the screen in Retina pixels.

  • gammaErrorPolicy (str) – If raise, an error is raised if the gamma table is unable to be retrieved or set. If warn, a warning is raised instead. If ignore, neither an error nor a warning are raised.

  • bpc (array_like or int) – Bits per color (BPC) for the back buffer as a tuple to specify bit depths for each color channel separately (red, green, blue), or a single value to set all of them to the same value. Valid values depend on the output color depth of the display (screen) the window is set to use and the system graphics configuration. By default, it is assumed the display has 8-bits per color (8, 8, 8). Behaviour may be undefined for non-fullscreen windows, or if multiple screens are attached with varying color output depths.

  • depthBits (int) – Back buffer depth bits. Default is 8, but can be set higher (eg. 24) if drawing 3D stimuli to minimize artifacts such a ‘Z-fighting’.

  • stencilBits (int) – Back buffer stencil bits. Default is 8.

  • backendConf (dict or None) – Additional options to pass to the backend specified by winType. Each backend may provide unique functionality which may not be available across all of them. This allows you to pass special configuration options to a specific backend to configure the feature.

  • infoMsg (str or None) – Message to display during frame rate measurement (i.e., when checkTiming=True). Default is None, which means that a default message is displayed. If you want to hide the message, pass an empty string.

Notes

  • Some parameters (e.g. units) can now be given default values in the user/site preferences and these will be used if None is given here. If you do specify a value here it will take precedence over preferences.

size

Dimensions of the window’s drawing area/buffer in pixels [w, h].

Type:

array-like (float)

monitorFramePeriod

Refresh rate of the display if checkTiming=True on window instantiation.

Type:

float

_assignFlipTime(obj, attrib, format=<class 'float'>)[source]

Helper function to assign the time of last flip to the obj.attrib

Parameters:
  • obj (dict or object) – A mutable object (usually a dict of class instance).

  • attrib (str) – Key or attribute of obj to assign the flip time to.

  • format (str, class or None) – Format in which to return time, see clock.Timestamp.resolve() for more info. Defaults to float.

_checkMatchingSizes(requested, actual)[source]

Checks whether the requested and actual screen sizes differ. If not then a warning is output and the window size is set to actual

_cleanEditables()[source]

Make sure there are no dead refs in the editables list

_endOfFlip(clearBuffer)[source]

Override end of flip with custom color channel masking if required.

_getFrame(rect=None, buffer='front')[source]

Return the current Window as an image.

_getPixels(rect=None, buffer='front', includeAlpha=True, makeLum=False)[source]

Return an array of pixel values from the current window buffer or sub-region.

Parameters:
  • rect (tuple[int], optional) – The region of the window to capture in pixel coordinates (left, bottom, width, height). If None, the whole window is captured.

  • buffer (str, optional) – Buffer to capture.

  • includeAlpha (bool, optional) – Include the alpha channel in the returned array. Default is True.

  • makeLum (bool, optional) – Convert the RGB values to luminance values. Values are rounded to the nearest integer. Default is False.

Returns:

Pixel values as a 3D array of shape (height, width, channels). If includeAlpha is False, the array will have shape (height, width, 3). If makeLum is True, the array will have shape (height, width).

Return type:

ndarray

Examples

Get the pixel values of the whole window:

pix = win._getPixels()

Get pixel values and convert to luminance and get average:

pix = win._getPixels(makeLum=True)
average = pix.mean()
_getRegionOfFrame(rect=(-1, 1, 1, -1), buffer='front', power2=False, squarePower2=False)[source]

Deprecated function, here for historical reasons. You may now use :py:attr:`~Window._getFrame() and specify a rect to get a sub-region, just as used here.

power2 can be useful with older OpenGL versions to avoid interpolation in PatchStim. If power2 or squarePower2, it will expand rect dimensions up to next power of two. squarePower2 uses the max dimensions. You need to check what your hardware & OpenGL supports, and call _getRegionOfFrame() as appropriate.

_renderFBO()[source]

Perform a warp operation.

(in this case a copy operation without any warping)

_setCurrent()[source]

Make this window’s OpenGL context current.

If called on a window whose context is current, the function will return immediately. This reduces the number of redundant calls if no context switch is required. If useFBO=True, the framebuffer is bound after the context switch.

_setupGL()[source]

Setup OpenGL state for this window.

_setupGamma(gammaVal)[source]

A private method to work out how to handle gamma for this Window given that the user might have specified an explicit value, or maybe gave a Monitor.

_startOfFlip()[source]

Custom hardware classes may want to prevent flipping from occurring and can override this method as needed.

Return True to indicate hardware flip.

addEditable(editable)[source]

Adds an editable element to the screen (something to which characters can be sent with meaning from the keyboard).

The current editable object receiving chars is Window.currentEditable

Parameters:

editable

Returns:

property ambientLight

Ambient light color for the scene [r, g, b, a]. Values range from 0.0 to 1.0. Only applicable if useLights is True.

Examples

Setting the ambient light color:

win.ambientLight = [0.5, 0.5, 0.5]

# don't do this!!!
win.ambientLight[0] = 0.5
win.ambientLight[1] = 0.5
win.ambientLight[2] = 0.5
applyEyeTransform(clearDepth=True)[source]

Apply the current view and projection matrices.

Matrices specified by attributes viewMatrix and projectionMatrix are applied using ‘immediate mode’ OpenGL functions. Subsequent drawing operations will be affected until flip() is called.

All transformations in GL_PROJECTION and GL_MODELVIEW matrix stacks will be cleared (set to identity) prior to applying.

Parameters:

clearDepth (bool) – Clear the depth buffer. This may be required prior to rendering 3D objects.

Examples

Using a custom view and projection matrix:

# Must be called every frame since these values are reset after
# `flip()` is called!
win.viewMatrix = viewtools.lookAt( ... )
win.projectionMatrix = viewtools.perspectiveProjectionMatrix( ... )
win.applyEyeTransform()
# draw 3D objects here ...
property aspect

Aspect ratio of the current viewport (width / height).

backgroundFit

How should the background image of this window fit? Options are:

None, “None”, “none”

No scaling is applied, image is present at its pixel size unaltered.

“cover”

Image is scaled such that it covers the whole screen without changing its aspect ratio. In other words, both dimensions are evenly scaled such that its SHORTEST dimension matches the window’s LONGEST dimension.

“contain”

Image is scaled such that it is contained within the screen without changing its aspect ratio. In other words, both dimensions are evenly scaled such that its LONGEST dimension matches the window’s SHORTEST dimension.

“scaleDown”, “scale-down”, “scaledown”

If image is bigger than the window along any dimension, it will behave as if backgroundFit were “contain”. Otherwise, it will behave as if backgroundFit were None.

backgroundImage

Background image for the window, can be either a visual.ImageStim object or anything which could be passed to visual.ImageStim.image to create one. Will be drawn each time win.flip() is called, meaning it is always below all other contents of the window.

blendMode

Blend mode to use.

callOnFlip(function, *args, **kwargs)[source]

Call a function immediately after the next flip() command.

The first argument should be the function to call, the following args should be used exactly as you would for your normal call to the function (can use ordered arguments or keyword arguments as normal).

e.g. If you have a function that you would normally call like this:

pingMyDevice(portToPing, channel=2, level=0)

then you could call callOnFlip() to have the function call synchronized with the frame flip like this:

win.callOnFlip(pingMyDevice, portToPing, channel=2, level=0)
clearAutoDraw()[source]

Remove all autoDraw components, meaning they get autoDraw set to False and are not added to any list (as in .stashAutoDraw)

clearBuffer(color=True, depth=False, stencil=False)[source]

Clear the present buffer (to which you are currently drawing) without flipping the window.

Useful if you want to generate movie sequences from the back buffer without actually taking the time to flip the window.

Set color prior to clearing to set the color to clear the color buffer to. By default, the depth buffer is cleared to a value of 1.0.

Parameters:
  • color (bool) – Buffers to clear.

  • depth (bool) – Buffers to clear.

  • stencil (bool) – Buffers to clear.

Examples

Clear the color buffer to a specified color:

win.color = (1, 0, 0)
win.clearBuffer(color=True)

Clear only the depth buffer, depthMask must be True or else this will have no effect. Depth mask is usually True by default, but may change:

win.depthMask = True
win.clearBuffer(color=False, depth=True, stencil=False)
close()[source]

Close the window (and reset the Bits++ if necess).

property color

Set the color of the window.

This command sets the color that the blank screen will have on the next clear operation. As a result it effectively takes TWO flip() operations to become visible (the first uses the color to create the new screen, the second presents that screen to the viewer). For this reason, if you want to changed background color of the window “on the fly”, it might be a better idea to draw a Rect that fills the whole window with the desired Rect.fillColor attribute. That’ll show up on first flip.

See other stimuli (e.g. GratingStim.color) for more info on the color attribute which essentially works the same on all PsychoPy stimuli.

See Color spaces for further information about the ways to specify colors and their various implications.

property colorSpace

The name of the color space currently being used

Value should be: a string or None

For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).

Please note that changing colorSpace does not change stimulus parameters. Thus you usually want to specify colorSpace before setting the color. Example:

# A light green text
stim = visual.TextStim(win, 'Color me!',
                       color=(0, 1, 0), colorSpace='rgb')

# An almost-black text
stim.colorSpace = 'rgb255'

# Make it light green again
stim.color = (128, 255, 128)
property contentScaleFactor

Scaling factor (float) to use when drawing to the backbuffer to convert framebuffer to client coordinates.

property convergeOffset

Convergence offset from monitor in centimeters.

This is value corresponds to the offset from screen plane to set the convergence plane (or point for toe-in projections). Positive offsets move the plane farther away from the viewer, while negative offsets nearer. This value is used by setPerspectiveView and should be set before calling it to take effect.

Notes

  • This value is only applicable for setToeIn and setOffAxisView.

coordToRay(screenXY)[source]

Convert a screen coordinate to a direction vector.

Takes a screen/window coordinate and computes a vector which projects a ray from the viewpoint through it (line-of-sight). Any 3D point touching the ray will appear at the screen coordinate.

Uses the current viewport and projectionMatrix to calculate the vector. The vector is in eye-space, where the origin of the scene is centered at the viewpoint and the forward direction aligned with the -Z axis. A ray of (0, 0, -1) results from a point at the very center of the screen assuming symmetric frustums.

Note that if you are using a flipped/mirrored view, you must invert your supplied screen coordinates (screenXY) prior to passing them to this function.

Parameters:

screenXY (array_like) – X, Y screen coordinate. Must be in units of the window.

Returns:

Normalized direction vector [x, y, z].

Return type:

ndarray

Examples

Getting the direction vector between the mouse cursor and the eye:

mx, my = mouse.getPos()
dir = win.coordToRay((mx, my))

Set the position of a 3D stimulus object using the mouse, constrained to a plane. The object origin will always be at the screen coordinate of the mouse cursor:

# the eye position in the scene is defined by a rigid body pose
win.viewMatrix = camera.getViewMatrix()
win.applyEyeTransform()

# get the mouse location and calculate the intercept
mx, my = mouse.getPos()
ray = win.coordToRay([mx, my])
result = intersectRayPlane(   # from mathtools
    orig=camera.pos,
    dir=camera.transformNormal(ray),
    planeOrig=(0, 0, -10),
    planeNormal=(0, 1, 0))

# if result is `None`, there is no intercept
if result is not None:
    pos, dist = result
    objModel.thePose.pos = pos
else:
    objModel.thePose.pos = (0, 0, -10)  # plane origin

If you don’t define the position of the viewer with a RigidBodyPose, you can obtain the appropriate eye position and rotate the ray by doing the following:

pos = numpy.linalg.inv(win.viewMatrix)[:3, 3]
ray = win.coordToRay([mx, my]).dot(win.viewMatrix[:3, :3])
# then ...
result = intersectRayPlane(
    orig=pos,
    dir=ray,
    planeOrig=(0, 0, -10),
    planeNormal=(0, 1, 0))
property cullFace

True if face culling is enabled.`

property cullFaceMode

Face culling mode, either back, front or both.

property currentEditable

The editable (Text?) object that currently has key focus

property depthFunc

Depth test comparison function for rendering.

property depthMask

True if depth masking is enabled. Writing to the depth buffer will be disabled.

property depthTest

True if depth testing is enabled.

property draw3d

True if 3D drawing is enabled on this window.

property eyeOffset

Eye offset in centimeters.

This value is used by setPerspectiveView to apply a lateral offset to the view, therefore it must be set prior to calling it. Use a positive offset for the right eye, and a negative one for the left. Offsets should be the distance to from the middle of the face to the center of the eye, or half the inter-ocular distance.

property farClip

Distance to the far clipping plane in meters.

flip(clearBuffer=True)[source]

Flip the front and back buffers after drawing everything for your frame. (This replaces the update() method, better reflecting what is happening underneath).

Parameters:

clearBuffer (bool, optional) – Clear the draw buffer after flipping. Default is True.

Returns:

Wall-clock time in seconds the flip completed. Returns None if waitBlanking is False.

Return type:

float or None

Notes

  • The time returned when waitBlanking is True corresponds to when the graphics driver releases the draw buffer to accept draw commands again. This time is usually close to the vertical sync signal of the display.

Examples

Results in a clear screen after flipping:

win.flip(clearBuffer=True)

The screen is not cleared (so represent the previous screen):

win.flip(clearBuffer=False)
fps()[source]

Report the frames per second since the last call to this function (or since the window was created if this is first call)

property frameBufferSize

Size of the framebuffer in pixels (w, h).

property frontFace

Face winding order to define front, either ccw or cw.

property fullscr

Return whether the window is in fullscreen mode.

gamma

Set the monitor gamma for linearization.

Warning

Don’t use this if using a Bits++ or Bits#, as it overrides monitor settings.

gammaRamp

Sets the hardware CLUT using a specified 3xN array of floats ranging between 0.0 and 1.0.

Array must have a number of rows equal to 2 ^ max(bpc).

getActualFrameRate(nIdentical=10, nMaxFrames=100, nWarmUpFrames=10, threshold=1, infoMsg=None)[source]

Measures the actual frames-per-second (FPS) for the screen.

This is done by waiting (for a max of nMaxFrames) until nIdentical frames in a row have identical frame times (std dev below threshold ms).

Parameters:
  • nIdentical (int, optional) – The number of consecutive frames that will be evaluated. Higher –> greater precision. Lower –> faster.

  • nMaxFrames (int, optional) – The maximum number of frames to wait for a matching set of nIdentical.

  • nWarmUpFrames (int, optional) – The number of frames to display before starting the test (this is in place to allow the system to settle after opening the Window for the first time.

  • threshold (int or float, optional) – The threshold for the std deviation (in ms) before the set are considered a match.

Returns:

Frame rate (FPS) in seconds. If there is no such sequence of identical frames a warning is logged and None will be returned.

Return type:

float or None

getContentScaleFactor()[source]

Get the scaling factor required for scaling correctly on high-DPI displays.

If the returned value is 1.0, no scaling needs to be applied to objects drawn on the backbuffer. A value >1.0 indicates that the backbuffer is larger than the reported client area, requiring points to be scaled to maintain constant size across similarly sized displays. In other words, the scaling required to convert framebuffer to client coordinates.

Returns:

Scaling factor to be applied along both horizontal and vertical dimensions.

Return type:

float

Examples

Get the size of the client area:

clientSize = win.frameBufferSize / win.getContentScaleFactor()

Get the framebuffer size from the client size:

frameBufferSize = win.clientSize * win.getContentScaleFactor()

Convert client (window) to framebuffer pixel coordinates (eg., a mouse coordinate, vertices, etc.):

# `mousePosXY` is an array ...
frameBufferXY = mousePosXY * win.getContentScaleFactor()
# you can also use the attribute ...
frameBufferXY = mousePosXY * win.contentScaleFactor

Notes

  • This value is only valid after the window has been fully realized.

getFutureFlipTime(targetTime=0, clock=None)[source]

The expected time of the next screen refresh. This is currently calculated as win._lastFrameTime + refreshInterval

Parameters:
  • targetTime (float) – The delay from now for which you want the flip time. 0 will give the because that the earliest we can achieve. 0.15 will give the schedule flip time that gets as close to 150 ms as possible

  • clock (None, 'ptb', 'now' or any Clock object) – If True then the time returned is compatible with ptb.GetSecs()

  • verbose (bool) – Set to True to view the calculations along the way

getMovieFrame(buffer='front')[source]

Capture the current Window as an image.

Saves to stack for saveMovieFrames(). As of v1.81.00 this also returns the frame as a PIL image

This can be done at any time (usually after a flip() command).

Frames are stored in memory until a saveMovieFrames() command is issued. You can issue getMovieFrame() as often as you like and then save them all in one go when finished.

The back buffer will return the frame that hasn’t yet been ‘flipped’ to be visible on screen but has the advantage that the mouse and any other overlapping windows won’t get in the way.

The default front buffer is to be called immediately after a flip() and gives a complete copy of the screen at the window’s coordinates.

Parameters:

buffer (str, optional) – Buffer to capture.

Returns:

Buffer pixel contents as a PIL/Pillow image object.

Return type:

Image

getMsPerFrame(nFrames=60, showVisual=False, msg='', msDelay=0.0)[source]

Assesses the monitor refresh rate (average, median, SD) under current conditions, over at least 60 frames.

Records time for each refresh (frame) for n frames (at least 60), while displaying an optional visual. The visual is just eye-candy to show that something is happening when assessing many frames. You can also give it text to display instead of a visual, e.g., msg='(testing refresh rate...)'; setting msg implies showVisual == False.

To simulate refresh rate under cpu load, you can specify a time to wait within the loop prior to doing the flip(). If 0 < msDelay < 100, wait for that long in ms.

Returns timing stats (in ms) of:

  • average time per frame, for all frames

  • standard deviation of all frames

  • median, as the average of 12 frame times around the median (~monitor refresh rate)

Author:
  • 2010 written by Jeremy Gray

hideMessage()[source]

Remove any message that is currently being displayed.

hidePilotingIndicator()[source]

Hide the visual indicator which shows we are in piloting mode.

property lights

Scene lights.

This is specified as an array of ~psychopy.visual.LightSource objects. If a single value is given, it will be converted to a list before setting. Set useLights to True before rendering to enable lighting/shading on subsequent objects. If lights is None or an empty list, no lights will be enabled if useLights=True, however, the scene ambient light set with ambientLight will be still be used.

Examples

Create a directional light source and add it to scene lights:

dirLight = gltools.LightSource((0., 1., 0.), lightType='directional')
win.lights = dirLight  # `win.lights` will be a list when accessed!

Multiple lights can be specified by passing values as a list:

myLights = [gltools.LightSource((0., 5., 0.)),
            gltools.LightSource((-2., -2., 0.))
win.lights = myLights
logOnFlip(msg, level, obj=None)[source]

Send a log message that should be time-stamped at the next flip() command.

Parameters:
  • msg (str) – The message to be logged.

  • level (int) – The level of importance for the message.

  • obj (object, optional) – The python object that might be associated with this message if desired.

property mouseVisible

Returns the visibility of the mouse cursor.

property nearClip

Distance to the near clipping plane in meters.

nextEditable()[source]

Moves focus of the cursor to the next editable window

property projectionMatrix

Projection matrix defined as a 4x4 numpy array.

recordFrameIntervals

Record time elapsed per frame.

Provides accurate measures of frame intervals to determine whether frames are being dropped. The intervals are the times between calls to flip(). Set to True only during the time-critical parts of the script. Set this to False while the screen is not being updated, i.e., during any slow, non-frame-time-critical sections of your code, including inter-trial-intervals, event.waitkeys(), core.wait(), or image.setImage().

Examples

Enable frame interval recording, successive frame intervals will be stored:

win.recordFrameIntervals = True

Frame intervals can be saved by calling the saveFrameIntervals method:

win.saveFrameIntervals()
removeEditable(editable)[source]
resetEyeTransform(clearDepth=True)[source]

Restore the default projection and view settings to PsychoPy defaults. Call this prior to drawing 2D stimuli objects (i.e. GratingStim, ImageStim, Rect, etc.) if any eye transformations were applied for the stimuli to be drawn correctly.

Parameters:

clearDepth (bool) – Clear the depth buffer upon reset. This ensures successive draw commands are not affected by previous data written to the depth buffer. Default is True.

Notes

  • Calling flip() automatically resets the view and projection to defaults. So you don’t need to call this unless you are mixing 3D and 2D stimuli.

Examples

Going between 3D and 2D stimuli:

# 2D stimuli can be drawn before setting a perspective projection
win.setPerspectiveView()
# draw 3D stimuli here ...
win.resetEyeTransform()
# 2D stimuli can be drawn here again ...
win.flip()
resetViewport()[source]

Reset the viewport to cover the whole framebuffer.

Set the viewport to match the dimensions of the back buffer or framebuffer (if useFBO=True). The scissor rectangle is also set to match the dimensions of the viewport.

retrieveAutoDraw()[source]

Add all stimuli which are on ‘hold’ back into the autoDraw list, and clear the hold list.

property rgb
saveFrameIntervals(fileName=None, clear=True)[source]

Save recorded screen frame intervals to disk, as comma-separated values.

Parameters:
  • fileName (None or str) – None or the filename (including path if necessary) in which to store the data. If None then ‘lastFrameIntervals.log’ will be used.

  • clear (bool) – Clear buffer frames intervals were stored after saving. Default is True.

saveMovieFrames(fileName, codec='libx264', fps=30, clearFrames=True)[source]

Writes any captured frames to disk.

Will write any format that is understood by PIL (tif, jpg, png, …)

Parameters:
  • filename (str) – Name of file, including path. The extension at the end of the file determines the type of file(s) created. If an image type (e.g. .png) is given, then multiple static frames are created. If it is .gif then an animated GIF image is created (although you will get higher quality GIF by saving PNG files and then combining them in dedicated image manipulation software, such as GIMP). On Windows and Linux .mpeg files can be created if pymedia is installed. On macOS .mov files can be created if the pyobjc-frameworks-QTKit is installed. Unfortunately the libs used for movie generation can be flaky and poor quality. As for animated GIFs, better results can be achieved by saving as individual .png frames and then combining them into a movie using software like ffmpeg.

  • codec (str, optional) – The codec to be used by moviepy for mp4/mpg/mov files. If None then the default will depend on file extension. Can be one of libx264, mpeg4 for mp4/mov files. Can be rawvideo, png for avi files (not recommended). Can be libvorbis for ogv files. Default is libx264.

  • fps (int, optional) – The frame rate to be used throughout the movie. Only for quicktime (.mov) movies.. Default is 30.

  • clearFrames (bool, optional) – Set this to False if you want the frames to be kept for additional calls to saveMovieFrames. Default is True.

Examples

Writes a series of static frames as frame001.tif, frame002.tif etc.:

myWin.saveMovieFrames('frame.tif')

As of PsychoPy 1.84.1 the following are written with moviepy:

myWin.saveMovieFrames('stimuli.mp4') # codec = 'libx264' or 'mpeg4'
myWin.saveMovieFrames('stimuli.mov')
myWin.saveMovieFrames('stimuli.gif')
property scissor

Scissor rectangle (x, y, w, h) for the current draw buffer.

Values x and y define the origin, and w and h the size of the rectangle in pixels. The scissor operation is only active if scissorTest=True.

Usually, the scissor and viewport are set to the same rectangle to prevent drawing operations from spilling into other regions of the screen. For instance, calling clearBuffer will only clear within the scissor rectangle.

Setting the scissor rectangle but not the viewport will restrict drawing within the defined region (like a rectangular aperture), not changing the positions of stimuli.

property scissorTest

True if scissor testing is enabled.

property screenshot
setBuffer(buffer, clear=True)[source]

Choose which buffer to draw to (‘left’ or ‘right’).

Requires the Window to be initialised with stereo=True and requires a graphics card that supports quad buffering (e,g nVidia Quadro series)

PsychoPy always draws to the back buffers, so ‘left’ will use GL_BACK_LEFT This then needs to be flipped once both eye’s buffers have been rendered.

Parameters:
  • buffer (str) – Buffer to draw to. Can either be ‘left’ or ‘right’.

  • clear (bool, optional) – Clear the buffer before drawing. Default is True.

Examples

Stereoscopic rendering example using quad-buffers:

win = visual.Window(...., stereo=True)
while True:
    # clear may not actually be needed
    win.setBuffer('left', clear=True)
    # do drawing for left eye
    win.setBuffer('right', clear=True)
    # do drawing for right eye
    win.flip()
setMouseType(name='arrow')[source]

Change the appearance of the cursor for this window. Cursor types provide contextual hints about how to interact with on-screen objects.

The graphics used ‘standard cursors’ provided by the operating system. They may vary in appearance and hot spot location across platforms. The following names are valid on most platforms:

  • arrow : Default pointer.

  • ibeam : Indicates text can be edited.

  • crosshair : Crosshair with hot-spot at center.

  • hand : A pointing hand.

  • hresize : Double arrows pointing horizontally.

  • vresize : Double arrows pointing vertically.

Parameters:

name (str) – Type of standard cursor to use (see above). Default is arrow.

Notes

  • On Windows the crosshair option is negated with the background color. It will not be visible when placed over 50% grey fields.

setOffAxisView(applyTransform=True, clearDepth=True)[source]

Set an off-axis projection.

Create an off-axis projection for subsequent rendering calls. Sets the viewMatrix and projectionMatrix accordingly so the scene origin is on the screen plane. If eyeOffset is correct and the view distance and screen size is defined in the monitor configuration, the resulting view will approximate ortho-stereo viewing.

The convergence plane can be adjusted by setting convergeOffset. By default, the convergence plane is set to the screen plane. Any points on the screen plane will have zero disparity.

Parameters:
  • applyTransform (bool) – Apply transformations after computing them in immediate mode. Same as calling applyEyeTransform() afterwards.

  • clearDepth (bool, optional) – Clear the depth buffer.

setPerspectiveView(applyTransform=True, clearDepth=True)[source]

Set the projection and view matrix to render with perspective.

Matrices are computed using values specified in the monitor configuration with the scene origin on the screen plane. Calculations assume units are in meters. If eyeOffset != 0, the view will be transformed laterally, however the frustum shape will remain the same.

Note that the values of projectionMatrix and viewMatrix will be replaced when calling this function.

Parameters:
  • applyTransform (bool) – Apply transformations after computing them in immediate mode. Same as calling applyEyeTransform() afterwards if False.

  • clearDepth (bool, optional) – Clear the depth buffer.

setToeInView(applyTransform=True, clearDepth=True)[source]

Set toe-in projection.

Create a toe-in projection for subsequent rendering calls. Sets the viewMatrix and projectionMatrix accordingly so the scene origin is on the screen plane. The value of convergeOffset will define the convergence point of the view, which is offset perpendicular to the center of the screen plane. Points falling on a vertical line at the convergence point will have zero disparity.

Parameters:
  • applyTransform (bool) – Apply transformations after computing them in immediate mode. Same as calling applyEyeTransform() afterwards.

  • clearDepth (bool, optional) – Clear the depth buffer.

Notes

  • This projection mode is only ‘correct’ if the viewer’s eyes are converged at the convergence point. Due to perspective, this projection introduces vertical disparities which increase in magnitude with eccentricity. Use setOffAxisView if you want to display something the viewer can look around the screen comfortably.

showMessage(msg)[source]

Show a message in the window. This can be used to show information to the participant.

This creates a TextBox2 object that is displayed in the window. The text can be updated by calling this method again with a new message. The updated text will appear the next time draw() is called.

Parameters:

msg (str or None) – Message text to display. If None, then any existing message is removed.

showPilotingIndicator()[source]

Show the visual indicator which shows we are in piloting mode.

property size

Size of the drawable area in pixels (w, h).

stashAutoDraw()[source]

Put autoDraw components on ‘hold’, meaning they get autoDraw set to False but are added to an internal list to be ‘released’ when .releaseAutoDraw is called.

property stencilTest

True if stencil testing is enabled.

timeOnFlip(obj, attrib, format=<class 'float'>)[source]

Retrieves the time on the next flip and assigns it to the attrib for this obj.

Parameters:
  • obj (dict or object) – A mutable object (usually a dict of class instance).

  • attrib (str) – Key or attribute of obj to assign the flip time to.

  • format (str, class or None) – Format in which to return time, see clock.Timestamp.resolve() for more info. Defaults to float.

Examples

Assign time on flip to the tStartRefresh key of myTimingDict:

win.getTimeOnFlip(myTimingDict, 'tStartRefresh')
title
units

None, ‘height’ (of the window), ‘norm’, ‘deg’, ‘cm’, ‘pix’ Defines the default units of stimuli initialized in the window. I.e. if you change units, already initialized stimuli won’t change their units.

Can be overridden by each stimulus, if units is specified on initialization.

See Units for the window and stimuli for explanation of options.

updateLights(index=None)[source]

Explicitly update scene lights if they were modified.

This is required if modifications to objects referenced in lights have been changed since assignment. If you removed or added items of lights you must refresh all of them.

Parameters:

index (int, optional) – Index of light source in lights to update. If None, all lights will be refreshed.

Examples

Call updateLights if you modified lights directly like this:

win.lights[1].diffuseColor = [1., 0., 0.]
win.updateLights(1)
property useLights

Enable scene lighting.

Lights will be enabled if using legacy OpenGL lighting. Stimuli using shaders for lighting should check if useLights is True since this will have no effect on them, and disable or use a no lighting shader instead. Lights will be transformed to the current view matrix upon setting to True.

Lights are transformed by the present GL_MODELVIEW matrix. Setting useLights will result in their positions being transformed by it. If you want lights to appear at the specified positions in world space, make sure the current matrix defines the view/eye transformation when setting useLights=True.

This flag is reset to False at the beginning of each frame. Should be False if rendering 2D stimuli or else the colors will be incorrect.

property viewMatrix

View matrix defined as a 4x4 numpy array.

viewPos

The origin of the window onto which stimulus-objects are drawn.

The value should be given in the units defined for the window. NB: Never change a single component (x or y) of the origin, instead replace the viewPos-attribute in one shot, e.g.:

win.viewPos = [new_xval, new_yval]  # This is the way to do it
win.viewPos[0] = new_xval  # DO NOT DO THIS! Errors will result.
property viewport

Viewport rectangle (x, y, w, h) for the current draw buffer.

Values x and y define the origin, and w and h the size of the rectangle in pixels.

This is typically set to cover the whole buffer, however it can be changed for applications like multi-view rendering. Stimuli will draw according to the new shape of the viewport, for instance and stimulus with position (0, 0) will be drawn at the center of the viewport, not the window.

Examples

Constrain drawing to the left and right halves of the screen, where stimuli will be drawn centered on the new rectangle. Note that you need to set both the viewport and the scissor rectangle:

x, y, w, h = win.frameBufferSize  # size of the framebuffer
win.viewport = win.scissor = [x, y, w / 2.0, h]
# draw left stimuli ...

win.viewport = win.scissor = [x + (w / 2.0), y, w / 2.0, h]
# draw right stimuli ...

# restore drawing to the whole screen
win.viewport = win.scissor = [x, y, w, h]
waitBlanking

After a call to flip() should we wait for the blank before the script continues.

property windowedSize

Size of the window to use when not fullscreen (w, h).


Back to top