Ensure condition failed: Importing a Maya FBX Mesh MTAccessDetector.h at Line: 355

Hello!

I’m running into a data race issue with a Python script in a blank project.
Using Unreal Engine 5.4.1 with Py3.11 (I also tried in 5.3.1 and Py3.9x) .

The script basically does two things:

  1. After importing an asset, it checks a metadata property (FBX.mesh_type).
  2. Then, it moves the asset to a specific directory based on the value of this metadata property.

(EditorAssetLibrary.rename_asset)

The rule works and only triggers the data race error the first time I import an FBX. If I import another one, there’s no error. But if I restart the engine, the error pops up again on the first import.

Even though I’ve added locks (threading.Lock) to sync access to shared resources, I’m still seeing concurrency errors. Could really use some help to figure this out!

The Error

The logs show a data race and mention MTAccessDetector.h with this message:

LogOutputDevice: Error: === Handled ensure: ===
LogOutputDevice: Error: Ensure condition failed: InState.GetWriterThreadId() == CurrentThreadId  [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Misc\MTAccessDetector.h] [Line: 355] 
LogOutputDevice: Error: Data race detected! Acquiring read access on thread 14844 concurrently with 1 writers on thread 31536:
LogOutputDevice: Error: Current thread 14844 callstack:
LogOutputDevice: Error: [Callstack] 0x0000029760d9d167 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []

Here is the script I’m using:

import unreal
from threading import Lock
import sys
from ImporterRules import importer_rules_manager, Rule
from ImporterRules.Actions import ImportActionBase
from ImporterRules.Queries import QueryBase

mesh_type_to_directory_mapping = {
    1: "Enviro",
    2: "Weapons",
    3: "Deco"
}

lock = Lock()

class ContainsMeshTypeProperty(QueryBase):
    def test(self, factory: unreal.Factory, created_object: unreal.Object) -> bool:
        with lock:
            unreal.log("[ContainsMeshTypeProperty] Acquired lock")
            EditorAssetSubsystem = unreal.get_editor_subsystem(unreal.EditorAssetSubsystem)
            if not EditorAssetSubsystem:
                unreal.log("[ContainsMeshTypeProperty] EditorAssetSubsystem not found")
                return False

            unreal.log("[ContainsMeshTypeProperty] Importing FBX with Python VER")
            unreal.log(sys.version)
            value = EditorAssetSubsystem.get_metadata_tag(created_object, unreal.Name("FBX.mesh_type"))
            unreal.log(f"[ContainsMeshTypeProperty] Retrieved metadata tag value: {value}")

            try:
                value_int = int(value)
                unreal.log(f"[ContainsMeshTypeProperty] Converted value to int: {value_int}")
            except ValueError:
                unreal.log(f"[ContainsMeshTypeProperty] ERROR '{value}' is not a valid integer FBX.mesh_type")
                return False

            if value_int not in mesh_type_to_directory_mapping:
                unreal.log(f"[ContainsMeshTypeProperty] ERROR '{value_int}' is not a supported FBX.mesh_type")
                return False

            unreal.log("[ContainsMeshTypeProperty] Test passed")
            return True

class MoveMeshBasedOnType(ImportActionBase):
    def apply(self, factory: unreal.Factory, created_object: unreal.Object) -> bool:
        with lock:
            unreal.log("[MoveMeshBasedOnType] Acquired lock")
            unreal.log("MoveMeshBasedOnType")
            if created_object is None:
                unreal.log("[MoveMeshBasedOnType] Created object is None")
                return False

            EditorAssetSubsystem = unreal.get_editor_subsystem(unreal.EditorAssetSubsystem)
            EditorAssetLibrary = unreal.EditorAssetLibrary()
            unreal.log("[MoveMeshBasedOnType] Retrieved EditorAssetSubsystem and EditorAssetLibrary")

            value = EditorAssetSubsystem.get_metadata_tag(created_object, unreal.Name("FBX.mesh_type"))
            unreal.log(f"[MoveMeshBasedOnType] Retrieved metadata tag value: {value}")

            try:
                value_int = int(value)
                unreal.log(f"[MoveMeshBasedOnType] Converted value to int: {value_int}")
            except ValueError:
                unreal.log(f"[MoveMeshBasedOnType] ERROR '{value}' is not a valid integer FBX.mesh_type")
                return False

            if value_int not in mesh_type_to_directory_mapping:
                unreal.log(f"[MoveMeshBasedOnType] ERROR '{value_int}' is not a supported FBX.mesh_type")
                return False

            destination_path = f"/Game/{mesh_type_to_directory_mapping.get(value_int)}/{created_object.get_fname()}"
            unreal.log(f"[MoveMeshBasedOnType] Renaming asset to: {destination_path}")
            EditorAssetLibrary.rename_asset(created_object.get_path_name(), destination_path)
            unreal.log("[MoveMeshBasedOnType] Asset renamed successfully")
            return True

importer_rules_manager.register_rules(
    class_type=unreal.StaticMesh,
    rules=[
        Rule(
            queries=[
                ContainsMeshTypeProperty(),
            ],
            actions=[
                MoveMeshBasedOnType(),
            ]
        )
    ]
)

Any suggestions on what might be causing this issue or how to fix it? Has anyone experienced something similar?

Thank you!

Same error using a diferrent approach:

unreal.AssetToolsHelpers.get_asset_tools().rename_assets([unreal.AssetRenameData(imported_mesh, destination_path, asset_name)])

ogOutputDevice: Error: === Handled ensure: ===
LogOutputDevice: Error: Ensure condition failed: InState.GetWriterThreadId() == CurrentThreadId  [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Misc\MTAccessDetector.h] [Line: 355] 
LogOutputDevice: Error: Data race detected! Acquiring read access on thread 10872 concurrently with 1 writers on thread 26996:
LogOutputDevice: Error: Current thread 10872 callstack:
LogOutputDevice: Error: [Callstack] 0x000001f1219cd167 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121afb404 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219b4572 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219ccbbc UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c0aed UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c148c UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a89f93 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c00d2 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4dd3919 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd526dbe6 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd524817f UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4eced34 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4ed9a41 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6c2485 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6c21bf UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6ea115 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6b2390 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce8b6b13 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdced1b7ad UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdced0f127 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3d09257d KERNEL32.DLL!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3ee4aa48 ntdll.dll!UnknownFunction []
LogOutputDevice: Error: Writer thread 26996 callstack:
LogOutputDevice: Error: [Callstack] 0x00007ffe3ee8f9d4 ntdll.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3c26427e KERNELBASE.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdced773a1 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4ebbade UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd5212405 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd525c50d UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd525c35d UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc87ee421 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc87d0487 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc14bbb4b UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc147c737 UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc14d3159 UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc14f1fa7 UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4f004c2 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd5269■■■ UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a47082 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a91480 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a901d6 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a92f85 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c4e1e UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdb7671263 python311.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdb76748df python311.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdb76760b2 python311.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdb76a6f94 python311.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdb76f0773 python311.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219d7cf4 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a2238b UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4f004c2 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd5269■■■ UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc792b9a3 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc795d818 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc82fffee UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc831ad61 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc1497cd3 UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc1493500 UnrealEditor-AssetTools.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc8c6ec3f UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc8c51920 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc8c410dd UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdcc280cd7 UnrealEditor-Engine.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdcc317863 UnrealEditor-Engine.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc810b753 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdc8d8f596 UnrealEditor-UnrealEd.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff650998e0b UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff6509bf74c UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff6509bf83a UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff6509c2cb4 UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff6509d86b4 UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ff6509db95a UnrealEditor.exe!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3d09257d KERNEL32.DLL!UnknownFunction []
LogOutputDevice: Error: Stack: 
LogOutputDevice: Error: [Callstack] 0x000001f121afb45b UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219b4572 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219ccbbc UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c0aed UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c148c UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f121a89f93 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x000001f1219c00d2 UnrealEditor-PythonScriptPlugin.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4dd3919 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd526dbe6 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd524817f UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4eced34 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdd4ed9a41 UnrealEditor-CoreUObject.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6c2485 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6c21bf UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6ea115 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce6b2390 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdce8b6b13 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdced1b7ad UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffdced0f127 UnrealEditor-Core.dll!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3d09257d KERNEL32.DLL!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffe3ee4aa48 ntdll.dll!UnknownFunction []
LogStats:                SubmitErrorReport -  0.000 s
LogStats:                    SendNewReport -  0.772 s
LogStats:             FDebug::EnsureFailed -  0.792 s

I found a workaround. It’s a bit messy, but no date-race.

Using duplicate_asset instead of rename_asset

EditorAssetLibrary.duplicate_asset(asset_path, destination_path)

Then iterate over the metadata tags to copy them to the duplicated asset.

However, when I try to delete the original asset, the editor crashes, but its something.