I am trying to add a layer to a map, then convert the labels to graphics. But when I run the script from a custom toolbox script inside the map document it gives me this strange error that I couldn't find any info on:
Invalid CIM dataset for path: CIMPATH=map/ground.json
When there is another layer in the map document, it is referenced in the bottom of the error instead of "map/ground". I set the Convert to Graphics tool to only work on a "single_layer", so I don't know how other layers could possibly be involved. It's not the main source of the error, but curious none-the-less.
When I run the script as a standalone script it works fine, no other changes necessary!
And here's another twist. It works when I run it from a custom toolbox script in the map, IF... I add the layer by hand before running the script. And before you suggest it, no, grabbing the layer from map.listLayers() after adding it does not fix the original version of the script. It has to literally be added by hand for this to work.
I've tried comparing the object type when I added the layer by hand vs. when I added it through python, and it's exactly the same. I really have no clue what the difference is. But I need to be able to run this through a map document toolbox.
Solved! Go to Solution.
I figured out a workaround.
Just use: map.addDataFromPath()
Instead of:
arcpy.management.MakeFeatureLayer()
map.addLayer()
Still seems like a glitch, but hey this works...
Actually... update needed.
You also need to pull the reference to the layer from your map after you add it.
So it would be:
added_lyr = map.addDataFromPath(my_path)
useful_lyr = map.listLayers(added_lyr.name)[0]
Then use the "useful_lyr" as the input to the Convert to Graphics tool.
Then add that graphics results to your map.
If you use the results from the addDataFromPath tool as the input to Convert to Graphics, instead of grabbing the layer reference from the map, it will do some funky stuff, such as added an extra copy of the graphics to your map, and also not let you edit the text of the graphics label. No clue why.
I figured out a workaround.
Just use: map.addDataFromPath()
Instead of:
arcpy.management.MakeFeatureLayer()
map.addLayer()
Still seems like a glitch, but hey this works...
Actually... update needed.
You also need to pull the reference to the layer from your map after you add it.
So it would be:
added_lyr = map.addDataFromPath(my_path)
useful_lyr = map.listLayers(added_lyr.name)[0]
Then use the "useful_lyr" as the input to the Convert to Graphics tool.
Then add that graphics results to your map.
If you use the results from the addDataFromPath tool as the input to Convert to Graphics, instead of grabbing the layer reference from the map, it will do some funky stuff, such as added an extra copy of the graphics to your map, and also not let you edit the text of the graphics label. No clue why.