Using Python in Unreal to import static meshes
In any pipeline it is important to have consistent import settings in a project. An import tool allows content creators to not worry to much about importing assets and allows them to focus on art. In this article I will walk you through the steps of using python to import static meshes into Unreal. I am starting with static meshes because I wanted to automate enabling nanite for meshes to utilize that feature. These ideas can be expanded to additional import types by updating the different import options.
The bulk of using python to import assets in unreal is defining the import tasks. Import tasks defines the settings and options used to import assets. Any option that is used in the import dialog can be defined in an import task.
This is the code to build an import task. I will walk you through it step by step. I defined it in a function for reusability.
def build_import_tasks(filename: str, destination_name: str, destination_path: str, options):
tasks = []
task = unreal.AssetImportTask()
task.set_editor_property(‘automated’, True)
task.set_editor_property(“destination_name”, destination_name)
task.set_editor_property(“destination_path”, destination_path)
task.set_editor_property(“filename”, filename)
task.set_editor_property(‘options’, options)
tasks.append(task)
return tasks
The function that executes these tasks requires a list. So I created a list that I will append the task to.
tasks = []
To create an import task you need to use the unreal.AssetImportTask() to build tasks.
task = unreal.AssetImportTask()
Then use the set_editor_property() to set the options in the task.
The basic settings that are needed for a basic import are automated, destination_name, destination_path, and filename:
- Setting the automated setting to true. Imports the task without opening unreal’s importer dialog.
- The destination_path is the folder in the editor in where the asset will be imported to.
- The filename is the name of the asset that is being imported.
The options property sets the options in the import UI. And this is used for
def build_import_options(static_mesh_data):
options = unreal.FbxImportUI()
options.set_editor_property(‘import_mesh’, True)
options.set_editor_property(‘import_textures’, False)
options.set_editor_property(‘import_materials’, False)
options.set_editor_property(‘import_as_skeletal’, False)
options.set_editor_property(“static_mesh_import_data”, static_mesh_data)
return options
The static_mesh_import_data property is used to set up options specifically for static meshes. Such as enabling nanite support.
def build_static_mesh_data():
static_mesh_data = unreal.FbxStaticMeshImportData()
static_mesh_data.set_editor_property(“build_nanite”, False)
static_mesh_data.set_editor_property(“auto_generate_collision”, False)
return static_mesh_data
Then to use the import tasksyou need to create an assettoolshelper class and call the get_asset_tools() function.adef import_static_mesh(tasks):
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
asset_tools.import_asset_tasks(tasks)
Then you put all the functions together and run it in the script in the editor.
def execute_import_static_mesh(game_path):
mesh_data = build_static_mesh_data()
mesh_options = build_import_options(mesh_data)
import_tasks = build_import_tasks(game_path, “name”, “/Game/ImportFolder”, mesh_options)
import_static_mesh(import_tasks)
execute_import_static_mesh(r”C:\Users\deonw\OneDrive\Desktop\Import_Static_Mesh_Test.FBX”)