Importing A Character into UE4 with Python
One aspect that most people immediately want to do, when they hear that UE4 has python exposure, is import assets. This isn’t the most straight forward, so I will walk you through a character import. Yes, all the code below imports a single skelmesh/skeleton.
#first we need to know where we're importing the character into, let's make a new path asset_path = '/Game/Characters/MyTest' unreal.EditorAssetLibrary.make_directory(asset_path) #now we make an import task and access it's options, which are similar to the import UI task = unreal.AssetImportTask() #now let's set some import options on the task class task.filename = PATH_TO_FBX_FILE task.destination_path = asset_path task.destination_name = 'my_asset' task.replace_existing = True task.automated = True #save the file when it is imported, that's right! task.save = True |
Now let’s instance an ‘options’ class on the task class and set some skeletal mesh specific options
task.options = unreal.FbxImportUI() task.options.import_as_skeletal = True task.options.override_full_name = True task.options.mesh_type_to_import = unreal.FBXImportType.FBXIT_SKELETAL_MESH task.options.skeletal_mesh_import_data.set_editor_property('update_skeleton_reference_pose', False) task.options.skeletal_mesh_import_data.set_editor_property('use_t0_as_ref_pose', True) task.options.skeletal_mesh_import_data.set_editor_property('preserve_smoothing_groups', 1) task.options.skeletal_mesh_import_data.set_editor_property('import_meshes_in_bone_hierarchy', False) task.options.skeletal_mesh_import_data.set_editor_property('import_morph_targets', True) task.options.skeletal_mesh_import_data.set_editor_property('threshold_position', 0.00002) task.options.skeletal_mesh_import_data.set_editor_property('threshold_tangent_normal', 0.00002) task.options.skeletal_mesh_import_data.set_editor_property('threshold_uv', 0.000977) task.options.create_physics_asset = False task.options.import_animations = False task.options.skeletal_mesh_import_data.set_editor_property('convert_scene', True) task.options.skeletal_mesh_import_data.set_editor_property('force_front_x_axis', False) task.options.skeletal_mesh_import_data.set_editor_property('convert_scene_unit', False) |
Now let’s set the normal/tangent import method, and the normal generation method. This a bit odd, you need to set it as an existing python object, let’s set it to normals and tangents, but there are also (unreal.FBXNormalImportMethod.FBXNIM_IMPORT_NORMALS, unreal.FBXNormalImportMethod.FBXNIM_COMPUTE_NORMALS)
normal_import_method = unreal.FBXNormalImportMethod.FBXNIM_IMPORT_NORMALS_AND_TANGENTS normal_generation_method = unreal.FBXNormalGenerationMethod.MIKK_T_SPACE task.options.skeletal_mesh_import_data.set_editor_property('normal_generation_method', normal_generation_method) task.options.skeletal_mesh_import_data.set_editor_property('normal_import_method', normal_import_method) |
The task class and its options look to be prepared, let’s now do the deed!
imported_asset = unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task]) #let's check what files were imported/created: imported_skelmesh = task.imported_object_paths |
Hey Chris,
Thanks for the info. This is all really exciting. Following up on this, is it possible to do this outside of the editor as well? Either by connecting an external Python session to a running editor or start a command line session from Python code?
Thanks,
Mathias
Comment by Mathias Royrvik — 2019/02/19 @ 12:26 PM
hey, thanks for all of this !!
if you get a crash after importing skmesh with python with this error
Fatal error: Path does not map to any roots.
be sure to have “/Game/” in your path (even it has been renamed)
asset_path = ‘/Game/Characters/MyTest’
Comment by Antonin Delboy — 2019/06/06 @ 5:45 AM