Select to view content in your preferred language

Convert labels to graphics broken with Python?

1118
5
Jump to solution
10-27-2022 01:11 PM
by Anonymous User
Not applicable

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)

 

Scott431_0-1666901648985.png

Scott431_1-1666901667942.png

 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DavidSolari
Frequent Contributor

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.

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

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.

Scott431_0-1666908383778.png

 

0 Kudos
DavidSolari
Frequent Contributor

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.

by Anonymous User
Not applicable

Success!!!! I had tried that already, but I did "graphics" instead of "graphics[0]". Adding the [0] index made all the difference, thanks!

by Anonymous User
Not applicable

I guess it makes sense considering the types returned...

Scott431_0-1666909056034.png

 

0 Kudos