1

I have a python script that turns off all layers in an ArcGIS Pro project and then turns back on the Basemap layer. However, I need to convert this into a script tool so that it can be used in ModelBuilder. Either as individual script tools, one that turns all layers off and one that turns the Basemap layer back on; or as one script tool that does both.

Here is the script I have at the moment.

Turns all layers off

p = arcpy.mp.ArcGISProject("Current")
m = p.listMaps("*")[0]
lyrList = m.listLayers()
for lyr in lyrList:   
   lyr.visible = False

Turns Basemap layer on

p = arcpy.mp.ArcGISProject("Current")
m = p.listMaps("*")[0]
lyrList = m.listLayers('Topographic')
for lyr in lyrList:   
   lyr.visible = True

1 Answer 1

1

Instead of using arcpy.mp.ArcGISProject("Current") that works within an open Project, use arcpy.GetParameterAsText(0) for your p variable, this will allow you to select the Project you want as input. Then attach the script to a script tool, see how to in Adding a script tool. Make sure you select the right data type for your parameter (ArcGIS Pro project).

If you want to group both scripts, this is how your script should look like (don't forget to save the changes in the end):

p = arcpy.GetParameterAsText(0)

m = p.listMaps("*")[0]
lyrList = m.listLayers()
for lyr in lyrList:   
   lyr.visible = False

basemap = m.listLayers('Topographic')[0] # if you have only one layer called 'Topographic'
basemap.visible = True

p.save()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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