I am trying to run a python script that will convert labels to graphics. However, it does not work when the script is run from a separate python script, or as a toolbox script. It only works when run from the python window inside ArcGIS Pro. I tried the exact same commands, so I know it's not a typo (obviously changed where necessary to refer to the "current" map instead of a path, etc).
The stand alone script / toolbox scripts dont' give any errors, they just doesn't add the graphics layer to the map. Furthermore, when I try to save the output from Convert to Graphics tool to a layer file it creates a bad file. Again, it doesn't throw any errors, the file just doesn't work when I try to load it into a map. When I opened the .lyrx file a text editor you can see the CIM info is mostly nonexistent.
Tell me I'm not the only one!
(Using Pro 2.9)
Solved! Go to Solution.
Replace your call to the "SaveToLayerFile" function with "m.addLayer(graphics)". The former is implicitly adding the new layer to the active map while Pro is open but that's not how GP tools work outside of Pro, so you need to explicitly add the layer through the Map object's method. If that doesn't work, try extracting the specific graphics layer result with "graphics = graphics[0]" first, sometimes full result objects act a bit odd.
Try dropping
graphics =
from the cartography construction line. The tool doesn't return anything and the variable graphics (which would equal None) may be obscuring the 'graphics' in the tool
I already tried that and it doesn't help. The tool actually does return a graphics layer object that can be referenced as a variable. Screenshot below I returned info about the results object. I get the exact same arcpy.da.Describe info when I successfully run the code in the ArcPro Python window, as when I run it as a standalone script and it doesn't work.
Not sure if that's useful for troubleshooting. Probably more confounding than anything.
Replace your call to the "SaveToLayerFile" function with "m.addLayer(graphics)". The former is implicitly adding the new layer to the active map while Pro is open but that's not how GP tools work outside of Pro, so you need to explicitly add the layer through the Map object's method. If that doesn't work, try extracting the specific graphics layer result with "graphics = graphics[0]" first, sometimes full result objects act a bit odd.
Success!!!! I had tried that already, but I did "graphics" instead of "graphics[0]". Adding the [0] index made all the difference, thanks!
I guess it makes sense considering the types returned...