Unable to save label expression in arcpy module 3.3

379
2
09-20-2024 07:16 AM
PrayagShah
Emerging Contributor

Hello everyone, 

I am trying to write code that saves label expressions before performing an Add Join, as the join process messes up the expressions. After the Add Join is completed, I plan to restore the label expressions.

We are on the verge of migrating from the arcpy.mapping module to the arcpy.mp module. According to the documentation, it seems that label functionality has been changed to the LabelClass object. I can confirm that my layer supports label expressions, as shown by the command lyr.supports("SHOWLABELS"). However, when the code reaches the part where a setDefinition is implemented, I get an error message.

"ArcGIS Pro caused an error and it causes script to break". 


code used in arcpy.mapping (just of reference):

lblExpression = opplan_outline.labelClasses[0].expression

performing Add join

# retrieving label expression
opplan_outline.labelClasses[0].expression = lblExpression


Code I am trying to convert in arcpy.mp module
opplan_outline = map_obj.listLayers("Opplan_Layer_Outline")[0]
label_defs = []
if opplan_outline.supports("SHOWLABELS"):
         for lblClass in opplan_outline.listLabelClasses():
              label_defs.append(lblClass.getDefinition("V3"))

performing add join 

if opplan_outline.supports("SHOWLABELS"):
        for i, lblClass in enumerate(opplan_outline.listLabelClasses()):
              lblClass.setDefinition(label_defs[i])

Error message is ArcGIS Application has stopped working. even though Arcgis Pro is not open. And I have also figured out that the issue lies in when the set definition comes into place. 

PrayagShah_0-1726841681337.png

 

Thank you everyone in advance. 

2 Replies
HaydenWelch
MVP Regular Contributor

Have you tried using V2 as the CIM definition? I haven't worked with Arcmap at all really, but I know the CIM version has changed a lot and it could be an issue with that.

Otherwise you're stuck in implementation struggles and might have to manually port the CIM defs over one attribute at a time.

0 Kudos
DuncanHornby
MVP Notable Contributor

It's not possible to answer this question as you do not show what you do at the line "performing add join". I'm guessing that what ever you are doing there is creating a corrupted definition object which is the input to lblClass.setDefinition() and causes the script to catastrophically fail. I believe a limitation of accessing CIM is that there is no validation of the parameter, unlike tools you call in arcpy. So if you are not super careful in how you alter the definition object then you feed into CIM something that syntactically invalid and having by-passed any sort of validation you kill the script/application.

0 Kudos