This is my first topic at this forum, so please be merciful. I wrote a plugin using Jython and it happily worked on old ImageJ versions, but started to crash with newer versions. In the plugin, I select points in an image and save them as ROIs in a zip file. This utilizes a
MouseListener class, so I use global variables
xlist to keep the track of multiple ROIs. The plugin behaves normally for 4-7 clicks, and then crashes with a message
global name 'updateROIs' is not defined.
This happens with some variability in the number of clicks. It seems that the system destroys my function
updateROIs() while the program is running. Sometimes it crashes with error messages about global variables as well, like
Does anyone know what’s happening behind the curtains here? Or should I just re-write my code in Java and suffer a post-traumatic syndrome from Jython?
My full code is below, sorry for being long.
from ij import IJ from ij.plugin.frame import RoiManager from java.awt.event import MouseAdapter, KeyEvent, KeyAdapter from ij.gui import GenericDialog, WaitForUserDialog, GenericDialog, Roi, OvalRoi, Toolbar, Overlay from ij.io import SaveDialog # create variables iROI = 0 xlist = ylist = zlist =  def reset(): global iROI, xlist, ylist, zlist xlist =  ylist =  zlist =  manager.runCommand('Reset') manager.runCommand('Show All') iROI = 0 class ML(MouseAdapter): def mousePressed(self, keyEvent): updateROIs() def updateROIs(): global iROI, xlist, ylist, zlist iROI += 1 canv = imp.getCanvas() p = canv.getCursorLoc() z = imp.getSlice() roi = OvalRoi(p.x - 5, p.y - 5, 10, 10) roi.setName('z' + str(z) + 'cell' + str(iROI)) roi.setPosition(z) xlist.append(p.x) ylist.append(p.y) zlist.append(z) imp.setRoi(roi) manager.addRoi(roi) manager.runCommand('Draw') # MAIN code imp = IJ.getImage() IJ.setTool(Toolbar.RECTANGLE) manager = RoiManager.getInstance() if manager is None: manager = RoiManager() #user defines parameter values: reset() #Listeners: listener = ML() win = imp.getWindow() win.getCanvas().addMouseListener(listener)