How to use ApplySymbologyFromLayer in Python-script?

2903
10
10-26-2017 12:40 AM
ThomasKölbel
New Contributor II

ArcGIS Pro 1.4, 2D-Map

I'm trying to change the symbology of a featureclass  through a layerfile (.lyrx):

import arcpy

arcpy.MakeFeatureLayer_management("myFc", "myFc_lyr")

inputLayer = "myFc"
symbologyLayer = "myFc.lyrx"
symbologyFields = [["VALUE_FIELD", "StrKlasse", "StrKlasse"],["VALUE_FIELD", "Abschnitt_Ast", "Abschnitt_Ast"]]

arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer, symbologyFields)

Running the commnds in the python-window, the symbology will be changed as expected, but in a python script from a toolbox nothing happens. The attributes in lyrx-file and the fc/layer match correctly.

Thanks for any solution.

10 Replies
DanPatterson_Retired
MVP Emeritus

http://pro.arcgis.com/en/pro-app/tool-reference/data-management/apply-symbology-from-layer.htm

In both examples, not in immediate mode, the scripts set the arcpy.env variable (see examples 2 and 3) 

Try adding that information since it is lacking your script

0 Kudos
ThomasKölbel
New Contributor II

Thank's for your answer. I've tried:

import arcpy

from arcpy import env

env.workspace = r"C:\ArcGIS\Projects\MyProject6"

arcpy.MakeFeatureLayer_management("myFc", "myFc_lyr")

inputLayer = "myFc"
symbologyLayer = "myFc.lyrx"
symbologyFields = [["VALUE_FIELD", "StrKlasse", "StrKlasse"],["VALUE_FIELD", "Abschnitt_Ast", "Abschnitt_Ast"]]

arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer, symbologyFields)

All sources are in the same Project folder.  The script runs and completes with no error, but nothing is changed.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I am not used to seeing script tool without a dialog that allows you to select layers etc http://pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/basics/create-a-python-script-tool.htm

The need to refresh the map doesn't exist as an option as it did with ArcMap, so that can't be an issue.  That leaves something in the toolbox environment.  Try converting your hardcoded parameters into variables for selection

XanderBakker
Esri Esteemed Contributor

If the script runs as a standalone script, the arcpy.ApplySymbologyFromLayer_management  will probably have little effect if you don't:

  • apply it to a layer in a MXD, refresh the map and store the MXD
  • or save the layer as a layerfile with the applied symbology
ThomasKölbel
New Contributor II

There is no mxd in Pro.

0 Kudos
DanPatterson_Retired
MVP Emeritus

same message as Xander... just called an *aprx

ThomasKölbel
New Contributor II

Converted the four Parameters inputLayer, symbologyLayer and ValueField1 and ValueField2 to selectable params in my script. No changes like before.

I've written some scripts before without dialogs - just running commands. They all worked well. So hardcoded params can be used.

0 Kudos
WillArellano
Occasional Contributor

I am having the same issue as the original poster but I am running ArcPro 2.6, python 3

My code to add symbology from a layer works great if I run it in a Notebook, but does not work when used in a script tool. When run in the script tool no errors are thrown, and a little lock icon shows up next to the input layer in the Contents window, so it looks like it is working, but the symbology is unchanged. 

If I use the geoprocessing tool and run AddSymbologyFromLayer, it works perfectly so I know it isn't a bad .lyrx or other file issue. 

Ideas?

RebeccaKRansom
New Contributor

I am also having the same issue in ArcGIS ArcPro 2.9, python 3.

I have a lyrx file and the ApplySymbologyFromLayer tool works from the python window and in a standalone script, but when I try to run it from a toolbox script it is not updating. Also, I am getting no errors.