How to change skeletalmesh and animblueprint at runtime?

I have started this question on answerhub, but did not get answer.

Maybe here someone can answer, if he tried same thing to do.

The main idea is to change mesh and animations in same blueprint, and not spawn new another blueprint of same class whith same parameters and only with different skeletal mesh and animations.

class .h

UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
TSubobjectPtr<USkeletalMeshComponent> CurrentMesh;
UPROPERTY(EditDefaultsOnly, Category = Mesh)
USkeletalMesh* FlagMesh;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Animation)
UAnimBlueprint* FlagVIM;

class .cpp

CurrentMesh->SetSkeletalMesh(FlagMesh); //works
CurrentMesh->SetAnimClass(FlagVIM->GetAnimBlueprintGeneratedClass()); //fails

So, i do not understand what to pass to Mesh->SetAnimClass() as parameter UCLASS*, because all my tries failed.

Still no answer is. :frowning:
No one tried to do same thing?

Hi h2o,

I’d really expect the code you pasted to work as expected, unless the skeleton of FlagMesh and FlagVIM do not match.

Can you set a breakpoint after the call to SetAnimClass, and inspect CurrentMesh to see what the value of AnimScriptInstance is? Also double-check the value of FlagMesh->Skeleton and FlagVIM->GeneratedClass->TargetSkeleton are as you expected.

You can also try setting AnimationMode = EAnimationMode::Type::AnimationBlueprint and AnimBlueprintGeneratedClass to FlagVIM->GetFoo directly.

Michael Noland

Tnx for reply **Michael Noland **

SetAnimClass() as i can see do all this things and also recreates new animinstance. So i do not sure that direct AnimBlueprintGeneratedClass setting will be correct, because AnimInstance will be old and wrong.

I have done additional investigation - and i was wrong.

CurrentMesh->SetSkeletalMesh(FlagMesh); //works
CurrentMesh->SetAnimClass(FlagVIM->GetAnimBlueprintGeneratedClass()); //works too

After the line CurrentMesh->SetSkeletalMesh(FlagMesh) i got this message:

But in VS debug after second line CurrentMesh->SetAnimClass(FlagVIM->GetAnimBlueprintGeneratedClass()); it looks like it set right:

So, to change skeletal mesh i need to do something else before call SetSkeletalMesh().
I remember that some time ago i saw in answerhub thread where epic support answered that it is not needed to recreate USkeletalMeshComponent, just need to SetSkeletalMesh(), but should be also something else to clear old mesh and then set new one.

As, it looks like SetAnimClass() works proper, than animation do not work with different reason. So here is just one last question, how to correct change skeletal mesh to not have animBp conflict.

This is the actual error, the skeletons don’t match. You can solve it in a couple of different ways, depending on exactly how your meshes are set up. Do both flag meshes have the same number of bones and are they named the same? If so (or if one flag has the same bones at the root, but additional leaf bones), then you can make the share the same skeleton. The easiest way is to reimport the second mesh and when importing pick the Skeleton of the first mesh (the one that you already made FlagVIM for). You’ll also need to remport any animations that target the second flag.

Michael Noland