Python CIM Accessing Label Properties

401
7
Jump to solution
10-28-2020 12:58 PM
New Contributor III

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")[0]
ldef=lyr.getDefinition('V2')
for eachclass in ldef.labelClasses:
if eachclass.name == 'Class 1':
lblclass = eachclass

lblclass.maplexLabelPlacementProperties.rotationProperties.rotationField = 'Bearing'
lyr.setDefinition(ldef)

lyr.showLabels = True

aprx.save()

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!

Reply
0 Kudos
1 Solution

Accepted Solutions
New Contributor III

Solved: Need to add this line of code in.

lblclass.maplexLabelPlacementProperties.rotationProperties.enable = True

View solution in original post

7 Replies
MVP Regular Contributor

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.

Reply
0 Kudos
New Contributor III

Didn't work unfortunately, Thank you for the response though!

Reply
0 Kudos
MVP Regular Contributor

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.

Reply
0 Kudos
New Contributor III

For sure, I would like to keep editing the actual json as a last resort but it might be the only option right now, Thank you. 

Reply
0 Kudos
New Contributor III

Solved: Need to add this line of code in.

lblclass.maplexLabelPlacementProperties.rotationProperties.enable = True

View solution in original post

MVP Regular Contributor

Nice you got it to work. Haven't had a look at the rotation properties myself, so missed the obvious.

Reply
0 Kudos
MVP Esteemed Contributor

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.