ArcGIS Pro 3.2.1
I have a Python script that converts 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 (4, 5, 8, 9, 10, 14, 15, 16, 20, 24, 25, 26, 29, 30)
That script is the only Python script I ever use. I want to save it as the default script so that it opens automatically when I open the Python window.
Could functionality be added that lets me set a default Python script? That would be more efficient than opening the file via File Explorer and copying/pasting the script into the Python window each time I want to use it.
You could make a script tool (in a toolbox) from your code, and run it that way.
This tutorial has two notebooks that show converting a workflow from a notebook to a script tool.
https://learngis.maps.arcgis.com/home/item.html?id=24e13f2f4fe8453b9d1107e1f5b1d3cf
Here is the help topic: https://pro.arcgis.com/en/pro-app/3.1/help/analysis/geoprocessing/basics/create-a-python-script-tool...
You could set up the tool so it takes an input layer (even set your favorite layer as the default) or even easier, not have any parameters at all.
Use arcpy.addMessage to print the result in the tool messages.
https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/addmessage.htm
Thanks @BobBooth1.
...or even easier, not have any parameters at all.
Are you suggesting the tool would use whatever layer is selected in the Contents pane?
Hi Bud,
The code, as written, would pick the layer:
lyr = m.listLayers('MY_USER.ROADS')[0]
But you could also set it up so it would accept a drop-down pick of layers on your map.
Bud,
So two ways to go:
Script tool with no parameters, you would double-click the tool and click Run, and it would apply the definition query to the hard-coded layer.
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
# I hard-coded my layer name, TestBob here
lyr = m.listLayers('TestBob')[0]
qdef_selected_features(lyr)
Script tool with a single input parameter that lets you pick the layer:
# configure script to accept a layer input from the script tool
lyr = arcpy.GetParameterAsText(0)
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
thelyr = m.listLayers(lyr)[0]
qdef_selected_features(thelyr)
The difference is you can set the script tool to accept an input feature layer, so you can choose a layer from the map.
So you double-click the tool and see:
Click Run...
The hard-coded target layer gets its definition query updated.
Versus the case where you set up a script tool with an input layer parameter:
The difference when setting up the script tool is you have to specify a parameter and set the type to feature layer.
@BobBooth1 Thanks Bob. That helps.
Thanks @BobBooth1 for helping @Bud with an existing solution. I'll add that there are some additional options for you:
1. If you go the route of a Script tool, and you run the same script repeatedly on a schedule, you could look into scheduling the tool to run: Schedule geoprocessing tools—ArcGIS Pro | Documentation
2. Notebooks in ArcGIS Pro can serve as a document containing a (Python code) workflow.
3. Probably not what you are looking for but possibly more efficient than copy/pasting code from a script: You can right-click the prompt area of the Python Window and select Load Code from the context menu to load a script file's contents directly into the prompt.
In summary, we will close this idea as there are established routes for achieving a similar result that work better with the intended use pattern of the Python Window and the broader user story of Python in ArcGIS Pro.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.