Select to view content in your preferred language

ApplySymbologyFromLayer works in the Python window on ArcGIS Pro, but not in a ToolBox

437
1
01-17-2020 07:39 AM
Gustavo_Souza
New Contributor III

I am having this issue for months now, I am keeping up with the topics about it here, but no solution yet.

ApplySymbologyFromLayer does not work in a Python Toolbox. It does work on the Python window, but in a toolbox it does not show any result. No error, no warnings, just no effect at all. 

My Python toolbox code is decent sized, it has a number of tools and none of them have any issue, does a lot of different tasks, but for some reason, it can't Apply Symbology From Layer. When the line runs, I can see the layer being locked for a while, and then the code complete without any results.

There is one workround, which is constantly suggested here, and I was using successfully: 

new_lyr_file = arcpy.mp.LayerFile(symbology_layer)
new_lyr = new_lyr_file.listLayers()[0]
old_lyr = aprxMap.listLayers(inputlayer)[0]
old_lyr_name = old_lyr.name
new_lyr.updateConnectionProperties(new_lyr.connectionProperties, old_lyr.connectionProperties)
new_lyr.name = old_lyr_name
new_lyr_file.save()
aprxMap.insertLayer(old_lyr, new_lyr_file)
aprxMap.removeLayer(old_lyr)

Until i noticed that it screws up the URL in the Data Source. It brings some issues when trying to upload the Map to AGOL. 

The funny part is that both 

- arcpy.management.ApplySymbologyFromLayer(Input, Symbol)

and 

- arcpy.management.ApplySymbologyFromLayer(Input, Symbol)

lines of code works perfectly when using the Python Windom on ArcGIS Pro. 

Any ideas why the code would run in the Python Window but not on the Toolbox?

0 Kudos
1 Reply
Luke_Pinner
MVP Regular Contributor

I've never used ApplySymbologyFromLayer in a toolbox so can't comment on the issue, but what I do that works is set the symbology property of output layer parameters, and the symbology is automagically applied when the output parameter is set at the end of my execute method:

class Tool(object):
    def getParameterInfo(self):
        params = []
        params[0] = arcpy.Parameter(
            displayName="Output Raster Layer",
            name="out_raster_lyr",
            datatype="GPRasterLayer",
            parameterType="Derived",
            direction="Output")

        params[0].symbology = r'path\to\symbology.lyrx'

        return params
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
    def execute(self, parameters, messages):
            layer = do_something_and_return_a_layer()
            arcpy.SetParameter(0, layer)  # Layer gets added to the map with symbology applied without me manually applying it.
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos