Race condition changing the content database

I’m using Flax Engine to visualise some data in a file. This is run entirely in the editor. When I load the file I delete all of my existing materials, to permit a reload of the file into a fresh environment:

string folder = "MuJoCoEd/Materials";

    private static void DeleteProjectContentsFiles(string folder)
    {
		Editor.Instance.ContentDatabase.Rebuild(true);
        ContentFolder materialsFolder = Editor.Instance.ContentDatabase.Find(Path.Combine(Globals.ProjectContentFolder, folder)) as ContentFolder;
        List<ContentItem> itemsToDelete = [];

        if (materialsFolder != null)
        {
            foreach (ContentItem item in materialsFolder.Children)
            {
                itemsToDelete.Add(item);
            }

            foreach (ContentItem item in itemsToDelete)
            {
                Editor.Instance.ContentDatabase.Delete(item);
            }
        }       
        else
        {
            Debug.LogWarning("Material deletion failed");
        }
        Editor.Instance.ContentDatabase.Rebuild(true);
    }

And I see an output log reflecting this:
[Info] Deleting asset FlaxEngine.Material, f820e8d74fe28897c503eb9f8a6757f3, D:\FlaxProjects/MuJoCoEd/Content/MuJoCoEd/Materials/black.flax…

However, when I attempt to create the material using:

string Pname = "black"

    private void CreateMaterial()
    {
        Editor.Instance.ContentDatabase.Rebuild(true);
        string localPath = "Content/MuJoCoEd/Materials/" + Pname + ".flax";
        if(Editor.CreateAsset("Material", localPath))
        {
            throw new Exception("Failed to create new asset.");
        }
    }

I get an exception thrown and I can see
[Info] Asset already exists. Using old ID: f820e8d74fe28897c503eb9f8a6757f3
[Warning] Cannot move imported file C:\Users/***/AppData/Local/Temp/678bf465-4be6-5c21-e238-4396df38f6ce/44158ab1491c91d9e19cd98382416c87.flax to the destination path Content/MuJoCoEd/Materials/black.flax

After the exception is thrown, I then see:
[Info] Deleting asset ‘D:\FlaxProjects/MuJoCoEd/Content/MuJoCoEd/Materials/black.flax’:f820e8d74fe28897c503eb9f8a6757f3.

So apparently the asset deletion has not been fully processed before the asset creation occurs, even though I force a content database update with Editor.Instance.ContentDatabase.Rebuild(true);

Any thoughts on this or a better strategy for cleaning out an asset directory and dynamically creating new material assets in it based on some parameters in an imported xml file would be much appreciated.

Edit: I guess this could just be a delay in the file system, if so, what would be a good wait until ready strategy?

I am pretty sure the localPath needs to actually be the full path. So something like Path.Combine(Globals.ProjectContentFolder, "MuJoCoEd", "Materials", $"{Pname}.flax");

Ah, I don’t think that is the problem because if I manually delete the materials within Flax Editor, then import the file, it works as expected and creates the new materials in the folder I specified. But if I leave the materials there and re-import, relying on the delete script, there is this problem where it appears to delete, then is unable to write to them, then completes the delete.

I’m experimenting with using CreateVirtualInstance(), but it makes testing more painful as I need to find something to assign it to and I can’t just go and poke around in the material editor.

Yeah it doesnt look like there is a good call back for when the asset is “completely” removed. You could always add an async task that delays a second or two before adding the new assets… not ideal, but it works.

1 Like

I’ll try some different approaches and see what happens. Thanks for your thoughts, I really appreciate the help you’ve provided recently.