6

I have a QGIS Project with Temporal Controller, in Python Console. How could I capture the current moment of the simulation?

enter image description here


I'm trying, but doesn't work

canvas = iface.mapCanvas()
temporal= canvas.temporalController().currentFrameNumber ()
0
5

You can use the following considering you have QGIS 3.14+

from qgis.PyQt import sip

canvas = iface.mapCanvas()
temporalController = canvas.temporalController()
# Cast the QgsTemporalController to QgsTemporalNavigationObject or stucked 
temporalNavigationObject = sip.cast(temporalController, QgsTemporalNavigationObject)


currentFrameNumber = temporalNavigationObject.currentFrameNumber()
print('Frame number', currentFrameNumber)
currentDateTimeRange = temporalNavigationObject.dateTimeRangeForFrameNumber(currentFrameNumber)
print('Current start time range', print(currentDateTimeRange.begin()))
print('Current end time range', print(currentDateTimeRange.end()))

# Experiment section as a reminder (because seems a bug in the signal connect later)
# def animation_state_changed(state):
#     print("DEBUG")

# Signal does not work here when above function + below statement uncommented
# temporalNavigationObject.stateChanged.connect(animation_state_changed)
1
  • Did you manage to get the signal to work?
    – Joonala
    Nov 11 '20 at 16:49
3

I'm happy to report that this works as desired for me without a sip cast (currently testing on 3.18).

I think the desired signal is the updateTemporalRange, not the stateChanged signal as mentioned in @ThomasG77's answer (which still fails to connect also in my case).

This prints the current frame number in the QGIS Python console:

canvas = iface.mapCanvas()
controller = canvas.temporalController()

def f():
    print(controller.currentFrameNumber())
    
controller.updateTemporalRange.connect(f)

Via the frame number, the .dateTimeRangeForFrameNumber can be used to get the datetimes as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.