Hi,
I'm trying to create a script that will add output with a predefined symbology (.lyrx parameter). Apparently you can control that with tool parameters (https://community.esri.com/t5/python-questions/cannot-apply-symbology-from-layer-using-python/m-p/16...) but I can't figure how to set that there AND THEN have the data added to the map.
Here is my current code:
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.activeMap
aprxMap.addDataFromPath(arcpy.management.ApplySymbologyFromLayer(in_layer=arcpy.MakeFeatureLayer_management(ZONE_DETAILS_FILLED, "Carte d'occupation du territoire"), in_symbology_layer=Symbologie, symbology_fields="VALUE_FIELD TYPE TYPE", update_symbology="MAINTAIN"))
#Where
#ZONE_FILLED_DETAILS is the created output from previous tool
#Symbology is the .lyrx that is defined in the script parameters
It works really well with raw path within the python window of ArcGIS Pro. However, if I try it within the full script I get this error:
Traceback (most recent call last):
File "<string>", line 61, in <module>
File "C:\Program Files\XTools\XTools AGP\Python\Tools\FeaturesToPointsTool.py", line 7, in <module>
import XTools.XToolsAGP.Geoprocessing
ModuleNotFoundError: No module named 'XTools'
Traceback (most recent call last):
File "C:\Temp\22_0993_Outil_carbone\SCRIPTS\OutilCarboneCERFO_v4.py", line 643, in <module>
OutilCarboneCouches(*argv[1:])
File "C:\Temp\22_0993_Outil_carbone\SCRIPTS\OutilCarboneCERFO_v4.py", line 550, in OutilCarboneCouches
aprxMap.addDataFromPath(arcpy.management.ApplySymbologyFromLayer(in_layer=arcpy.MakeFeatureLayer_management(ZONE_DETAILS_FILLED, "Carte d'occupation du territoire"), in_symbology_layer=Symbologie, symbology_fields="VALUE_FIELD TYPE TYPE", update_symbology="MAINTAIN"))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 3744, in addDataFromPath
return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path, web_service_type, custom_parameters,), True)))
RuntimeError
Does anyone have a cue for me?
Thanks!
Solved! Go to Solution.
Here is my workaround!
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.activeMap #Map must be active when script launched
lyr = aprxMap.addDataFromPath(fc) #add data to map
L = aprxMap.addDataFromPath(lyrx_symbology) #add lyrx to map
sym = L.symbology # get symbology from lyrx
lyr.symbology = sym #apply symbology to data
aprxMap.removeLayer(L) #remove lyrx from project
I am looking to do the same thing in a stand-alone script, so I'd love to know if there's a cure as well!
Here is my workaround!
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.activeMap #Map must be active when script launched
lyr = aprxMap.addDataFromPath(fc) #add data to map
L = aprxMap.addDataFromPath(lyrx_symbology) #add lyrx to map
sym = L.symbology # get symbology from lyrx
lyr.symbology = sym #apply symbology to data
aprxMap.removeLayer(L) #remove lyrx from project