UE4资产的导入需要首先创建一个AssetImportTask,然后再执行这个Task来达到导入资产的功能
import unreal
def creatImportTask(filename, destination_path, options=None):
"""
:param filename: 导入的文件的路径 eg: 'F:/workPlace/Scripts/MyTexture.TGA'
:param destination_path: 导出后置产要放在什么位置 eg: '/GAME/Texture'
:param options: 导入置产属性, 静态属性可由函数build_static_mesh_import_options获得,
骨骼属性可由build_skeletal_mesh_import_options获得
:return: Task 返回一个导入任务
"""
importtask = unreal.AssetImportTask()
importtask.set_editor_property("automated", True)
import_task.set_editor_property('destination_name', '')
import_task.set_editor_property('destination_path', destination_path)
import_task.set_editor_property('filename', filename)
import_task.set_editor_property('replace_existing', True) # 覆盖现有文件
import_task.set_editor_property('options', options) # 覆盖现有文件
import_task.set_editor_property('save', True)
return import_task
里面的option是导入所需要的参数, 在ue4中就是导入的选项
def build_static_mesh_import_options():
"""
构建导入静态网格选项
:return: options 导入静态网格选项
"""
options = unreal.FbxImportUI()
options.set_editor_property('import_mesh', True)
options.set_editor_property('import_texture', False)
options.set_editor_property('import_materials', True)
options.set_editor_property('import_as_skeletal', False) # 是否当作骨骼物体来导入
options.static_mesh_import_data.set_editor_property('import_translation', unreal.Vector(0.0, 0.0, 0.0))
options.static_mesh_import_data.set_editor_property('import_rotation', unreal.Rotator(0, 0, 0))
options.static_mesh_import_data.set_editor_property('import_uniform_scale', 1.0)
options.static_mesh_import_data.set_editor_property('combine_meshes', True)
options.static_mesh_import_data.set_editor_property('generate_lightmap_u_vs', True)
options.static_mesh_import_data.set_editor_property('auto_generate_collision', True)
return options
具体的属性可以参考:
https://docs.unrealengine.com/en-US/PythonAPI/class/FbxImportUI.html?highlight=fbximportui
有了任务之后,再实例一个资产工具(unreal.AssetToolsHelpers.get_asset_tools)来直接执行导入任务
def execute_import_tasks(tasks):
"""
执行导入任务
:param tasks: array 任务池
:return: True
"""
asset_tools = unreal.AssetToolsHelpers.get_asset_tools() # 创建一个资产工具
asset_tools..import_asset_tasks(tasks) # 导入资产
最后执行代码
import_files = "E:/Project/UE4_Learning_Project/UEPy_Learn/Assets/wall.fbx"
destination_path = "/GAME/Mesh"
options = build_static_mesh_import_options()
import_task = creat_import_task(import_files , destination_path , options )
execute_import_tasks(import_task )
!: unreal.AssetTools类在线文档:
https://docs.unrealengine.com/en-US/PythonAPI/class/AssetTools.html#unreal.AssetTools
unreal.AssetImportTask类在线文档:
https://docs.unrealengine.com/en-US/PythonAPI/class/AssetImportTask.html?highlight=assetimporttask