I'm creating a Python Toolbox. One input is a workspace and the second is a list of feature classes and tables. The list of feature classes and tables is generated once the workspace is input by the user, using a series of ListFeatureClasses
and ListTables
. Annoyingly, once the workspace is input the tool will re-run this code when the user updates ANY of the parameters. This can freeze up ArcGIS for minutes if the workspace contains lots of feature classes/tables and is located on a network. Is there a way to only run code when a specifc parameter is altered? You'd think parameter.altered == True
would do the trick but this seems no different than if parameter.valueAsText
returns True
in a logic test (has a value).
Making the feature class/tables parameter dependent of the workspace doesn't work because feature classes in datasets are ignored.
Code:
class Tool(object):
def __init__(self):
self.label = "Copy Feature Classes And Relationship Classes"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
inGdb = arcpy.Parameter(
displayName = "Input Geodatabase",
name = "ingdb",
datatype = "Workspace",
parameterType = "Required",
direction = "Input")
outGdb = arcpy.Parameter(
displayName = "Output Geodatabase",
name = "outgdb",
datatype = "Workspace",
parameterType = "Required",
direction = "Input")
inFcs = arcpy.Parameter (
displayName = "Feature Classes And Tables",
name = "infcs",
datatype = "GPString",
parameterType = "Required",
direction = "Input",
multiValue = True)
return [inGdb, outGdb, inFcs]
def updateParameters(self, parameters):
if not parameters [0].valueAsText:
parameters [2].enabled = False
###below code runs whenever any new input is input!!!
if parameters [0].valueAsText and parameters [0].altered: ##altered doesn't work! :(
try: wst = arcpy.Describe (parameters [0].valueAsText).workspaceType
except:
parameters [2].filter.list = []
parameters [2].value = None
return
if wst == "LocalDatabase":
parameters [2].enabled = True
gdb = parameters [0].valueAsText
arcpy.env.workspace = gdb
fcs = []
for typ in ["Point", "Polygon", "Polyline", "Multipoint"]:
fcs += ["{} (Feature Class)".format (fc) for fc in arcpy.ListFeatureClasses (feature_type = typ)]
fcs += ["{} (Table)".format (tab) for tab in arcpy.ListTables ()]
datasets = arcpy.ListDatasets ()
for dataset in datasets:
for typ in ["Point", "Polygon", "Polyline", "Multipoint"]:
dsFcs = arcpy.ListFeatureClasses (None, typ, dataset)
for dsFc in dsFcs:
fc = os.path.join (dataset, dsFc)
fcs += [fc]
parameters [2].filter.list = fcs
else:
parameters [2].filter.list = []
parameters [2].value = None