Hi, yes, that suggested code wouldn’t have been compatible with a Builder-generated experiment (I’d assumed you were working with an experiment built from scratch, where you were in control of the drawing loop).
But it should be relatively straightforward to integrate the code you need into your Builder file, rather than editing the .py file. I suggest you do the following:
(1) Remove the Image stimulus components from your Trial_1 and Trial_2 routines. We will replace them with the list of ImageStims you created in code, and draw them in code.
(2) You will still need some sort of dummy stimulus just to control the duration of the trial. e.g. put a small Polygon stimulus in the centre of the screen, set to last for 15 frames. Don’t worry, we’ll draw the image over it, so it won’t be visible.
(3) Insert a Code Component in Trial_1. Put it below the Polygon stimulus, so what it draws will erase the polygon. In its
Begin Experiment tab, insert your caching code:
""" PRELOAD IMAGES"""
imgList = 
path = os.getcwd()
for infile in glob.glob(os.path.join(path, '*.jpg')):
pictures = [visual.ImageStim(win, img, ori=0, pos=[0, 0]) for img in imgList]
(4) Disconnect your inner loops from their .xlsx conditions files. We aren’t getting the stimulus names from there anymore. (But keep them connected if there is some other info in those files that you are using).
(5) Put this in the
Begin routine tab, so that the name of the current image gets saved in the data for the current trial. i.e. we figure out which image to use based on the trial number of this loop (i.e.
Block_loop_1.thisN), and access its
.image property, which gives the filename that was used to create it:
(5) In the
Every frame tab, put this to actually draw your image:
pictures[Block_loop_1.thisN].draw() # gets repeated 15 times
i.e. the first image in your list (i.e.
pictures) will be drawn for 15 frames. Then on the next trial,
pictures will be drawn for 15 frames, and so on. The names of those images will be saved in the data at the start of each trial.
Hopefully you can see how to do the same thing in Trial_2, but in the code component there, don’t repeat the code in the
Begin experiment tab. That only needs to be specified once. [Unless you have a different set of images for that part of the experiment? I wasn’t sure. If so, cache those too, but in a different list (say,