I have a script that allows me apply a single SQL Definition Query to all feature classes within a particular Group Layer. For ease of input for others in the office, I changed this into a model tool, and so rather than typing these values into the python script itself, a dialog pops up with two text boxes/drop down menus.
One of the input boxes is just a SQL Expression data type, and the other uses the "Group Layer" data type, so it populates a drop down list of all group layers within the map. Using this method, the drop down does populate with all the map's group layers and I am able to select a group layer. However, the script doesn't work (though it doesn't throw any errors). If I instead type the Group Layer name directly into the script with quotes around it, it works properly. What am I doing wrong? I'm sure it's something simple, but I'm new to scripting. Script code as follows:
groupLayer = arcpy.GetParameterAsText(0)
sql = arcpy.GetParameterAsText(1)
mxd = arcpy.mapping.MapDocument ("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if not lyr.isGroupLayer:
continue
if not lyr.name == groupLayer:
continue
for subLyr in lyr:
subLyr.definitionQuery = sql
1 Answer 1
Since it appears to be a subgroup layer problem, if not lyr.name == groupLayer:
should be changed to if not lyr.longName == groupLayer:
.
groupLayer = arcpy.GetParameterAsText(0)
sql = arcpy.GetParameterAsText(1)
mxd = arcpy.mapping.MapDocument ("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if not lyr.isGroupLayer:
continue
if not lyr.longName == groupLayer:
continue
for subLyr in lyr:
subLyr.definitionQuery = sql
-
It works perfectly now. Thank you very very much!Tim Miller– Tim Miller2015年11月02日 19:16:07 +00:00Commented Nov 2, 2015 at 19:16
arcpy.AddMessage (groupLayer)
before the start of thefor
loop. In ArcMap this will print the variable's value in the progress window. Does the variable contain the name of the group layer?