1

This is a modification of Assigning keyboard key to Python script in ArcMap?

I want to assign a shortcut key to call custom Python script while editing vector layer in ArcGIS 10.3. The rationale behind Python script that called by shortcut is to minimize steps required to turn on/off particular layers in the Table of contents. The answer provided in the initial question does not solve the problem.


'''
Script turns off A & B layers.
'''

import arcpy

#A list of layer names that needs to be turned off.
names = ["A", "B"]

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)

for layer in layers:
  if layer.name in names:
    layer.visible = False

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

1
  • 1
    What about creating a Python Add-in, which is then added as a button on a toolbar, and set a keyboard shortcut on that button?
    – Midavalo
    May 18 '17 at 14:44
1

Solved by a Python add-in for ArcGIS.

import arcpy
import pythonaddins


class ButtonOn(object):
    """Implementation for btn2 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        names = ["A", "B"]
        mxd = arcpy.mapping.MapDocument("current")
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        layers = arcpy.mapping.ListLayers(mxd, "*", df)
    for layer in layers:
      if layer.name in names:
        if layer.visible:
          layer.visible = False
        else: 
          layer.visible = True
    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()

This script creates button that turns layers 'A' and 'B' on/off. Shortcut keys were assigned through a Customize menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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