arcpy.management.ApplySymbologyFromLayer() works from a standalone script, or the Arc Python window, but NOT from a toolbox script inside a map session. It executes without an error, but it doesn't change the symbology.
The only possible reason I can see is that the object type is a little different for a layer depending on whether it's a standalone/python window or the toolbox script.
standalone/python window layer type: <arcpy._mp.Layer object at 0x000001C3E7390C10>
toolbox script layer type: <MappingLayerObject object at 0x000001AEA43E2270>
Does anyone know what's going on?? I have run into other weird glitches with ArcGIS Pro where tools function differently in toolbox scripts, and it's very frustrating.
Script example that only works if run as a standalone script:
Solved! Go to Solution.
From the tool doc:
To see the symbology created in a script tool, the tool must include the layer as a derived output parameter.
Are you using a custom toolbox where you can specify the parameter type from a dialog or is this a parameter-less tool? A toolbox script will behave properly if you define the parameters in the tool and select them from the dialog like this?
I was grabbing the layers through the map.listLayers() method because one of them will be an in-memory layer that I created in my script, not a user input.
But I just tried making them both user input parameters (Feature Layers), and it still didn't work.
From the tool doc:
To see the symbology created in a script tool, the tool must include the layer as a derived output parameter.
Ahh, thanks! I guess I should have read the doc more thoroughly. I've been avoiding derived parameters, because I didn't understand them.
Here's the code that works.
Actually, I found a better solution for my purposes. I was having an issue with the derived output adding a second copy of the layer being affected, when that layer was an in-memory layer. When I changed so that it wasn't added to the map at all until the derived output from the symbology tool, then I realized that the derived output doesn't get added to the map until the script ends. This is a problem because I need to apply symbology, and then edit it within the same script.
Here's the new method that works better when your affected layer is an in-memory layer: no duplication, and immediately added to map.
I know this seems like a pointless script... it's just a fragment of a larger script.
This worked for me. Thanks!
Thanks for posting! This fixed my issue, too, without needing to mess around with all the derived output stuff.
Hi
More or less the same code works for me when I take the source layer from lyrx file and not from the TOC