From 1327b07fb4edfbaa55cf678ec967054cb4ece634 Mon Sep 17 00:00:00 2001 From: BleuRaven Date: Mon, 22 Jan 2024 20:32:44 +0100 Subject: [PATCH] Next and new bfu_import_module for Unreal Engine --- .../bfu_import_module/__init__.py | 39 ++++++++++ .../asset_import.py} | 19 ++--- .../bfu_import_module/asset_import_script.py | 71 +++++++++++++++++++ .../sequencer_import.py} | 11 +-- .../sequencer_import_script.py | 71 +++++++++++++++++++ blender-for-unrealengine/bfu_write_text.py | 2 +- blender-for-unrealengine/bfu_write_utils.py | 2 +- 7 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 blender-for-unrealengine/bfu_import_module/__init__.py rename blender-for-unrealengine/{import_scripts/asset_import_script.py => bfu_import_module/asset_import.py} (98%) create mode 100644 blender-for-unrealengine/bfu_import_module/asset_import_script.py rename blender-for-unrealengine/{import_scripts/sequencer_import_script.py => bfu_import_module/sequencer_import.py} (98%) create mode 100644 blender-for-unrealengine/bfu_import_module/sequencer_import_script.py diff --git a/blender-for-unrealengine/bfu_import_module/__init__.py b/blender-for-unrealengine/bfu_import_module/__init__.py new file mode 100644 index 00000000..6e6e1994 --- /dev/null +++ b/blender-for-unrealengine/bfu_import_module/__init__.py @@ -0,0 +1,39 @@ +# ====================== BEGIN GPL LICENSE BLOCK ============================ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# All rights reserved. +# +# ======================= END GPL LICENSE BLOCK ============================= + +import importlib + +from . import asset_import +from . import sequencer_import + +if "asset_import" in locals(): + importlib.reload(asset_import) +if "sequencer_import" in locals(): + importlib.reload(sequencer_import) + + + +print("Import module loaded.") + +def run_asset_import(assets_data): + pass + asset_import.ImportAllAssets(assets_data) + +def run_sequencer_import(sequence_data): + pass + sequencer_import.CreateSequencer(sequence_data) \ No newline at end of file diff --git a/blender-for-unrealengine/import_scripts/asset_import_script.py b/blender-for-unrealengine/bfu_import_module/asset_import.py similarity index 98% rename from blender-for-unrealengine/import_scripts/asset_import_script.py rename to blender-for-unrealengine/bfu_import_module/asset_import.py index bc70389b..9469a76c 100644 --- a/blender-for-unrealengine/import_scripts/asset_import_script.py +++ b/blender-for-unrealengine/bfu_import_module/asset_import.py @@ -62,15 +62,15 @@ def is_unreal_version_greater_or_equal(target_major, target_minor=0, target_patc return False -def ImportAllAssets(): +def ImportAllAssets(assets_data): # Prepare process import json_data_file = 'ImportAssetData.json' dir_path = os.path.dirname(os.path.realpath(__file__)) - import_assets_data = JsonLoadFile(os.path.join(dir_path, json_data_file)) + assets_data - bfu_unreal_import_location = import_assets_data['bfu_unreal_import_location'] + bfu_unreal_import_location = assets_data['bfu_unreal_import_location'] ImportedList = [] ImportFailList = [] @@ -88,14 +88,14 @@ def ValidUnrealAssetsName(filename): def GetAssetByType(type): target_assets = [] - for asset in import_assets_data["assets"]: + for asset in assets_data["assets"]: if asset["asset_type"] == type: target_assets.append(asset) return target_assets def ImportAsset(asset_data): - counter = str(len(ImportedList)+1) + "/" + str(len(import_assets_data["assets"])) + counter = str(len(ImportedList)+1) + "/" + str(len(assets_data["assets"])) print("Import asset " + counter + ": ", asset_data["asset_name"]) if asset_data["asset_type"] == "StaticMesh" or asset_data["asset_type"] == "SkeletalMesh": @@ -502,7 +502,7 @@ def ImportSkeletalLod(lod_name, lod_number): # Process import print('========================= Import started ! =========================') - print(import_assets_data["assets"]) + print(assets_data["assets"]) # Import assets with a specific order @@ -552,10 +552,3 @@ def ImportSkeletalLod(lod_name, lod_number): else: return 'Assets imported with success !' - -print("Start importing assets.") - -if CheckTasks(): - print(ImportAllAssets()) - -print("Importing assets finished.") diff --git a/blender-for-unrealengine/bfu_import_module/asset_import_script.py b/blender-for-unrealengine/bfu_import_module/asset_import_script.py new file mode 100644 index 00000000..058827c7 --- /dev/null +++ b/blender-for-unrealengine/bfu_import_module/asset_import_script.py @@ -0,0 +1,71 @@ +# This script was generated with the addons Blender for UnrealEngine : https://github.com/xavier150/Blender-For-UnrealEngine-Addons +# It will import into Unreal Engine all the assets of type StaticMesh, SkeletalMesh, Animation and Pose +# The script must be used in Unreal Engine Editor with Python plugins : https://docs.unrealengine.com/en-US/Engine/Editor/ScriptingAndAutomation/Python +# Use this command in Unreal cmd consol: py "[ScriptLocation]\asset_import_script.py" + +import importlib +import sys +import os +import json + +def JsonLoad(json_file): + # Changed in Python 3.9: The keyword argument encoding has been removed. + if sys.version_info >= (3, 9): + return json.load(json_file) + else: + return json.load(json_file, encoding="utf8") + +def JsonLoadFile(json_file_path): + if sys.version_info[0] < 3: + with open(json_file_path, "r") as json_file: + return JsonLoad(json_file) + else: + with open(json_file_path, "r", encoding="utf8") as json_file: + return JsonLoad(json_file) + +def load_module(import_module_path): + # Import and run the module + module_name = os.path.basename(import_module_path).replace('.py', '') + module_dir = os.path.dirname(import_module_path) + + if module_dir not in sys.path: + sys.path.append(module_dir) + + imported_module = importlib.import_module(module_name) + + if "bfu_import_module" in locals(): + importlib.reload(imported_module) + + # Assuming the module has a main function to run + if hasattr(imported_module, 'main'): + imported_module.main() + + return imported_module, module_name + +def unload_module(module_name): + # Vérifier si le module est dans sys.modules + if module_name in sys.modules: + # Récupérer la référence du module + module = sys.modules[module_name] + # Supprimer la référence globale + del sys.modules[module_name] + del module + +def RunImportScriptWithJsonData(): + # Prepare process import + json_data_file = 'ImportAssetData.json' + dir_path = os.path.dirname(os.path.realpath(__file__)) + + import_assets_data = JsonLoadFile(os.path.join(dir_path, json_data_file)) + + import_module_path = import_assets_data["info"]["import_modiule_path"] # Module to run + print("Module path to import:", import_module_path) + + imported_module, module_name = load_module(import_module_path) + + unload_module(module_name) + imported_module.run_asset_import(import_assets_data) + +print("Start importing assets.") +RunImportScriptWithJsonData() +print("Importing assets finished.") diff --git a/blender-for-unrealengine/import_scripts/sequencer_import_script.py b/blender-for-unrealengine/bfu_import_module/sequencer_import.py similarity index 98% rename from blender-for-unrealengine/import_scripts/sequencer_import_script.py rename to blender-for-unrealengine/bfu_import_module/sequencer_import.py index 5334869e..77cb1133 100644 --- a/blender-for-unrealengine/import_scripts/sequencer_import_script.py +++ b/blender-for-unrealengine/bfu_import_module/sequencer_import.py @@ -62,14 +62,12 @@ def is_unreal_version_greater_or_equal(target_major, target_minor=0, target_patc else: return False -def CreateSequencer(): +def CreateSequencer(sequence_data): # Prepare process import json_data_file = 'ImportSequencerData.json' dir_path = os.path.dirname(os.path.realpath(__file__)) - sequence_data = JsonLoadFile(os.path.join(dir_path, json_data_file)) - spawnable_camera = sequence_data['spawnable_camera'] startFrame = sequence_data['startFrame'] endFrame = sequence_data['endFrame']+1 @@ -327,10 +325,3 @@ def AddSequencerSectionBoolKeysByIniFile(sequencer_section, track_dict): unreal.EditorAssetLibrary.sync_browser_to_objects([seq.get_path_name()]) return 'Sequencer created with success !' - -print("Start importing sequencer.") - -if CheckTasks(): - print(CreateSequencer()) - -print("Importing sequencer finished.") diff --git a/blender-for-unrealengine/bfu_import_module/sequencer_import_script.py b/blender-for-unrealengine/bfu_import_module/sequencer_import_script.py new file mode 100644 index 00000000..be7e5452 --- /dev/null +++ b/blender-for-unrealengine/bfu_import_module/sequencer_import_script.py @@ -0,0 +1,71 @@ +# This script was generated with the addons Blender for UnrealEngine : https://github.com/xavier150/Blender-For-UnrealEngine-Addons +# It will import into Unreal Engine all the assets of type StaticMesh, SkeletalMesh, Animation and Pose +# The script must be used in Unreal Engine Editor with Python plugins : https://docs.unrealengine.com/en-US/Engine/Editor/ScriptingAndAutomation/Python +# Use this command in Unreal cmd consol: py "[ScriptLocation]\sequencer_import_script.py" + +import importlib +import sys +import os +import json + +def JsonLoad(json_file): + # Changed in Python 3.9: The keyword argument encoding has been removed. + if sys.version_info >= (3, 9): + return json.load(json_file) + else: + return json.load(json_file, encoding="utf8") + +def JsonLoadFile(json_file_path): + if sys.version_info[0] < 3: + with open(json_file_path, "r") as json_file: + return JsonLoad(json_file) + else: + with open(json_file_path, "r", encoding="utf8") as json_file: + return JsonLoad(json_file) + +def load_module(import_module_path): + # Import and run the module + module_name = os.path.basename(import_module_path).replace('.py', '') + module_dir = os.path.dirname(import_module_path) + + if module_dir not in sys.path: + sys.path.append(module_dir) + + imported_module = importlib.import_module(module_name) + + if "bfu_import_module" in locals(): + importlib.reload(imported_module) + + # Assuming the module has a main function to run + if hasattr(imported_module, 'main'): + imported_module.main() + + return imported_module, module_name + +def unload_module(module_name): + # Vérifier si le module est dans sys.modules + if module_name in sys.modules: + # Récupérer la référence du module + module = sys.modules[module_name] + # Supprimer la référence globale + del sys.modules[module_name] + del module + +def RunImportScriptWithJsonData(): + # Prepare process import + json_data_file = 'ImportSequencerData.json' + dir_path = os.path.dirname(os.path.realpath(__file__)) + + sequence_data = JsonLoadFile(os.path.join(dir_path, json_data_file)) + + import_module_path = sequence_data["info"]["import_modiule_path"] # Module to run + print("Module path to import:", import_module_path) + + imported_module, module_name = load_module(import_module_path) + + unload_module(module_name) + imported_module.run_sequencer_import(sequence_data) + +print("Start importing assets.") +RunImportScriptWithJsonData() +print("Importing assets finished.") diff --git a/blender-for-unrealengine/bfu_write_text.py b/blender-for-unrealengine/bfu_write_text.py index 31499d8b..7383cc0f 100644 --- a/blender-for-unrealengine/bfu_write_text.py +++ b/blender-for-unrealengine/bfu_write_text.py @@ -213,7 +213,7 @@ def WriteAllTextFiles(): ExportSingleText(Text, scene.bfu_export_other_file_path, Filename) # Import script - bfu_path = os.path.join("addons", "blender-for-unrealengine", "import_scripts") + bfu_path = os.path.join("addons", "blender-for-unrealengine", "bfu_import_module") bfu_path_ref = os.path.join(bpy.utils.user_resource('SCRIPTS'), bfu_path) if scene.text_ImportAssetScript: diff --git a/blender-for-unrealengine/bfu_write_utils.py b/blender-for-unrealengine/bfu_write_utils.py index 018cad01..3e0a1f54 100644 --- a/blender-for-unrealengine/bfu_write_utils.py +++ b/blender-for-unrealengine/bfu_write_utils.py @@ -70,7 +70,7 @@ def add_generated_json_meta_data(json_data): blender_file_path = bpy.data.filepath version_str = 'Version '+ bbpl.blender_addon.addon_utils.get_addon_version_str("Blender for UnrealEngine") addon_path = bbpl.blender_addon.addon_utils.get_addon_path("Blender for UnrealEngine") - import_modiule_path = os.path.join(addon_path, "import_scripts") + import_modiule_path = os.path.join(addon_path, "bfu_import_module") json_data['info'] = {