[GIS] Apply Symbology to Layer not working outside of ModelBuilder

arcgis-10.2arcgis-desktopmodelbuilder

I am running ArcGIS 10.2.2 Version 10.2.2.3552, using ArcMap.

I am creating a python script and one of the things i need to do is apply symbology from layer to a shapefile in my mxd that is maintained outside of the mxd. It works fine in model builder but when i run it in python i get multiple errors.

Code:

# Import arcpy module
import arcpy


# Local variables:
Tioga = "Tioga\\Tioga"
Tioga_lyr = "C:\\Users\\jcarl\\Desktop\\Dynamic LH Map\\Symbology\\Tioga.lyr"

# Process: Apply Symbology From Layer
arcpy.ApplySymbologyFromLayer_management(Tioga, Tioga_lyr)

Model

Runtime error Traceback (most recent call last): File "", line 10, in File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6004, in ApplySymbologyFromLayer raise e ExecuteError: ERROR 000966: The input layer is not valid Failed to execute (ApplySymbologyFromLayer).

Best Answer

EDIT: I realized my first answer was incorrect, this is an update (I forgot that the function needs a Layer object, not a dataset path).

I think the issue is that the you are passing "Tioga\Tioga" (which is a technically a string) but the Apply Symbology function needs a Layer object. You can create the layer object like this:

mxd = arcpy.mapping.MapDocument("CURRENT")
Tioga = arcpy.mapping.ListLayers(mxd,"Tioga1")[0]

arcpy.management.ApplySymbologyFromLayer(Tioga,symbologylayerpath)

Before trying this, rename your layer "Tioga1" in the table of contents, otherwise it'll grab the group layer and not work.

Apologies for the misdirection earlier.

Related Question