I'm using Pro 3.2 currently. I'm trying to work on some code that runs a calculate field on a shapefile and then will label that shapefile once the calculation is done. I'm able to get this code working in Notebook running cell by cell, but once it put it into my actual code base, it's failing with this error message:
LayerObject: Label Class expression is not valid.
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers()[0]
l.showLabels = True
lc1 = l.createLabelClass(
name = 'EI - VBScript2',
expression = '[MUSYM] & vbCrLf & Round([SLength], 0) & " ft." & " X " & Round([Avg_Slope], 0) & " ft." & vbCrLf & " EI = " & Round([EIFin], 2)',
sql_query = "",
labelclass_language = 'VBScript')
lc1.visible = True
When I'm looking at the shapefile after the failure message, all of my field calculations are being done correctly and being updated to the attribute table. It just seems to be having issues when it comes to displaying the label. I can also go through and manually label everything, but since this is going to be used by a few people, I'd rather just have the labels generate how they need to look.
I feel like I'm missing something super simple here to get it working. 😅
I tried your code with a cut down expression in 3.4 and it worked on my test data. Try a simpler expression and then slowly build it up until you find the subexpression that breaks it.
Thanks for the quick reply! I've been spending some time messing around with the code, and it seems like if I break it up into two separate scripts, like Script A runs my calculations and then Script B labels my data it works. Completely unsure why - maybe the shapefile is getting locked up somehow?
Running it as two scripts works fine-ish for now (an extra step usually means someone is going to either forget to click it or complain about it lol) but I might try call it as a subprocesses since having it in the main code body seems to be making the code fail.
Another way to chain two scripts together is to write two script tools, then chain them together in Modelbuilder. ArcGIS does some checks and cleanup between tools so that might be enough to fix your bug.
Layer—ArcGIS Pro | Documentation
Newly added label classes may not display initially. There are two levels of settings to display labels. First, the Layer class has a property called showLabels and needs to be set to True. Second, each LabelClass has a visible property and it also needs to be set to True. Refer to the LabelClass help topic for more information and code samples.
Is worth having a look at as well
Is this different from what Jackson did in their listing?
sometimes it it is the order that things are done
other times it is whether the instance exists prior to doing something
and then there is 'time' ... a script may act too fast for steps to get fully realized
Hi Dan! I think this might be what's happening (my script acting too fast). Do you happen to have any ideas on effective ways to make the script pause?
I'm seeing some time.sleep() and asyncio.time() modules that can be used to pause scripts but I haven't given it a try yet.
I have used time.sleep(some number) in the past.
Attaching scripts to toolboxes seems to mitigate that necessity