Working with Static Mesh in Unreal Engine

Published on

What is it Static Mesh, what properties does it have and how to work with it in the Unreal Engine?

Platforma

Static Meshes

Static Mesh assets are the basic unit used to create world geometry for levels. These 3D models are created in external modeling applications (such as 3dsMax, Maya, Blender, and so on) that are imported into the Unreal Editor through the Content Browser. The vast majority of any level made with Unreal Engine will consist of Static Meshes, generally in the form of Static Mesh Actors.

Instanced Static Mesh

Import Static Mesh - .fbx file

#include "Kismet/BlueprintFunctionLibrary.h" // Engine, <mark>Editor only</mark>
/**
 * Editor Only - Will not work in packaged build.
 *
 * Create and process a StaticMesh import task.
 *
 * @param SourcePath		The path of the source file: "C:/Temp/MyStaticMesh.fbx"
 * @param DestinationPath	The path of the imported asset: "/Game/Folder/MyStaticMesh"
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 *
 * @return The imported StaticMesh
 */
UFUNCTION(BlueprintCallable, Category = "")
<strong>static class UStaticMesh* ImportStaticMesh(FString SourcePath, FString DestinationPath, bool& bOutSuccess, FString& OutInfoMsg);</strong>
.cpp file
#include "Factories/FbxImportUI.h" // <mark>UnrealEd (Editor Only)</mark>
#include "Factories/FbxStaticMeshImportData.h" // <mark>UnrealEd (Editor Only)</mark>

UStaticMesh* UExampleClass::ImportStaticMesh(FString SourcePath, FString DestinationPath, bool& bOutSuccess, FString& OutInfoMsg)
{
   <mark><a href="https://docs.unrealengine.com/5.2/en-US/API/Editor/UnrealEd/Factories/UFbxImportUI/" target="_blank">UFbxImportUI</a>* Options</mark> = NewObject&lt;UFbxImportUI&gt;();

   // Required options to specify that we're importing a static mesh
   Options-&gt;bAutomatedImportShouldDetectType = false;
   Options-&gt;MeshTypeToImport = <mark>EFBXImportType::FBXIT_StaticMesh</mark>;
   Options-&gt;bImportMesh = true;
   // No skeletal mesh or animations
   Options-&gt;bImportAsSkeletal = <mark>false</mark>;
   Options-&gt;bImportAnimations = false;
   Options-&gt;bCreatePhysicsAsset = false;

   // More options
   Options-&gt;bImportTextures = true;
   Options-&gt;bImportMaterials = true;
   Options-&gt;bResetToFbxOnMaterialConflict = true;
   Options-&gt;LodNumber = 0;

   // UFbxAssetImportData
   Options-&gt;StaticMeshImportData-&gt;ImportTranslation = FVector(0.0f);
   Options-&gt;StaticMeshImportData-&gt;ImportRotation = FRotator(0.0f);
   Options-&gt;StaticMeshImportData-&gt;ImportUniformScale = 1.0f;
   Options-&gt;StaticMeshImportData-&gt;bConvertScene = true;
   Options-&gt;StaticMeshImportData-&gt;bForceFrontXAxis = true;
   Options-&gt;StaticMeshImportData-&gt;bConvertSceneUnit = true;

   // UFbxMeshImportData
   Options-&gt;StaticMeshImportData-&gt;bTransformVertexToAbsolute = false;
   Options-&gt;StaticMeshImportData-&gt;bBakePivotInVertex = false;
   Options-&gt;StaticMeshImportData-&gt;bImportMeshLODs = true;
   Options-&gt;StaticMeshImportData-&gt;NormalImportMethod = EFBXNormalImportMethod::FBXNIM_ComputeNormals;
   Options-&gt;StaticMeshImportData-&gt;NormalGenerationMethod = EFBXNormalGenerationMethod::BuiltIn;
   Options-&gt;StaticMeshImportData-&gt;bComputeWeightedNormals = true;
   Options-&gt;StaticMeshImportData-&gt;bReorderMaterialToFbxOrder = false;

   // UFbxStaticMeshImportData
   Options-&gt;StaticMeshImportData-&gt;StaticMeshLODGroup = FName();
   Options-&gt;StaticMeshImportData-&gt;VertexColorImportOption = EVertexColorImportOption::Replace;
   Options-&gt;StaticMeshImportData-&gt;bRemoveDegenerates = true;
   Options-&gt;StaticMeshImportData-&gt;bBuildReversedIndexBuffer = true;
   Options-&gt;StaticMeshImportData-&gt;bBuildNanite = true;
   Options-&gt;StaticMeshImportData-&gt;bGenerateLightmapUVs = true;
   Options-&gt;StaticMeshImportData-&gt;bOneConvexHullPerUCX = true;
   Options-&gt;StaticMeshImportData-&gt;bAutoGenerateCollision = true;
   Options-&gt;StaticMeshImportData-&gt;bCombineMeshes = true;
   Options-&gt;StaticMeshImportData-&gt;DistanceFieldResolutionScale = 0.0f;

   // Create the import task
   UAssetImportTask* Task = <a href="/unreal-engine/import-assets#CreateImportTask">UExampleClass::CreateImportTask</a>(SourcePath, DestinationPath, nullptr, <strong>Options</strong>, bOutSuccess, OutInfoMsg);
   if (!bOutSuccess) return nullptr;
	
   // Import the asset
   UStaticMesh* RetAsset = Cast&lt;UStaticMesh&gt;(<a href="#ProcessImportTask">UExampleClass::ProcessImportTask</a>(Task, bOutSuccess, OutInfoMsg));
   if (!bOutSuccess) return nullptr;
	
   // Return the imported asset
   bOutSuccess = true;
   OutInfoMsg = FString::Printf(TEXT("Import Static Mesh Succeeded - '%s'"), *DestinationPath);
   return RetAsset;
}

Assign material for Static Mesh

Generate Static Mesh Collisions With C++

Auto generate a convex collision for a static mesh

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * <strong>Editor Only</strong> - Will not work in packaged build.
 * 
 * <mark>Auto generate a convex collision for a static mesh.</mark>
 *
 * @param StaticMesh		   Static mesh to add convex collision on.
 * @param HullCount			Maximum number of convex pieces that will be created. Must be positive.
 * @param MaxHullVerts		Maximum number of vertices allowed for any generated convex hull.
 * @param HullPrecision		Number of voxels to use when generating collision. Must be positive.
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=gh1ScZaiBDM" target="_blank">Alex Quevillon - Generate Mesh Collisions</a>")
<strong>static void AutoGenetateStaticMeshCollisions(class UStaticMesh* StaticMesh, int HullCount, int MaxHullVerts, int HullPrecision, bool& bOutSuccess, FString& OutInfoMsg);</strong>

Remove collisions from a static mesh

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * <strong>Editor Only</strong> - Will not work in packaged build.
 * 
 * <mark>Remove collisions from a static mesh.</mark>
 * 
 * @param StaticMesh		Static mesh to remove collisions from.
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=gh1ScZaiBDM" target="_blank">Alex Quevillon - Generate Mesh Collisions</a>")
<strong>static void ClearStaticMeshCollisions(class UStaticMesh* StaticMesh, bool& bOutSuccess, FString& OutInfoMsg);</strong>

Add Box Collision To Static Mesh

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * <strong>Editor Only</strong> - Will not work in packaged build.
 * 
 * <mark>Add Box Collision To Static Mesh</mark>
 *
 * @param StaticMesh		   Static mesh to add box collision on
 * @param CollisionIndex	Index of the collision to update. If invalid, will create a new collision
 * @param Location			Location of the box collision
 * @param Rotation			Rotation of the box collision
 * @param Extents			   Extents of the box collision
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	      More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=gh1ScZaiBDM" target="_blank">Alex Quevillon - Generate Mesh Collisions</a>")
<strong>static void AddOrUpdateBoxCollisionOnStaticMesh(UStaticMesh* StaticMesh, int CollisionIndex, FVector Location, FRotator Rotation, FVector Extents, bool& bOutSuccess, FString& OutInfoMsg);</strong>

Add Sphere Collision To Static Mesh

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * <strong>Editor Only</strong> - Will not work in packaged build.
 * 
 * <mark>Add Sphere Collision To Static Mesh</mark>
 *
 * @param StaticMesh		   Static mesh to add sphere collision on
 * @param CollisionIndex	Index of the collision to update. If invalid, will create a new collision
 * @param Location			Location of the sphere collision
 * @param Radius			   Radius of the sphere collision
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	      More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=gh1ScZaiBDM" target="_blank">Alex Quevillon - Generate Mesh Collisions</a>")
<strong>static void AddOrUpdateSphereCollisionOnStaticMesh(class UStaticMesh* StaticMesh, int CollisionIndex, FVector Location, float Radius, bool& bOutSuccess, FString& OutInfoMsg);</strong>

Add Capsule Collision To Static Mesh

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * <strong>Editor Only</strong> - Will not work in packaged build.
 * 
 * <mark>Add Capsule Collision To Static Mesh</mark>
 *
 * @param StaticMesh		Static mesh to add capsule collision on
 * @param CollisionIndex	Index of the collision to update. If invalid, will create a new collision
 * @param Location			Location of the capsule collision
 * @param Rotation			Rotation of the capsule collision
 * @param Length			Length of the capsule collision
 * @param Radius			Radius of the capsule collision
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=gh1ScZaiBDM" target="_blank">Alex Quevillon - Generate Mesh Collisions</a>")
<strong>static void AddOrUpdateCapsuleCollisionOnStaticMesh(class UStaticMesh* StaticMesh, int CollisionIndex, FVector Location, FRotator Rotation, float Length, float Radius, bool& bOutSuccess, FString& OutInfoMsg);</strong>
Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us