Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"ImportError: No module named util" #210

Open
RCBurrito opened this issue Jan 11, 2025 · 2 comments
Open

"ImportError: No module named util" #210

RCBurrito opened this issue Jan 11, 2025 · 2 comments
Labels
R Major Annoying but non-blocking issue or bug.

Comments

@RCBurrito
Copy link

RCBurrito commented Jan 11, 2025

I've been having this issue and I'm unsure on how to resolve it. I'm not the most experienced with python, so I'm sure this issue is pretty simple.

I'm trying to import a camera from blender to UE4.
I get this error when copying and pasting the import script from blender to UE4:

LogPython: Error: Traceback (most recent call last):
LogPython: Error: File "C:/Users/xxx/Downloads/ExportedFbx/ImportSequencerScript.py", line 8, in
LogPython: Error: import importlib.util
LogPython: Error: ImportError: No module named util

Not sure what information is needed but:

python version 3.13.1
UE4 version 4.25.4
Blender version 3.1.0
Blender Plugin version: "unrealengine_assets_exporter_4.3.10-blender_4.2"

@xavier150
Copy link
Owner

Thanks for the report, I will check this issue soon.

@xavier150 xavier150 added the R Major Annoying but non-blocking issue or bug. label Jan 12, 2025
@xavier150
Copy link
Owner

Hey @RCBurrito thanks for the report sadly I don't know how fix that issue in Unreal Engine 4.25

Unreal Engine use Python 2.7.x and my addon use importlib.util included in Python 3.1

Unreal Engine Version Python Version
4.25 Python 2.7.x
4.26 - 4.27 Python 3.7.x
5.0 - 5.2 Python 3.9.x
5.4 - 5.5 Python 3.11.x

The import script of my addon load and run bfu_import_module using importlib.util.

I read it possible to have the same result using imp but the loaded module don't import all sub modules in the file __init__.py like bpl.

LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "P:/GitHubBlenderAddon/Blender-For-UnrealEngine-Addons/docs/Examples/ExportedFbx/ImportAssetScript.py", line 61, in <module>
LogPython: Error:     RunImportScriptWithJsonData()
LogPython: Error:   File "P:/GitHubBlenderAddon/Blender-For-UnrealEngine-Addons/docs/Examples/ExportedFbx/ImportAssetScript.py", line 56, in RunImportScriptWithJsonData
LogPython: Error:     module.run_from_asset_import_script(import_file_path)
LogPython: Error:   File "C:\Users\BleuRaven\AppData\Roaming\Blender Foundation\Blender\4.3\extensions\user_default\unrealengine_assets_exporter\run_unreal_import_script.py", line 61, in run_from_asset_import_script
LogPython: Error:     module = import_unreal_module()
LogPython: Error:   File "C:\Users\BleuRaven\AppData\Roaming\Blender Foundation\Blender\4.3\extensions\user_default\unrealengine_assets_exporter\run_unreal_import_script.py", line 50, in import_unreal_module
LogPython: Error:     module = imp.load_module(module_name, file, module_file, ('.py', 'r', imp.PY_SOURCE))
LogPython: Error:   File "C:\Users\BleuRaven\AppData\Roaming\Blender Foundation\Blender\4.3\extensions\user_default\unrealengine_assets_exporter\bfu_import_module\__init__.py", line 21, in <module>
LogPython: Error:     from . import bpl
LogPython: Error: ValueError: Attempted relative import in non-package

Here are my tests if anyone has any ideas.

asset_import_script.py

def RunImportScriptWithJsonData():
    # Path to the JSON data file
    json_data_file = 'ImportAssetData.json'
    dir_path = os.path.dirname(os.path.realpath(__file__))
    import_file_path = os.path.join(dir_path, json_data_file)
    
    # Load JSON data
    assets_data = JsonLoadFile(import_file_path)
    file_path = os.path.join(assets_data["info"]["addon_path"],'run_unreal_import_script.py')

    if sys.version_info >= (3, 1):
        import importlib.util
        spec = importlib.util.spec_from_file_location("__import_assets__", file_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
    else:
        # Dynamically load the script using imp
        import imp
        module_name = "__import_assets__"
        with open(file_path, 'r') as file:
            module = imp.load_module(module_name, file, file_path, ('.py', 'r', imp.PY_SOURCE))
        
    # Run script module function
    module.run_from_asset_import_script(import_file_path)

run_unreal_import_script.py

def import_unreal_module():
    # Get the script directory
    script_dir = os.path.dirname(os.path.abspath(__file__))
    module_name = "bfu_import_module"
    module_file = os.path.join(script_dir, module_name, "__init__.py")

    if sys.version_info >= (3, 1):
        import importlib.util
        spec = importlib.util.spec_from_file_location(module_name, module_file)
        module = importlib.util.module_from_spec(spec)
        sys.modules[module_name] = module
        spec.loader.exec_module(module)
    else:
        # Dynamically load the script using imp
        import imp
        with open(module_file, 'r') as file:
            module = imp.load_module(module_name, file, module_file, ('.py', 'r', imp.PY_SOURCE))
        main_module.__name__ = original_name

    # Return the imported module
    return module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R Major Annoying but non-blocking issue or bug.
Projects
None yet
Development

No branches or pull requests

2 participants