Hello, I am trying to adjust the labelling properties of a point layer through python by accessing the CIM in a script tool. When I run the tool, the properties are applied to the layer, however they do not draw. The labelling properties dialogue box reflects my changes, however the labels are not drawing to reflect the changes. Code below
I am attempting to change the rotation of a label by assigning the rotationField to a field in my attribute table called 'Bearing'.
lyr = m.listLayers(out_pointslayer + "_Map")
for eachclass in ldef.labelClasses:
if eachclass.name == 'Class 1':
lblclass = eachclass
lblclass.maplexLabelPlacementProperties.rotationProperties.rotationField = 'Bearing'
lyr.showLabels = True
After running this code in a script tool, the labelling properties seem to reflect the changes.
However the rotation does not get applied in the rendering of the labels, however it works when I@@ do it manually.
Any Help would be much appreciated!
Solved! Go to Solution.
It may be the indent missing after your for loop, dunno if the class has to be set to visible also?
Just my random musings with little experience of arcpy label classes or Pro.
It may be a bug in the CIM access. The one time I tried it, and used CIM access to change label properties for a Query Layer, I discovered it resulted in the loss of the datasource as soon as I set the CIM definition using lyr.setDefinition. This made CIM access unusable for me.
In my particular case, it was possible to workaround the issue by saving the layer to disk first as *.lyrx layer file using lyr.saveACopy, then do a json edit using the ordinary Python json.load and json.dump methods, and finally reload the layer in the project. This requires good knowledge and understanding of the structure of the CIM though, and how it translates to the dictionary structure that json.load creates. It took me a couple of rounds and thought to get it right.
Also, ESRI definitely, and probably rightly so, discourages direct json editing of layer files, but it can be done if you are careful.
What is misleading in the GUI is that you don't have to enable it explicitly, just setting it appears to enable it as well. Convenient for the end-user , but confusing for the developer who doesn't expect a two-step process.