Howdy, Stranger!

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

Supported by

draw with and track mouse at the same time

Hi everyone,
sorry, another question by me. This time I would like to draw something with the mouse and of course this has to be tracked for the later analysis.
In the forum I found a script which does the drawing well.
Here it is:
from openexp.canvas import canvas from openexp.mouse import mouse my_mouse = mouse(exp, buttonlist = ([1]), visible = True) my_canvas = canvas(exp) dot = exp.pool[u'dot3.png'] my_canvas.show() start = clock.time() my_mouse.set_pos(pos=(0, 0)) while clock.time() - start <= 3000: button, position, timestamp = my_mouse.get_click(timeout=10) x,y = position if button is not None else (None,None) if button is not None: (x0, y0) = (None,None) while my_mouse.get_pressed()==(1,0,0): (x, y), timestamp = my_mouse.get_pos() if (x0,y0) != (None, None): my_canvas.image(dot, x=x0, y=y0) my_canvas.line(x0,y0,x,y, penwidth=8) my_canvas.image(dot, x=x, y=y) else: my_canvas.image(dot, x=x, y=y) (x0,y0) = (x,y) my_canvas.show()
But this script does not log the position of the mouse as far as I can see. (Or am I wrong?)
Therefore I included after this script a mousetrap-response item. This does the logging, but it only starts after the drawing is done. But I want to log what is drawn.
It doesn't help if I reverse the order (first the mousetrp-response item and then the script for the drawing) because then it just logs the mouse-movements before the drawing which is neither what I want.
I want the movements of the drawing (the drawing itself) be logged.
(At the end I have a logger-item of course.)

Any ideas how I can achieve this?

Thanks a lot!
Pia

Comments

  • Hi Pia,

    You could append the latest coordinates to a list variable each iteration through the loop. So right after the my_mouse.get_pos() command, you place a line with coordinates_list.append((x,y)). Be sure to create the list variable prior to appending stuff to it.

    Alternatively you could just make a print-screen when the drawing is done. There's surely a python command for this.

    Cheers

    Josh

  • Thanks! Hm, unfortunately this gives an error-message, which I unfortunately don't understand:
    File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\inline_script.py", line 102, in run self.experiment.python_workspace._exec(self.crun) File "C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\python_workspace.py", line 161, in _exec exec(bytecode, self._globals) File "<string>", line 23, in <module> TypeError: 'instancemethod' object is not iterable

    Here is what I did:
    from openexp.canvas import canvas from openexp.mouse import mouse my_mouse = mouse(exp, buttonlist = ([1]), visible = True) my_canvas = canvas(exp) coordinates_list = list() dot = exp.pool[u'dot3.png'] my_canvas.show() start = clock.time() my_mouse.set_pos(pos=(0, 0)) while clock.time() - start <= 3000: button, position, timestamp = my_mouse.get_click(timeout=10) x,y = position if button is not None else (None,None) if button is not None: (x0, y0) = (None,None) while my_mouse.get_pressed()==(1,0,0): (x, y), timestamp = my_mouse.get_pos coordinates_list.append((x,y)) if (x0,y0) != (None, None): my_canvas.image(dot, x=x0, y=y0) my_canvas.line(x0,y0,x,y, penwidth=8) my_canvas.image(dot, x=x, y=y) else: my_canvas.image(dot, x=x, y=y) (x0,y0) = (x,y) my_canvas.show()
    So I included coordinates_list = list() in the beginning and placed coordinates_list.append((x,y)) after (x, y), timestamp = my_mouse.get_pos. Did I place it at the right place? What is my mistake?

    Thank you!!

  • Hi Pia,

    One mistake is this line:

    my_mouse.get_pos

    Can you change it to my_mouse.get_pos() and see whether it is enough to make the script work?

    Eduard

    Buy Me A Coffee

  • PiaPia
    edited May 2017

    Hi eduard,
    thanks, the error message disappears, but unfortunately I can't find my variable (coordinates_list) in the logfile. I placed an logger after the inline-script. And because the variable didn't show up automatically (with "log all variables") I added it manually, but it is still not in the logfile?
    So, how can I save the contents of this variable in the logfile?
    Thanks!
    Pia

    edit: I used print(coordinates_list) to print the coordinates_list in the debug window and it works. But how do I get it into the logfile?

  • Hi Pia,
    Only variables are logged that have the prefix var.. So if you changed the variable coordinates_list into var.coordinate_list, it will appear in the logfile.

    Eduard

    Buy Me A Coffee

  • Hi Pia, changing coordinates_list into exp.coordinates_list might work. The logger automatically stores all variables that are made available to the whole experiment (without .exp the variable can only be called within inline_scripts, but not, for instance, by the logger item).

    Your list may turn out huge though. Are you sure that a print-screen will not suffice?

    Cheers

    Josh

  • Greetings Ed

  • Thank you both!

    The experiment runs when I change the variable to var.coordinates_list and also when I change it to exp.coordinates_list but the list doesn't appear in the logfile, somehow. I also tried to add the variable to the logger, but it complains that this is (both) not a valid variable name...

    I have to log the coordinates because they are important for the later analysis. The participants have to draw a circle where a dot appeared before.

    Any idea what I can do to get the coordinates into the logfile?

  • Can you quickly upload your experiment? There must a be a little thing wrong somewhere....

    Buy Me A Coffee

  • Yes, I uploaded it!

  • Okay, so either change the variable exp.coordinate_list to var.coordinate_list, or add the custom variable coordinate_list (without exp or var) to the logger. Both solutions work for me.

    Eduard

    Buy Me A Coffee

  • Thanks a lot! It works!

Sign In or Register to comment.