Component script not running after restarting simulation

Hello,

I have a question about restarting the simulation from a component’s script.

In my application, I use a component’s behavior to detect when a specified simulation duration is reached. When this happens, I want to restart the simulation from the component’s script.

I tried doing this with the code below.

The simulation restarts, but it seems that the component behavior I used to restart the simulation is not running in the new simulation.

By tracing the execution, it appears that the script gets stuck at the instruction directly after app.startSimulation() (in my case, it gets stuck at the beginning of the while loop).

Do you have any idea what might be causing this issue?

Thank you.

from vcScript import *
import os
import datetime

# Gobal variables
global simInitTime
global timeReached

app = getApplication()
comp = getComponent()

def OnStart():
    global simInitTime

    print('On start Video Recorder')
    
    # Initialize global variables
    simInitTime = app.Simulation.SimTime
        
def OnRun():
    global simInitTime

    delay(2)

    print("On run video Recorder")

    # Initialize recorder
    app.Recorder = VC_RECORDER_VIDEO
    exportPath = comp.getProperty("ExportPath").Value
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    app.RecordingFileName = "{}/video_{}.avi".format(exportPath, timestamp)
    app.RecordingHeight = 1080
    app.RecordingWidth = 1920
    app.RecordingFrameRate = 25
    app.RecordingShow = False

    # Get relevant properties
    exportVideo = comp.getProperty("ExportVideo").Value
    videoDuration = comp.getProperty("VideoDuration").Value
    simDuration = comp.getProperty("SimDuration").Value
    isRecording = comp.getProperty("Recording").Value

    while True:

        delay(0.5)

        isRecording = comp.getProperty("Recording").Value

        simTime = app.Simulation.SimTime - simInitTime

        if exportVideo: 

            # Start recording
            if simTime > (simDuration*60-videoDuration) and not(isRecording):
                print('Recording')
                app.Recording = True
                isRecording = True

            # Stop recording
            if isRecording and simTime > simDuration*60:
                app.Recording = False
                isRecording = False
                print('Stop recording')

        # Stop simulation
        if simTime > simDuration*60:
            print("Reached Specified Duration of Simulation")
            app.stopSimulation()
            app.resetSimulation()
            app.startSimulation()
            
            
            # comp.TimeReached = True
            # app.Simulation.halt()


def createViewAnimation():

    exportVideo = comp.getProperty("ExportVideo").Value
    videoDuration = comp.getProperty("VideoDuration").Value
    simDuration = comp.getProperty("SimDuration").Value

    videoStartTime = simDuration*60 - videoDuration

    if exportVideo:

        # Get the view animator
        item = app.findLayoutItem("ViewAnimation")
        if not item:
            item = app.createLayoutItem("ViewAnimation")

        # Get the program executed by animator and clear its Main routine
        prog = item.Program
        routine = prog.MainRoutine
        routine.clear()

        # Add view statements
        # The view must exist in layout
        # Otherwise you will need to create it using either View Editor in GUI or API
        # Get vcApplication.Views to handle a list of user views

        # Define the delay before switching to next view
        vs = routine.addStatement(VC_STATEMENT_DELAY)
        vs.Delay = videoStartTime
        # Define the view
        vs = routine.addStatement(VC_STATEMENT_VIEW)
        vs.Delay = 0.0
        vs.View = "View1"

    else:

        # Get the view animator
        item = app.findLayoutItem("ViewAnimation")
        if not item:
            return
         
        # Get the program executed by animator and clear its Main routine
        prog = item.Program
        routine = prog.MainRoutine
        routine.clear()
        app.deleteLayoutItem("ViewAnimation")

createViewAnimation()

I think stopping the simulation is not necessarily a synchronous operation. You could try adding event handlers for simulation stop and reset, and then try to do your restart from there.

See the events in script here:
https://help.visualcomponents.com/4.9/Premium/en/Python_API/vcScript.htm

Thank you for your answer.

Unfortunately I still have the same problem when I restart the simulation using OnStop or OnReset events.