Script Module Editor kills scripts added through module hook

When exposing python scripts to a project through module’s hook like this:

    public static void exposeJythonBindings(ClientContext context) throws IOException
    {
        ScriptManager scriptMan = context.getScriptManager();
        String moduleCode = "# some python script here, I load each from a set of files bundled within the module's .jar";
            
        scriptMan.addScriptModule(moduleName, moduleCode);
    }

The problem is, every time I save changes in Script Module Editor of the Designer (editing scripts not exposed
with the above mechanism), the above python scripts disappear and stop working. Only way to resume
is by restarting the Designer.

Could you fix this?

Thanks!

This isn’t the correct way to expose scripts.

What you want to do override #initializeScriptManager(ScriptManager) in the appropriate module hooks and add your script module every time this is called. The lifecycle of a ScriptManager is sometimes short-lived. Saving changes to your project is one way to restart the lifecycle, killing off the ScriptManager you added your scripts to and creating a new one.

Never hang on to the ScriptManager parameter of #initializeScriptManager(ScriptManager), just remember to add your scripts every time it gets called.

My designer hook’s #initializeScriptManager(ScriptManager) only executes once at startup, but
not after project save like you suggested. The automatically added scripts still get dropped.

What am I missing?

My conclusion is it’s broken, but I found out an alternative solution:

In designer hook:

    @Override
    public void notifyProjectSaveDone()
    {
        super.notifyProjectSaveDone();
        
        // reload scripts. the "initializeScriptManager" doesn't seem to work as defined
        ScriptManager scriptMan = AClassWhereIStoredTheContextObjectsAtStartup
                .getClientContext().getScriptManager();
        exposeJythonScripts(scriptMan);
    }

What path are you adding them under? i.e. the 1st String argument to ScriptManager.addScriptModule

The scripts go to paths for example:

app.psc.project.SomeClass1.py
app.psc.project.SomeClass2.py

notifyProjectSaveDone() doesn’t work on the client, but addProjectChangeListener() can be used instead.