Using an arcpy script run from a toolbox, I want to import a feature class from a geodatabase into an ArcPro map and then symbolise that layer based on a lyrx file stored on disk. Here is a simplified version:
import arcpy, os
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('*')[0]
outputWorkspace = r"C:\Path\To\MyProject.gdb"
arcpy.env.workspace = outputWorkspace
out_fc = "fc_data"
out_fc_p = os.path.join(outputWorkspace, out_fc)
ref_lyrx = r"C:\Path\To\symbol_ref.lyrx"
m.addDataFromPath(out_fc_p)
out_fc_lyr = m.listLayers()[0]
arcpy.ApplySymbologyFromLayer_management(out_fc_lyr, ref_lyrx)
The script completes without error but no visible change is made to the symbology. Is this a bug? I've reviewed many similar questions but have been unable to resolve this.
Based on the help topic ...
Apply Symbology From Layer—Data Management toolbox | ArcGIS Desktop
the two optional parameters aren't specified
ApplySymbologyFromLayer_management (in_layer, in_symbology_layer, {symbology_fields}, {update_symbology})
which makes me wonder what the layer looks like before and after if this were done manually to ensure that the limitations of the 'immediate mode' are indeed being met (ie featureclass type etc).
Also the refreshActiveView no longer exists
Migrating from arcpy.mapping to ArcGIS Pro—ArcPy | ArcGIS Desktop
which would be the normal go-to
I'm trying it initially using only single symbol to match the symbology of the reference lyrx file so I didn't think the optional fields were required. Running it in immediate mode has the same effect as running it from the toolbox.
Unfortunate that refreshActiveView no longer exists. Previously in the desktop version I would use arcpy.mapping.UpdateLayer to update symbology of fields added to the TOC but that option too seems to have disappeared with the upgrade to Pro...
Hi Sean
I'm having the same problem with arcpy.ApplySymbologyFromLayer_management in ArcGIS Pro 2.1. You find any solution to the problem?
Not yet I'm afraid, but I'll keep trying to find a work around.
I tried also first converting the fc to a layer using arcpy.MakeFeatureLayer_management before adding to the map with addLayer and then using arcpy.ApplySymbologyFromLayer_management but the same thing happened: script completes successfully without error but no visible change is made.
Same here, and I'm seeing the same problem with applying symbology to rasters too.
Sharing with Python for broader awareness.
an issue with symbology from layer was reported on Beta 2.2 but there is mixed reports, you might wish to report this to tech support
They give an associated case which might be useful
Esri Case #02133585 - Applying symbology from layer does not work correctly
Sean, based on the details it seems you are running into this defect:
BUG-000108497: Running Apply Layer Symbology as a script in the Python window does not update the symbology in the map.
This is a quirky one and based on my testing I'd say the most reliable way to fix this is to clear the Display Cache via Project > Options > Display.
You can fix this programmatically though:
Updating the connection (same input and output) worked for me, thanks!
datapath = 'C:\\temp'
import arcpy
import os
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps('*')[0]
lyr = m.addDataFromPath(os.path.join(datapath, 'wrfout_d3.2015081400.f13.0000.UV10.tif'))
lyr0 = m.listLayers()[0]
print(lyr0)
asfl = arcpy.ApplySymbologyFromLayer_management(lyr0, os.path.join(datapath, 'wind_barbs_uv.lyrx'))
in_dict = {'dataset': 'wrfout_d3.2015081400.f13.0000.UV10.tif', 'database': datapath}
out_dict = {'dataset': 'wrfout_d3.2015081400.f13.0000.UV10.tif', 'database': datapath}
lyr0.updateConnectionProperties(in_dict, out_dict)