Select to view content in your preferred language

ApplySymbologyFromLayer_management not working on script tool. Works on Python console.

3329
3
11-06-2014 06:06 AM
MauricioSilveira
New Contributor

Hey guys,

I can't update layer symbology from my script tool. It's working on the Python console, and it's driving me nuts.

I've reduced my code to this most basic example that now follows.

On Python console:


arcpy.ApplySymbologyFromLayer_management("climate_pp_geq1020", "//VBOXSVR/data/10min/binarySymbology.lyr")


This WORKS.

On script tool:


import arcpy

 arcpy.ApplySymbologyFromLayer_management("climate_pp_geq1020", "//VBOXSVR/data/10min/binarySymbology.lyr")

The EXACT SAME THING ("import arcpy" is the only addition) DOES NOT WORK for me when ran from a script tool instead of the console.

I don't receive any error messages either.

The layer I'm trying to alter is a raster one in case it helps.

I'm using ArcGIS 10.1

I've also tried arcpy.mapping.UpdateLayer instead and it also does nothing with no error messages.

I'd greatly appreciate it if anyone could give me a solution to this.

Thanks!

0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor

Is the script tool running in the background? This will not work as background geoprocessing is always in a separate process from ArcMap (where your layer "climate_pp_geq1020" is living).

You can force your script to run in the foreground by either

1) Setting the checkbox "always run in foreground"

or

2) Turning off background geoprocessing in Geoprocessing/Options

0 Kudos
MauricioSilveira
New Contributor

Hi Curtis,

Thanks for your reply.

The script was indeed running in the foreground; it's not that.

I fixed the issue by getting a reference to the layer to update by calling ListLayers like so:

layerToUpdate = arcpy.mapping.ListLayers(mxd, "climate_pp_geq1020", df)[0]

arcpy.ApplySymbologyFromLayer_management(layerToUpdate, "//VBOXSVR/data/10min/binarySymbology.lyr")

I have no idea why this is necessary within the script but not the Python console. Can someone please explain?

Thanks.

0 Kudos
DuncanHornby
MVP Notable Contributor

I would image the problem is that in your script tool you are not providing enough information for it to find the layer. The ApplySymbologyFromLayer tool takes a FeatureLayer as input (it says that in the help) but just giving it a name (which you did) could be interpreted as a FeatureClass, an object that does not support symbology. So by using the ListLayers approach you are actually providing the tool with a FeatureLayer object.