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.
Thanks a lot for sharing this workaround, Michael. I don't know how many hours I would have had to spend to find the same combination/order of executing the different procedures....probably never would have found it....sometimes I feel like I keep running into limitations/bugs even though ArcGIS Pro is getting 4 years old....
Kind regards,
Martin
Thanks Michael, this worked for me within my python toolbox. I'm just posting a slightly more complete sample which includes the ApplySymbologyFromLayer function
arcpy.AddMessage('Apply style')current_project = arcpy.mp.ArcGISProject("CURRENT") ags_map = current_project.listMaps()[0]
old_lyr = ags_map.addDataFromPath(file_path)
arcpy.management.ApplySymbologyFromLayer(old_lyr, r"C:\Temp\sourcelayer.lyrx", "VALUE_FIELD FIL_COLOUR FIL_COLOUR", "UPDATE")
arcpy.SaveToLayerFile_management(f.output_name, r"C:\Temp\templayer.lyr", "RELATIVE")
new_lyr_file = arcpy.mp.LayerFile(r"C:\Temp\templayer.lyrx")
new_lyr = new_lyr_file.listLayers()[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()
ags_map.insertLayer(old_lyr, new_lyr_file)
ags_map.removeLayer(old_lyr)
I asked to be attached to the bug BUG-000108497. It has been closed as a duplicate of https://my.esri.com/#/support/bugs/BUG-000106281
This is strange because BUG-000106281 sites the pro SDK and was closed at 2.3.0. I have supplied a sample and will try to ensure a bug gets created and is not closed.
See BUG-000108497: Running the Apply Layer Symbology tool as a script i.. , I tried the workaround (reopening the project ). Interestingly the first Apply layer command in the Python Toolbox does work (without reopening) but the other commands don't.
I have added an updated to this case after receiving a response from Esri:
https://community.esri.com/thread/216962-apply-lyrx-file-symbology-via-python
I finally worked out how to do it a Python Toolbox, here's an example:
In Init:
Hi all,
I'm new to Python but I think I may have (somewhat accidentally) found a workaround for those encountering this error within a Python Toolbox. I saved my output symbology template (.lyrx) in the same folder as my Python Toolbox. After defining my parameters in the getParameterInfo(self) method of the tool, I added the following 2 lines:
When I ran the tool, the correct symbology was applied to the output. This worked on my own laptop and my University's remote server, so I feel confident that it's a viable solution.
The difference that I've noticed is that I applied they symbology in the getParameterInfo method rather than the execute method of the tool.
Note: I ran the tool countless times (and received countless errors) over a few days when trying to update my output symbology. During my trial and error, I do recall receiving an "Attribute Error: 'str' has no attribute 'symbology'". I still don't understand why it "output_param.symbology" worked this time and not previous times, but like I said, I'm new to programming.
Here's a simple solution for ArcGIS Pro 3 python script tool results I got to work after struggling with this issue and it doesn't involve messing around with the script tool parameters mentioned above. It creates a feature layer from a feature class, uses ApplySymbology, and then adds it to the map. This assumes you have an existing layer file (lyrx) with the desired symbology: