I realize this question was closed long ago, but I have some old tools that this was newly a problem for and the SendKeys solution no longer seems to work, so I rolled my own solution after experimenting. It doesn't disable drawing, but creates the performance equivalent of that by disabling layers and reenabling them when done. Having the script run in the background doesn't resolve the problem (though I thought it would), so I tried turning off all of the layers - and it worked! Full speedup to the equivalent code in an empty document. So here's some code to accomplish that.
These two functions, when combined, turn off all the layers in a document, returning the saved state of the layers. Then, when your operations are done, you can turn them back on by providing that saved state to the second function. Recommended usage:
try:
layer_state = turn_off_all_layers("CURRENT")
# Do interesting things here
finally: # put it in a finally block so that if your interesting code fails, your layers still get reenabled
turn_on_layers("CURRENT", layer_state)
And the functions are below - corrections, comments, etc welcome - pretty new code so it may have some bugs, but it's been tested some.
def turn_off_all_layers(document="CURRENT"):
"""
A speedup function for map generation in ArcMap - turns off all layers so that it doesn't try to rerender them while we're using tools (since these tools need
to run in the foreground and background processesing didn't seem to speed it up).
Creates a dictionary keyed on the arcpy layer value longName which contains True or False values for whether or not the layers were enabled before running this.
Allows us to then use turn_on_layers on the same document to reenable those layers
:param document: a map document. defaults to "CURRENT"
:return: dict: a dictionary keyed on layer longName values with True or False values for whether the layer was enabled.
"""
visiblity = {}
doc = arcpy.mapping.MapDocument(document)
for lyr in arcpy.mapping.ListLayers(doc):
if lyr.visible is True:
try:
visiblity[lyr.longName] = True
lyr.visible = False
except NameError:
visiblity[lyr.longName] = False # if we have trouble setting it, then let's not mess with it later
else:
visiblity[lyr.longName] = False
return visiblity
def turn_on_layers(document="CURRENT", storage_dict=None, only_change_visible=True):
if not storage_dict:
raise ValueError("storage_dict must be defined and set to a list of layer names with values of False or True based on whether the layer should be on or off")
doc = arcpy.mapping.MapDocument(document)
for lyr in arcpy.mapping.ListLayers(doc):
if lyr.longName in storage_dict:
if not only_change_visible or (only_change_visible is True and storage_dict[lyr.longName] is True): # if we're only supposed to set the ones we want to make visible and it is one, or if we want to set all
try:
lyr.visible = storage_dict[lyr.longName] # set the visibility back to what we cached
except NameError:
arcpy.AddWarning("Couldn't turn layer %s back on - you may need to turn it on manually" % lyr.longName) # we couldn't turn a layer back on... too bad