ArcGIS Pro 3.2.1
I have a Python script that I use in the Python window to convert a selection to a definition query.
def qdef_selected_features(lyr):
desc = arcpy.Describe(lyr)
# Get a semicolon-delimited string of selected feature IDs
fid_list = desc.FIDSet.split(";")
# build the query definition
query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
# apply the query definition back to the layer
lyr.definitionQuery = query
aprx = arcpy.mp.ArcGISProject('current')
m = aprx.activeMap
lyr = m.listLayers('MY_USER.ROADS')[0]
qdef_selected_features(lyr)
Result:
OBJECTID IN (6, 7, 17, 18, 19, 23, 27, 28)
The script works for feature layers but doesn't work for standalone tables.
Traceback (most recent call last):
File "<string>", line 12, in <module>
IndexError: list index out of range
How can I enhance the script so that it works for non-spatial tables, too?
I'm a novice. Any other tips about improvements would be welcome.
Related: