AnsweredAssumed Answered

Referencing specific layers in my add-in

Question asked by jayarem on Oct 17, 2013
Latest reply on Oct 24, 2013 by jayarem
Hello, I am writing an add-in to work with many different MXDs that I have to work with, they all use the same naming convention for the layers. Currently my addin is structured like this:

import x, y, z  mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd)[0] layerList = arcpy.mapping.ListLayers(mxd) tableList = arcpy.mapping.ListTableViews(mxd) for layer in layerList:     if "Footprints_Edit" in        footprints_fc = layer     if "Parcels_Edit" in        parcels_fc = layer     etc..     etc..  class Button(object)     ... class Button2(object)     ...

This works fine, except, I'd like to be able to pass the add-in around to other people. Now, because the assigning-variables logic happens at the top of the add-in, if the add-in is present in the toolbar before the .mxd is loaded, it won't find the expected layers and using the tools will throw an error: the only way to get it to work is to remove the add-in, close ArcMap, re-open, and load the .mxd. This is a pain. Now, I tried putting the assign-variables logic as the first step in each button's onClick method, but that causes all sorts of slowness for what should be quick operations.

Is there some better way to look for and assign these layers that I am missing? I tried throwing everything in a function and then adding this to onClick:

if not footprints_fc:     assignLayers()

but that doesn't seem to work. Any thoughts? Thanks in advance.