1

I am trying to rotate a bone of a mesh in C++ using Unreal Engine 5.

I have already tried using USkeletalMeshComponent and UAnimInstance to get the bone transform and update the animation, but I am facing errors like “USkeletalMeshComponent has no member SetBoneTransform”. I am seeking help to resolve this issue.

USkeletalMeshComponent* mesh = GetMesh();
UAnimInstance* animInstance = mesh->GetAnimInstance();

FName boneName = "head";
int32 boneIndex = mesh->GetBoneIndex(boneName);

while (true)
{
FQuat rotation = FQuat::MakeFromEuler(FVector(0.f, 0.f, 45.f));
FVector location(100.f, 0.f, 0.f); // example location
FVector scale(1.f, 1.f, 1.f); // example scale
FTransform newTransform(rotation, location, scale);

FTransform boneTransform = mesh->GetBoneTransform(boneIndex, FTransform::Identity);
boneTransform = newTransform;
mesh->SetBoneTransform(boneIndex, boneTransform, EBoneSpaces::WorldSpace);

animInstance->UpdateAnimation(0.f, false);
}
}

1 Answers1

0

Indeed SkeletalMeshComponent has no method to access and transform bones directly, I had the same issue and I solved using PoseableMesh instead: https://docs.unrealengine.com/5.1/en-US/BlueprintAPI/Components/PoseableMesh/

PoseMesh->SetBoneTransformByName(
    myBoneName, 
    myBoneTranform,
    EBoneSpaces::ComponentSpace
);
Mauro Dorni
  • 455
  • 2
  • 14
  • Hi thanks for your help but after i implemented it i got this issue error C2039: 'SetBoneTransformByName': is not a member of 'USkeletalMeshComponent' E:\Games\UE_5.1\Engine\Source\Runtime\Engine\Classes\GameFramework\Character.h(28): note: see declaration of 'USkeletalMeshComponent' – Muhammad Tariq May 08 '23 at 07:01
  • 1
    yes cause you are still using USkeletalMeshComponent -> you must replace it with PoseableMeshComponent instead and then you can use the method I mentioned above – Mauro Dorni May 09 '23 at 08:52
  • Thank you very much for helping i am using the default character in unreal engine and when i initialize (UPoseableMeshComponent* PoseableMesh = FindComponentByClass();) and check if (PoseableMesh) exists then run the code but it does not exsists also it crashes if i remove the check I am sorry I am a beginner i don't know almost anything about it Thanks for your help boss – Muhammad Tariq May 09 '23 at 14:54
  • yes, of course: standard character has no poseable mesh, it uses skeletal one. what you can do is create your own character with the poseable component, just copy the character structure(h and cpp) rename it as you prefer and replace skeletal with poseable; then use it in your project – Mauro Dorni May 10 '23 at 08:02