# Manipulating skeletal mesh vertex positions at runtime

**URL:** <https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [October 30, 2018, 7:17am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690 "2018-10-30T07:17:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeStErThEmOlEsT](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LeStErThEmOlEsT](https://forums.unrealengine.com/u/LeStErThEmOlEsT)\
**Post date:** [October 30, 2018, 7:17am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690/1 "2018-10-30T07:17:01Z")

</div>

Hey guys, I’m quite new to C++ programming (I much prefer C#) but I have a bit of experience with the engine itself (material creation etc.) and I’ve run into a bit of a problem. I’m trying to manipulate the vertex positions of a skeletal mesh at runtime without the editor. At first the engine crashed but I was able to fix that due to an array index being out of bounds. The real question I’m having is how can I update the mesh after the positions have been updated and to also fix a memory leak I’m having as well?

The code in question is here (no need for the header file as there’s not much other than the void function).

```
#include "UpdateVertex.h"
#include "SkeletalMeshTypes.h"
#include "Engine/Public/Rendering/SkeletalMeshLODRenderData.h"
#include "Engine/Public/Rendering/SkeletalMeshRenderData.h"
#include "Engine/SkeletalMesh.h"
#include "Components/SkinnedMeshComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine.h"

//DOESN'T UPDATE MESH AND CAUSES HUGE MEMORY LEAK OF OVER 200MB
void UUpdateVertex::Update(USkeletalMeshComponent* SkeletalMeshComp, int32 LodIndex)
{
	//Avoid running if no skeletal mesh was found
	if (!SkeletalMeshComp->SkeletalMesh)
	{
		UE_LOG(LogTemp, Warning, TEXT("No skeletal mesh found!"));
		return;
	}
	USkeletalMesh* SkelMesh = SkeletalMeshComp->SkeletalMesh;

	FSkeletalMeshRenderData* RenderData = SkelMesh->GetResourceForRendering();

	//Avoid crashing if index is higher than meshes LOD count
	if (LodIndex > SkelMesh->GetLODNum())
	{
		UE_LOG(LogTemp, Warning, TEXT("LOD greater than index!"));
		return;
	}
	FSkeletalMeshLODRenderData& LodRender = RenderData->LODRenderData[LodIndex];

	//Update every vertex position in every render section
	for (int32 j = 0; j < LodRender.RenderSections.Num(); j++)
	{
		for (int32 i = 0; i < LodRender.RenderSections[j].GetNumVertices(); i++)
		{
			LodRender.StaticVertexBuffers.PositionVertexBuffer.VertexPosition(i) += FVector(0.0f, 0.0f, 10.0f);
			UE_LOG(LogTemp, Warning, TEXT("Vertex position: %s"), *LodRender.StaticVertexBuffers.PositionVertexBuffer.VertexPosition(i).ToString());
		}
	}
	//FSkeletalMeshLODRenderData* LodRender = RenderData->LODRenderData;
	
	SkeletalMeshComp->MarkRenderStateDirty();
}

```

I’m planning on separating the variables into an init function if that helps with the memory leak.

---

<div class="post-metadata">

**Author:** ![LeStErThEmOlEsT](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LeStErThEmOlEsT](https://forums.unrealengine.com/u/LeStErThEmOlEsT)\
**Post date:** [November 2, 2018, 9:03am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690/2 "2018-11-02T09:03:31Z")

</div>

Okay so I fixed the memory leak, turns out it was just the spamming of output logs in the ‘for loop’ that caused the RAM to go up like that. However, I still haven’t found a way for the updated vertex positions to render…

---

<div class="post-metadata">

**Author:** ![HarryHighDef](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HarryHighDef](https://forums.unrealengine.com/u/HarryHighDef)\
**Post date:** [November 2, 2018, 9:23am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690/3 "2018-11-02T09:23:20Z")

</div>

Do you only want to add 10 units on Z to each vertex?

---

<div class="post-metadata">

**Author:** ![Liepsla](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Liepsla](https://forums.unrealengine.com/u/Liepsla)\
**Post date:** [May 21, 2020, 9:00am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690/4 "2020-05-21T09:00:32Z")

</div>

You can call

```
SkelMesh->PostEditChange()
```

 and 

```
SkelMesh->PostLoad()
```

  
Depending on your UE4 version, you may need to call 

```
SkelMesh->InvalidateDeriveDataCacheGUID()
```

---

<div class="post-metadata">

**Author:** ![dandingol03](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dandingol03/32/769404_2.png) [@dandingol03](https://forums.unrealengine.com/u/dandingol03)\
**Post date:** [November 16, 2023, 3:02am UTC](https://forums.unrealengine.com/t/manipulating-skeletal-mesh-vertex-positions-at-runtime/436690/5 "2023-11-16T03:02:23Z")

</div>

doesn’t work
