Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

PyGaze with Pyglet

edited December 2014 in PyGaze

Hi all,

I have a stimulus code that uses the Pyglet library to create and animate some OpenGL figures. I would like to use an EyeTracker to synchronize with the stimulus and then get the eye gaze data.

I understand that PyGaze is already prepared for PsychoPy and PyGame. Since I have the stimulus in Pyglet, I believe that I would have to modify the libscreen, libtime and libinput libraries.

Is modifying these files the only solution? Has anybody used Pyglet with PyGaze?

Thank you,
jl

Comments

  • edited 11:53AM

    Hi jl,

    There are two options:

    1) Rewrite Display and Screen (and possibly Keyboard and Mouse). Basically, this means implementing all the existing functionality (provided through PyGame and PsychoPy) by wrapping Pyglet.

    2) Use the EyeTracker class with an active PyGaze Display under the PsychoPy display type, then hijack the Pyglet window. The recipe would be fairly straightforward:

    constants.py

    # set the display size to the same display size as your 
    DISPSIZE = (1920,1080)
    # important: set the display type to 'psychopy'!
    DISPTYPE = 'psychopy'
    # set the tracker type to the type of your choice
    TRACKERTYPE = 'eyetribe'
    
    # you can add more stuff here, to overwrite the defaults
    

    experiment.py

    import pygaze
    from pygaze.display import Display
    from pygaze.eyetracker import EyeTracker
    
    # initialize a Display
    disp = Display()
    # create a new EyeTracker instance
    tracker = EyeTracker(disp)
    
    # get the handle to the pyglet window
    pygletwin = pygaze.expdisplay.winHandle
    
    # next do whatever you want with the Pyglet handle!
    

    Good luck!

  • edited December 2014

    Hi Edwin,

    Thank you very much for your answer.

    I have tried to put the second option in practice, but I have not succeeded. I understand that
    pygletwin = pygaze.expdisplay.winHandle

    will create a pyglet window, but I have not found any documentation on how to use it.

    Following the code in your answer, I have tried to make something simple as to display a square on the screen, but the screen remains empty and there is no error message.

    This is the code I am using:

    import pygaze
    from pygaze.libscreen import Display
    from pygaze.eyetracker import EyeTracker
    from pygaze.libinput import Keyboard
    import pyglet
    from pyglet.gl import *
    from random import random
    
    def main():
        # initialize a Display
        disp = Display()
    
        # create a new EyeTracker instance
        tracker = EyeTracker(disp)
    
        # calibrate eye tracker
        tracker.calibrate()
    
        # get the handle to the pyglet window
        pygletwin = disp.expdisplay.winHandle
    
        kb = Keyboard(keylist=['space'],timeout=None)
        x = pygletwin.width/2
        y = pygletwin.height/2
        w = 3000
    
        color = (1,1,1)
    
        # next do whatever you want with the Pyglet handle!
        while kb.get_key()[0] == None:
    
            pyglet.graphics.draw(4, pyglet.gl.GL_QUAD, 
                        ('v2i', (x-w, y-w, x-w, y+w, x+w, y-w, x+w, y+w)),                                                                                  # position of each vertices
                        ('c3B', (color[0], color[1], color[2], color[0], color[1], color[2], color[0], color[1], color[2], color[0], color[1], color[2]))   # color of each vertex                  
                        )   
            pygletwin.dispatch_events()
            pygletwin.clear()
            pygletwin.flip()
            pass
    
        pygletwin.close()
    
    if __name__=='__main__':
        main()
    
    

    Thanks for your help.

  • edited December 2014

    Could it be because you call pygletwin.clear() before calling pygletwin.flip()?

    You mention you already have the Pyglet code for your stimulus, so I assumed you were familiar with it's Window class. I am not, but I did find some documentation here.

    Good luck!

  • edited 11:53AM

    Hi Edwin,

    Thank you for your answer.

    I made the following mistake (but I have not solved my problem): nothing was being drawn because the pyglet.clear() call was after drawing the square. Now the while loop looks like this:

    def my_function():
        while kb.get_key()[0] == None:
    
            pygletwin.dispatch_events()     # dispatch events of pyglet window
            pygletwin.clear()               # clear pyglet window
    
            pyglet.graphics.draw(4, pyglet.gl.GL_QUADS,             # draw square 
                        ('v2i', (x-w, y-w, x-w, y+w, x+w, y-w, x+w, y+w)),
                        )   
    
            pygletwin.flip()        # flip pyglet window
            pass
    

    When pygletwin is initialzed as a normal pyglet window, i.e. pygletwin = pyglet.window.Window(), the square is drawn. However, when I use pygletwin = disp.expdisplay.winHandle, nothing is drawn.

    I am more or less familiar with the Pyglet Window class, but, sorry, I was not clear that I was refering to the winHandle method, that I believe it is a method of the PsychoPy window, but I have not found any documentation at all.

    Thank you very much for your help, I really appreciate it.

    jl

Sign In or Register to comment.