Update Symbology in ArcPro using a Stand Alone Script

1773
4
06-04-2021 02:49 PM
Status: Open
Labels (1)
RMiller
New Contributor II

When running the code below in ArcPro python window it works, but when ran in a stand alone script read into ArcPro it doesn't work...After talking with ESRI support, in order to change symbology the code has to be ran directly in the python window of ArcPro in order for it to work. That ArcPro doesn't have this capability just yet for it to be ran through a stand alone script. If you can look into this and make a stand alone script be able to run the arcpy.management.ApplySymbologyFromLayer(lyr, sym) that would be great!!!


for lyr in active_map.listLayers():
   if lyr.name == "Parcel_Buffer":
      sym = os.path.join(lyr_directory, "Parcel_Buffer_ArcGISPro.lyrx")
      arcpy.management.ApplySymbologyFromLayer(lyr, sym)
   elif lyr.name == "Parcel_Selection":
      sym = os.path.join(lyr_directory, "Selected_Parcel_ArcGISPro.lyrx")
      arcpy.management.ApplySymbologyFromLayer(lyr, sym)
   elif lyr.name == "Parcels_in_Buffer":
      sym = os.path.join(lyr_directory, "Parcel_Buffer_Intersect.lyrx")
      arcpy.management.ApplySymbologyFromLayer(lyr, sym)
   elif lyr.name == "Parcels_Sorted":
      sym = os.path.join(lyr_directory, "Sorted_Parcels_ArcGISPro.lyrx")
      arcpy.management.ApplySymbologyFromLayer(lyr, sym)
   else:
      "No symbol file for specified layer"

aprx.save()

Tags (3)
4 Comments
JohannesLindner

Is this what you want?

import os
import arcpy

aprx = arcpy.mp.ArcGISProject("path\to\your\project.aprx")
active_map = aprx.listMaps("MapName")[0]
lyr_directory = "..."
for lyr in active_map.listLayers():
    # etc
aprx.save()

 

Also, you could rewrite your code like this:

symbol_layers = {
    "Parcel_Buffer": "Parcel_Buffer_ArcGISPro.lyrx",
    "Parcel_Selection": "Selected_Parcel_ArcGISPro.lyrx",
    # ...
}
for lyr in active_map.listLayers():
    try:
        sym = os.path.join(lyr_directory, symbol_layers[lyr.name])
        arcpy.management.ApplySymbologyFromLayer(lyr, sym)
    except KeyError:
        print("No symbol file for layer {}".format(lyr.name))
RMiller

Hey @JohannesLindner , 

Thank you for commenting. I setup a python tool in ArcPro and I am running my script as a stand alone script. When I run my tool that's tied to my script it runs great with no errors. The only problem is that the symbology part doesn't actually change the symbology in the map. When I run my same script through the ArcPro window it will change the symbology in the map. I called ESRI support and they tried to look at it. That's when they told me to submit it as an idea because the symbology part doesn't work in a stand alone script right at this moment. 

I did take the second part you sent and tried restructuring my symbology code in the script. It runs great with no errors in my stand alone script but still no luck of it actually changing the symbology in the map.

JohannesLindner

Ah, now I get it...

can confirm that

changing the symbology WORKS when

  • running the script outside of ArcGIS (e.g. executing it in IDLE)
  • running the script in ArcGIS in the python window (either by copy/pasting or by importing the script)

changing the symbology DOES NOT WORK when

  • running the script as script tool
GeoNZ
by

This was a bug that was apparently fixed....but never was (alongside the 're-use maps' option when importing a Layout_