[GIS] arcpy.mapping.ListLayers() without calling current mxd document

arcgis-10.2arcpybatchsymbology

How can I use the arcpy.mapping.ListLayers() function without calling the current mxd? I'm hoping to run the Python script without having an ArcMap document open.

My setsymbology code runs extremely slowly as it is now. I could do it faster manually, and my computer is definitely not the limiting factor. I'm guessing it's slow because ArcMap is open. I'll show the code as it is now and the code that I'm trying to figure out a way for it to call an input directory/workspace rather than an mxd, but unfortunately it gets errors ('module' object has no attribute 'Listlayers').

# Import system modules
import arcpy
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

symbologyLayer = r"C:\VMshared\small_example_valley3\SnowColor2\snowdepthCOLOR.tif.lyr"

for lyr in arcpy.mapping.ListLayers(mxd):
    arcpy.ApplySymbologyFromLayer_management(lyr,symbologyLayer)

Best Answer

To your first question:

how can I use the arcpy.mapping.ListLayers() function without calling the current mxd?

I would say the answer is to use something like:

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name  # Added to show that ListLayers is working

in place of:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name  # Added to show that ListLayers is working

C:\temp\test.mxd does not need to be open and if you want to save changes to it use mxd.save()


In answer to what your question became, there is a code sample in the Online Help which I abbreviated slightly. It appears to use the same coding pattern that you did in your answer:

# Name: ApplySym.py
# Purpose: apply the symbology from one layer to another

# Import system modules
import arcpy

# Set the current workspace
arcpy.env.workspace = "C:/data"

# Set layer to apply symbology to
inputLayer = "sf_points.lyr"

# Set layer that output symbology will be based on
symbologyLayer = "water_symbols_pnt.lyr"

# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)