I created a script that adds definition queries to every layer in an mxd depending on the layer name.
This worked fine as a stand alone script, but when I try to use it as a script tool with arcpy.GetParameterAsText() it fails on the second query.
Here's the snippet:
import arcpy
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(0))
def applyDefQuery(mxd):
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers:
if layer.name == "PROJECT_POLYGON":
layer.definitionQuery = arcpy.GetParameterAsText(1)
else:
layer.definitionQuery = arcpy.GetParameterAsText(2)
mxd.save()
applyDefQuery(mxd)
The idea is to prep an mxd for QA/QC when a subcontractor returns a checkout with a ton of layers. The script should select all of the project area polygons that they edited within and for every other layer select the records that the subcontractor edited.
I've tried troubleshooting this in a variety of ways: adding a second function for the second query, taking it out of the for loop and running it before the conditional statement, searching for null values in the editor fields instead of the ones I'm looking for (I was concerned that the value format - 'COMPANY\editorname' was causing an issue since it contained an escape character). Nothing seems to work except for leaving the third field in the GUI blank and just running the first definition query.
Here are the parameters for the script tool:
and the input I'm passing to it:
and the error:
Executing: QCPrep "C:\Users\bhamson\Documents\Company Check- INs\VisualQC_04142015\VisualQC_04162015_2 (1).mxd" ""CONTRACT'" = 'AAA-CO-11-001_005'" ""EDITOR" LIKE 'COMPANY%'"
Start Time: Tue May 05 18:30:38 2015
Running script QCPrep...
Failed script QCPrep...
Traceback (most recent call last):
File "C:\Users\bhamson\Documents\Company Check-INs\VisualQC_04142015\QCPrep.py", line 17, in <module>
applyDefQuery(mxd)
File "C:\Users\bhamson\Documents\Company Check-INs\VisualQC_04142015\QCPrep.py", line 14, in applyDefQuery
layer.definitionQuery = arcpy.GetParameterAsText(2)
File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\_base.py", line 89, in _set
return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: LayerObject: Set attribute definitionQuery does not exist
Failed to execute (QCPrep). Failed at Tue May 05 18:30:56 2015 (Elapsed Time: 17.58 seconds)
Any idea what the problem could be?