I am currently working on a tool that uses the updateCursor function but the tool needs to be in an edit session to use. However some who may use the tool may end up not being in an edit session before running the tool and get an error. Rather than come ask me every time about the error, I'd like to implement an if statement in the script in the beginning that checks if the user is currently in an edit session, if they are then run tool. if not, a dialogue box explaining they need to be in an edit session and ask if they want the script to turn editor on. Formatted as such:
#if edit session is on: #run tool #elif edit session is off: #dialogue box explaining user has to be in edit session and asking if they want the script to turn editor on (Yes or no option): #if yes: #run tool #if no: #quit
I am trying to do this without using the work done by Mark Cederholm as referenced in this post: How do I access ArcObjects from Python?, and code examples provided by Matt Wilkie in his "Snippits.py" file.
Joshua, so it's actually a Runtime Error that says "Objects in this class cannot be updated outside an edit session [lists the feature class]." Then it says "Failed to execute (ToolName)." So there wasn't an error code you get and so I'm wondering if using exceptions would work then?
Something along the lines of:
fc = # path to feature class
fields = # list of fields
try:
with arcpy.da.UpdateCursor(fc, fields) as cur:
for row in cur:
# functional code
except RuntimeError as err:
if "cannot be updated outside an edit session" in err.message:
arcpy.AddMessage("No edit session exists")
else:
raise err
Perfect! Just what I was looking for. Something that actually works and answers the problem. There are a lot of false leads in this thread that I had already tried.
just start an edit session regardless its original state in the beginning of the script.