Skip to content

Commit

Permalink
Next and new bfu_import_module for Unreal Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier150 committed Jan 22, 2024
1 parent 4f6b4b9 commit 1327b07
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 25 deletions.
39 changes: 39 additions & 0 deletions blender-for-unrealengine/bfu_import_module/__init__.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
# 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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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":
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.")
71 changes: 71 additions & 0 deletions blender-for-unrealengine/bfu_import_module/asset_import_script.py
Original file line number Diff line number Diff line change
@@ -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.")
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 1 addition & 1 deletion blender-for-unrealengine/bfu_write_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion blender-for-unrealengine/bfu_write_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down

0 comments on commit 1327b07

Please sign in to comment.