| Reference | Downloads | Github

Changing the depth of image stimuli (with code)

PsychoPy version: 2020.1.3
Standard Standalone? (yes)
What are you trying to achieve?:

  • I have a drag and drop experiment (code partly from Drag and Drop Builder Demo), in which two differently colored dots (image stimuli) can be picked and dragged multiple times over another (bigger) image stimuli

The drawing order of the newly created pieces is wrong. The new ones always stay behind the bigger picture. But I want them to be on top of the bigger picture.
The depth of the new pieces always stays depth = (0.0); I would want them to be (-48.0) instead, but it won`t let me change that.

What did you try to make it work?:

  • without the creating new pieces part, the issue is not occuring
  • the depth of the newly created pieces can’t be manipulated with .setDepth
  • tried to put the code for creating new pieces on the bottom of all the other image components, but that didn’t change anything

What I need help with:
effectively changing the drawing order for the via code generated newPieces or a code snippet that tells the new pieces to always stay in the foreground of the bigger picture

I’m adding my code and a picture to visualize what I try to do…

#Beginn Experiment:
#Drag and Drop Code from: [DragAndDrop PsychoPy Demo]
# creating pieces that can be dragged and dropped later on
def createPiece(piece, pos, name):
    return visual.ImageStim(win, image=piece.image, name=name, size=piece.size, pos=pos, opacity=0.8)

def drawPicked(picked):
    for each in picked:

def movePicked(picked, mouseMulti, grabbed):
    #move piece if it is already being moved 
    if grabbed is not None and mouseMulti.isPressedIn(grabbed):
        grabbed.pos = mouseMulti.getPos()
        return grabbed
    else: #move newly clicked piece
        for piece in picked:
            if mouseMulti.isPressedIn(piece) and grabbed is None:
                return piece
#Beginn Routine:
polygons = [polygon1, polygon2, polygon3, polygon4, polygon5, polygon6, polygon7, polygon8, polygon9, polygon10, polygon11, polygon12, polygon13, polygon14, polygon15, polygon16, polygon17, polygon18, polygon19, polygon20, polygon21, polygon22, polygon23, polygon24, polygon25, polygon26, polygon27, polygon28, polygon29, polygon30, polygon31, polygon32, polygon33, polygon34, polygon35, polygon36, polygon37, polygon38, polygon39, polygon40, polygon41, polygon42]

for polygon in polygons:

pieces = [redPiece, greenPiece]
picked = []
newPiece = None
movingPiece = None

#Each frame:
for piece in pieces:
    if mouseMulti.isPressedIn(piece) and movingPiece is None:
        newPiece = createPiece(piece, mouseMulti.getPos(), piece.image)
        picked.append(newPiece) #to drag multiple newly created pieces (but are hidden behind image/polygon components)
        #picked.append(piece) #to create and drag only one piece (but correct drawing order)
    #reset the newPiece to none, if one was just picked to be able to pick another one again:
    if newPiece is not None and mouseMulti.getPressed()[0] == 0:
        newPiece = None
    movingPiece = movePicked(picked, mouseMulti, movingPiece)

Is there a way for me to get the new dots to show above the blue big picture??

There is, and thankfully it’s quite simple! Objects are rendered in order from top to bottom of the Routines canvas - so the objects at the bottom will be rendered last, therefore will be on top.

Thank you for your answer!
But for whatever reason, this is not happening with the through code generated new pieces. They stay behind all the other components, even if the code is put at the bottom of the Routine canvas…

When you click compile and view it in Coder view, does the code to draw the square appear after the code to draw the dots still? If you send the .psyexp file for it I’ll have a look

Ehm no, the code to draw the big picture is before the code to draw the dots. I’m sending the psyexp Data file of the shortend version of my experiment (it is quite complex), maybe I am misunderstanding something here
PsychPy_BeVi_Experiment_MouseDragAndDrop - Kopie.psyexp (169.7 KB)

And just a head up, I know the last part of the each frame code is not
making sense right now. I still need to adjust a lot with it… but that is something that I also need to fix with the experiment sometime later :slight_smile: