setOpacity for visual.Polygon seems not work properly

URL of experiment:

Description of the problem:

Hi, everyone. I am looking for your wisdom to solve my problem that lasted several days… I am naive to python and psychJS languages so I cannot find where the error is.

This is my experimental design: Search display consists of 11 homogenous shape items (hexagon or octagon) and 1 singleton shape item (target; octagon or hexagon, opposite shape to homogenous shape). Sometimes, one homogenous shape item can appear with a different color or not (distractor). So, there should be only one singleton shape item regardless of the presence of singleton color item (because the shape of singleton color item is always the same to 11 homogenous shape items).

I defined the shape, color, location, … relevant variables and checked in the java console that the variables are well-defined.

This is the problem: Sometimes, the search display does not match with the defined variables. 1) While the homogenous shape is hexagon and the singleton shape is octagon, the display shows the opposite. 2) Sometimes, the target and the distractor are in the same shape, which should not.

I create the hexagon or octagon polygons by manipulating the opacity according to the pre-defined variables. The opacity parameter seems well-defined but it seems not well adjusted when creating visual stimulus.

The structure of my code: I first of all draw 12 hexagons and 12 octagons, and changed the opacity to show either hexagon set or octagon set every trial. To draw the target, I overlaied a buffer square on one possible item location, then draw either the octagon or hexagon target on it. To draw the distractor, I also overlayed a buffer square on one possible item location, then draw either the hexagon or octagon distractor with different color on the buffer. All the opacities are controlled via the code below.

if shapeT == 6: #shapeT = homogenous shape
    hexagon = 1
    octagon = 0
elif shapeT == 8:
    hexagon = 0
    octagon = 1

I attached the gitlab and experiment urls above. I also attached py file for those who are familiar with python lines (the python code was created by psychopy program. The experiment was created with builder but most of the parameters are defined with code components). For your easy referencing, polygon drawing begins from line 226 to line 769, and variable settings begin from line 1213 to 1435.

Please share your wisdom for me…!!! (150.3 KB)
distSL_py.psyexp (143.5 KB)

Where is this code in relation to your polygon components?

The opacities are defined from line 1310 to line 1315 with the variable names “hexagon” and “octagon”.
The variables are applied to polygon components from line 1350 to 1435.


(P6 means hexagon item and P8 means octagon shapes. Because target’s shape is always opposite to non-singleton shape items, P8_Target should have ‘hexagon’ as its opacity and P6_Target ‘ocatgon’)

Does it work offline? Are you using Auto code components?

I couldn’t test it offline because it contains several javascript code lines in it.
I put python codes manually in code component of the builder, then auto-create py code.

I’m afraid I’m none the wiser.

Please could you show screenshots of your relevant code components and one of the polygons?

These are the correct display that showed up as I intended.
Different-color item sometimes can be present or absent, and among 12 items, only one item should have different shape (different-color item cannot have different-shape)


These are the wrong displays. I wrote short descriptions on what’s wrong with them.

The item shape can be either “hexagon among octagons” or “octagon among hexagons”, but the examples above are all ‘octagon among hexagons’, by chance. I found that the problem I mentioned shows up irrespective of item sets.

Personally I would probably control the shapes using setAutoDraw but having shown the issue, please could you show the code that’s causing it and a sample component?

I was having a similar issue for ImageStim elements, but it is no longer occurring on PsychoPy version 2021.1.3, so try updating. Rather than setOpacity I was that setting the attribute .opacity. I’m not sure if this makes a difference.

I also found that the code component setting the opacity needed to be placed below the stimulus, if the stimulus is being created by a builder component.

Hi @hij113, thanks for flagging, it looks like you have hit the opacity not automatically updating bug addressed in the latest PsychoPy, please could you try upgrading to version 2021.1.4 and post back if that fails to solve the issue? Here to help, x

Hi @sotiri, thanks for your comment. I upgraded PsychoPy to version 2021.1.4 and now the experiment works perfectly! Thank you for your help :slight_smile: