Importing assets to Unreal Engine

Published on

How to import various kind of assets to Unreal Engine with C++?

Platforma

Importing Assets with C++ to Unreal Engine

Importing assets using UBlueprintFunctionLibrary class works in the editor mode only. While we use it in the editor, we need to extend project's build settings through .Build.cs file for 2 additional modules, see below.

if (Target.bBuildEditor == true)
{
  PrivateDependencyModuleNames.AddRange(
    new string[]
		{
      <mark>"AssetTools"</mark>,
      <mark>"UnrealEd"</mark>,
    }
  );
};

Usually we will use 2 following custom help functions for importing - CreateImportTask and ProcessImportTask. They may be defined within the same UExampleClass or in a separated class we will ink to.

  • CreateImportTask custom help function

    /**
     * Editor Only - Will not work in packaged build.
     * 
     * <mark>Create an import task that you can then process to import any kind of assets</mark>
     *
     * @param SourcePath		The path of the source file: "C:/Temp/MyTexture.png"
     * @param DestinationPath	The path of the imported asset: "/Game/Folder/MyTexture"
     * @param ExtraFactory		Optional. Some asset types require a special factory to import them
     * @param ExtraOptions		Optional. Some asset types have some extra settings you can set
     * @param bOutSuccess		If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The import task
     */
    UFUNCTION(BlueprintCallable, Category = "Import Basic Assets")
    <strong>static class <mark>UAssetImportTask* CreateImportTask</mark>(FString SourcePath, FString DestinationPath, UFactory* ExtraFactory, UObject* ExtraOptions, bool& bOutSuccess, FString& OutInfoMsg);</strong>

    In the configuration above, imported assets are not saved automatically due to Task->bSave = false setup.

  • ProcessImportTask custom help function

    /**
     * Editor Only - Will not work in packaged build.
     *
     * <mark>Process the import task to import the assets</mark>
     *
     * @param ImportTask		The task you want to process
     * @param bOutSuccess		If the action was a success or not
     * @param OutInfoMsg	More information about the action's result
     *
     * @return The imported asset
     */
    UFUNCTION(BlueprintCallable, Category = "Import Basic Assets")
    <strong>static <mark>UObject* ProcessImportTask</mark>(UAssetImportTask* ImportTask, bool& bOutSuccess, FString& OutInfoMsg);</strong>

Import of basic data types (textures, sounds...) (Editor based)

As mentioned, we work in an inherited class from UBlueprintFunctionLibrary, thus our header file should look like below:

UCLASS()
class UExampleClass : public <mark>UBlueprintFunctionLibrary</mark>
{
 GENERATED_BODY()
public:
 /**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Create and process a basic import task.</mark>
 * Will work for the basic asset types like textures and sounds.
 * Can also be used for meshes, but you might not have all the control you want.
 *
 * @param SourcePath    The path of the source file: "C:/Temp/MyTexture.png"
 * @param DestinationPath   The path of the imported asset: "/Game/Folder/MyTexture"
 * @param bOutSuccess   If the action was a success or not
 * @param OutInfoMsg    More information about the action's result
 *
 * @return The imported asset
 */
 UFUNCTION(BlueprintCallable, Category = "Import Basic Assets")
 <strong>static UObject* ImportAsset(FString SourcePath, FString DestinationPath, bool& bOutSuccess, FString& OutInfoMsg);</strong>

 // <a href="#CreateImportTask">CreateImportTask help function</a>
 // <a href="#ProcessImportTask">ProcessImportTask help function</a>
};

In the similar way, there's possible:

Reimport Asset (Editor based)

The code below is a C++ way of following manual operation: "Selecting an asset in Content window" → Right mouse click → Selecting Reimport option or alternatively Reimport With New File option.

/**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Reimport an already imported asset</mark>
 *
 * @param AssetPath           The path of the asset to reimport: "/Game/Folder/MyTexture"
 * @param SourcePathOverride  <strong>(Optional)</strong> The path of the source file (if you want to use a new file): "C:/Temp/MyTexture.png"			
 * @param OutInfoMsg          More information about the action's result
 * @param return              If the action was a success or not
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=okHpPuWQmJw" target="_blank">Reimport Asset</a>")
<strong>static bool ReimportAsset(FString AssetPath, FString SourcePathOverride, FString& OutInfoMsg);</strong>

Get / Set Asset Import Source Path (Editor based)

Asset Source File Detail

The import path (File Path) is the path of the file that was used to import the asset to Unreal Engine. The code below allows to get File Path of any asset as well as change it to link at another file.

Get Asset Import Path

/**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Get the path used to import his asset</mark>
 *
 * @param AssetPath  The path of the asset: "/Game/Folder/MyTexture"
 * @param bOutSuccess   If the action was a success or not
 * @param OutInfoMsg   More information about the action's result
 *
 * @return The path used to import this asset
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=nlu5b2S_fuM" target="_blank">Modify Asset Import Path</a>")
static FString GetAssetImportPath(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg);

Set Asset Import Path

	/**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Set the path used to import his asset</mark>
 *
 * @param AssetPath  The path of the asset: "/Game/Folder/MyTexture"
 * @param NewImportPath The path of the new source files: "C:/Temp/MyTexture.png"
 * @param OutInfoMsg More information about the action's result
 * @param return If the action was a success or not
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=nlu5b2S_fuM" target="_blank">Modify Asset Import Path</a>")
static bool SetAssetImportPath(FString AssetPath, FString NewImportPath, FString& OutInfoMsg);
  • Setting Source File path for Meshes works only when setting a path with unreal-supported asset type endings (.fbx, and so on).
  • Setting Source File for Texture does not care about extension of the source path
  • Setting Source File for Sound does not care about extension of the source path

Get the Asset Import Data based on the asset class

	/**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Get the Asset Import Data based on the asset class</mark>
 *
 * @param Asset   The asset
 * 
 * @return Asset import data
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=nlu5b2S_fuM" target="_blank">Modify Asset Import Path</a>")
static class UAssetImportData* GetAssetImportData(UObject* Asset);

Import Alembic Geometry Cache

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * Editor Only - Will not work in packaged build.
 *
 * <mark>Create and process a GeometryCache import task.</mark>
 *
 * @param SourcePath		The path of the source file: "C:/Temp/MyGeometryCache.abc"
 * @param DestinationPath	The path of the imported asset: "/Game/Folder/MyGeometryCache"
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 *
 * @return The imported GeometryCache
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=mTKZosgBXAk" target="_blank">Alex Quevillon - Import Alembic Geometry Cache</a>")
<strong>static UGeometryCache* ImportGeometryCache(FString SourcePath, FString DestinationPath, bool& bOutSuccess, FString& OutInfoMsg);</strong>
Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us