2

I'm trying to apply a definition query to multiple layers within a Layer Group by using the following script where I would simply change the values for my 'SUBCODE' field:

import arcpy

#Variables to form defintion query
field = '"SUBCODE"'
values = "'1152','1153'"
#concatenate query syntax
queryStr = str(field) + "=" + str(value)
#Specify the aprx project (CURRENT), dataframe (Layers)
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Map_3D")[0]
#Apply defintion query to specified layer group 
for lyr in m.listLayers( "3D Layers")[0]:
    if lyr.supports("DEFINITIONQUERY"): 
    lyr.definitionQuery = queryStr
arcpy.RefreshActiveView()       
del aprx

This is the error I'm receiving when I run this in Arcpy:

Traceback (most recent call last):
  File "<string>", line 12, in <module>
IndexError: list index out of range

There's a similar question posted on SE that applies the same logic of applying a definition query to a Layer Group, however I'm using ArcGIS Pro and the post refers to ArcMap. There are some migration from arcpy.mapping to ArcGIS Pro I'm having trouble with - http://pro.arcgis.com/en/pro-app/arcpy/mapping/migratingfrom10xarcpymapping.htm

Here's an illustration of my Layer Group:

enter image description here

3
  • Thank you so much for your response! I've added the asterisk at the end of "3D Layers*" and came up with the following error: Traceback (most recent call last): File "<string>", line 12, in <module> IndexError: list index out of range
    – pac_co
    Commented Nov 9, 2017 at 16:53
  • Here's what I get when I print with: print(m.listlayers()) - print(m.listlayers()) Traceback (most recent call last): File "<string>", line 1, in <module> AttributeError: 'Map' object has no attribute 'listlayers' I've added an illustration of my Layer Group in my original post. Hopefully the logic in my script applies to this type of Layer Group?
    – pac_co
    Commented Nov 9, 2017 at 17:13
  • made the change and this is the error I've received - Traceback (most recent call last): File "<string>", line 12, in <module> TypeError: 'Layer' object is not iterable
    – pac_co
    Commented Nov 9, 2017 at 17:27

1 Answer 1

3

You cannot list layer groups with listlayers and you do not want to list groups because you cannot apply a definition query to a layer group. You want to list individual layers and then apply the definition query to each of them so change line 12 to:

for lyr in m.listLayers("CONDO*"):

Not the answer you're looking for? Browse other questions tagged or ask your own question.