Hi folks,
I'm running into an isse trying to build a python with an Extension Class, to be used to do some logic when a layer is added or removed from the mxd (itemAdded(), itemDeleted() built in functions)
So, I had orginally built a toolbar which included 2 comboboxes and a button and then the Extension Class. I wanted to test how the functions worked, so I left the Extension Class functions as the default coding and just threw in a couple print lines to see if they printed out the message in the python console in Arcmap when I added or removed a layer. The problem I am having is, it keeps throwing an error:
TypeError: unbound method itemAdded() must be called with ExtensionClass instance as first argument (got Layer instance instead)
I've been pouring through the net, trying to understand what an unbound method is, but I'm still not at a point where I understand how to resolve this error.
I tested again but simply creating a new addin project with one Extension Class and then the 2 fucntions above (itemAdded(), itemDeleted()). I threw a couple print lines in the functions to see if they fired off properly and success.
Further to that, I commented out all of my code in my original script, except the Extension Class (so that it has the exact same code as my test addin), fired up Arcmap and added a layer and it still throws the error.
Can anyone suggest what I am missing?
Test Addin Code (that works)
import arcpy import pythonaddins class ExtensionClass(object): """Implementation for Addins_addin.ExtensionClass (Extension)""" def __init__(self): # For performance considerations, please remove all unused methods in this class. self.enabled = True print self def itemAdded(self, new_item): print "Added " + new_item.name pass def itemDeleted(self, deleted_item): print "Deleted " + deleted_item.name pass
Original Code here:
import arcpy import pythonaddins cboSetup1 = None cboSetup2 = None lyr = None class ExtensionClass(object): """Implementation for Build_Definition_Query_from_Selected_Features_addin.ExtensionClass (Extension)""" def __init__(self): # For performance considerations, please remove all unused methods in this class. self.enabled = True print self def itemAdded(self, new_item): print "Added" pass def itemDeleted(self, deleted_item): print "Deleted" pass class btn1(object): """Implementation for Build_Definition_Query_from_Selected_Features_addin.btn1 (Button)""" def __init__(self): mxd = arcpy.mapping.MapDocument("CURRENT") self.items = [] global lyrDefQueryDict lyrDefQueryDict = {} for layer in arcpy.mapping.ListLayers(mxd): if not layer.isGroupLayer: self.items.append(layer.name) lyrDefQueryDict[layer.name] = layer.definitionQuery print lyrDefQueryDict self.enabled = True self.checked = False def onClick(self): combo1 = cbobox1 combo1.btnPopulate() combo1.refresh() combo2 = cbobox2 combo2.value = '' combo2.refresh() if lyr: for lyrName,lyrDefQuery in lyrDefQueryDict.iteritems(): if lyr.name == lyrName: print "lyr.name = " + lyr.name print "lyrName = " + lyrName print "lyrDef = " + lyrDefQuery lyr.definitionQuery = lyrDefQuery arcpy.RefreshActiveView() class cbobox1(object): """Implementation for Build_Definition_Query_from_Selected_Features_addin.cbobox1 (ComboBox)""" def __init__(self): mxd = arcpy.mapping.MapDocument("CURRENT") self.items = [] for layer in arcpy.mapping.ListLayers(mxd): if not layer.isGroupLayer: self.items.append(layer.name) global cboSetup1 cboSetup1 = self.items self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWWWWWWWWWW' self.width = 'WWWWWWWWWWWWWWWWW' def onSelChange(self, selection): mxd = arcpy.mapping.MapDocument("CURRENT") for layer in arcpy.mapping.ListLayers(mxd): if layer.name == selection: global lyr lyr = layer print layer.name fieldList = [field.name for field in arcpy.ListFields(layer.name)] global cboSetup2 cboSetup2 = fieldList cbobox2.updateFields() def onFocus(self, focused): self.items = cboSetup1 def btnPopulate(self): self.value = '' mxd = arcpy.mapping.MapDocument("CURRENT") self.items = [] for layer in arcpy.mapping.ListLayers(mxd): if not layer.isGroupLayer: self.items.append(layer.name) global cboSetup1 cboSetup1 = self.items class cbobox2(object): """Implementation for Build_Definition_Query_from_Selected_Features_addin.cbobox2 (ComboBox)""" def __init__(self): self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWWWWWWWWW' self.width = 'WWWWWWWWWWWWWWWWW' def onFocus(self, focused): self.items = cboSetup2 def onSelChange(self, selection): selectionList = [] for row in arcpy.SearchCursor(lyr): selectionList.append(row.getValue(selection)) if len(selectionList) == 1: lyr.definitionQuery = "{0} = {1}".format(selection,selectionList[0]) else: lyr.definitionQuery = "{0} in {1}".format(selection,tuple(selectionList)) arcpy.RefreshActiveView() def updateFields(self): self.items = cboSetup2
Just to follow up on this topic. I think the error was caused by having too many test addins created in Arcmap. I had a few of them and I went and cleaned out all the addins and then re-initialized my final one. This cleaned up the error message.