enableTime() in ArcPy throws a RuntimeError. But if you manually go to the properties and enable the time filter in ArcGIS Pro, then disable it, enableTime() works.
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.listLayers()[0].enableTime()
It gives the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 992, in enableTime
return convertArcObjectToPythonObject(self._arc_object.enableTime(*gp_fixargs((startTimeField, endTimeField, autoCalculateTimeRange, timeDimension), True)))
RuntimeError
In the Contents pane, right click the layer that was added -> Properties -> Time -> Filter layer contents based on attribute values -> OK.
The map refreshes and a time slider is visible. Go to properties and disable the time filter again.
Back in the Python window, run
m.listLayers()[0].enableTime()
It successfully enables the time filter with no errors.
Relevant documentation: https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layertime-class.htm
I think this can't be a problem with the data I am using, because clearly it can filter by time when I enable it manually. Is there a step that I missed in ArcPy? Thanks
Solved! Go to Solution.
I stumbled on a solution when working on a different thing. I had to pass in the name of the datetime field to enableTime, for example:
layer.enableTime('datetime')
LayerTime—ArcGIS Pro | Documentation
example 1
I would think that
if lyr.supports('TIME'):
if lyr.isTimeEnabled:
would be good checks since you have to set time properties somewhere first
Running this
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.listLayers()[0].supports('TIME')
returns True, but
m.listLayers()[0].isTimeEnabled
returns False.
When I manually toggle it, isTimeEnabled is still False, but enableTime() works anyway. I think isTimeEnabled simply reflects the current state, as it becomes True after enableTime(). Assuming we need to make isTimeEnabled True before using enableTime, how would I do that? Where would I "set time properties somewhere" with code?
The layerTime Properties, for the most part, are read/write so presumably if you know what fields etc you intend to use, you could provide and set that information. I can't find any examples around in Community so you might have to experiment. Good luck
I stumbled on a solution when working on a different thing. I had to pass in the name of the datetime field to enableTime, for example:
layer.enableTime('datetime')
Thanks so much for this, saved me. What I will add is that this didn't work for me at first, but it was because my date field was not actually named what I thought it was. In the attribute table it was called "Date", but when I used python code to display the names of the field , I saw it was actually named "MSA_Date". Just thought I would add to check your field is exactly correct or this fix won't work.
I ran into this error today. While the solution posted above fixed the error, the documentation for layer.enableTime() lists all fields as optional. It seems to me that if the method doesn't work without passing the time field parameter, it should not be listed as optional in the documentation.
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm