Accessing the timestep in Python script for overlay creation

When using a Python script to create a layer in a viewport, I would like to access the [Timestep] variable that is available in the “regular” modifiers. If I understand the documentation correctly I can, however, only access the current frame index (via the frame attribute). When displaying the time I hence I have to multiply by the dump interval (which might vary from run to run). Am I missing something or could this information be added easily?

Hi!

From a Python viewport layer function ("render()") you do have access to global attributes such as the [Timestep] attributes you’ve mentioned. Keep in mind though that global attributes are produced by a data pipeline, and there may be more than one pipeline in the current scene.

Unlike a Python modifier function ("modify()"), which is part of a data pipeline and has direct access to its data, a Python viewport layer function is not executed in the context of a particular pipeline. But the function may access all pipelines in the current scene and request their output data if needed.

The following example function shows how to evaluate a pipeline from the current scene (which means asking it for its output DataCollection at a specific animation time) and then access a global attribute from that data collection:

from ovito.vis import *

def render(args: PythonViewportOverlay.Arguments):
    
    # Get the active pipeline (the one currently open in the pipeline editor):
    pipeline = args.scene.selected_pipeline
    if not pipeline: return
    
    # Evaluate the pipeline at the current animation time (the frame we are rendering):
    data = pipeline.compute(args.frame)
    
    # The obtained DataCollection contains the global attribute:
    timestep = data.attributes['Timestep']
    args.painter.drawText(10, 10 + args.painter.fontMetrics().ascent(), "Timestep: {}".format(timestep))

Thanks. That worked like a charm.