How to set animation blueprint in c++

Hi,

I have a character with a skeletal mesh and I want to attach the animation blueprint to it.
could someone help out?

1 Like

found the problem :slight_smile:

// load the animation blueprint
const ConstructorHelpers::FObjectFinder<UAnimBlueprint> AnimObj(TEXT("AnimBlueprint'/Game/MixamoAnimPack/Mixamo_Maw/Anims/MixamoAnimBP_Maw.MixamoAnimBP_Maw'"));

// generated class is important here :)
GetMesh()->SetAnimInstanceClass(AnimObj.Object->GeneratedClass);
2 Likes

As of 4.7.3 (not sure about previous builds) this only works in Editor builds, it does not work in Cooked builds. Please test and confirm.

With 4.7.X doesn’t work in Cooked builds, to make it work in cooked builds the right way to set it up is:

static ConstructorHelpers::FObjectFinder AnimBPFinder(TEXT("/Path/YourAnimBP.YourAnimBP_C));

GetMesh()->AnimBlueprintGeneratedClass = AnimBPFinder.Object;

Can confirm - also, if you’re trying to find the AnimBlueprint outside of the constuctor, you can use StaticLoadObject(), e.g.

UAnimBlueprintGeneratedClass* armAnimBPGC = Cast<UAnimBlueprintGeneratedClass>(StaticLoadObject(UAnimBlueprintGeneratedClass::StaticClass(), NULL, *YourPathFStringVariable));
	if (armAnimBPGC)
	{
		ArmMesh->AnimBlueprintGeneratedClass = armAnimBPGC;
	}

static ConstructorHelpers::FObjectFinder MeshAnimClass(TEXT(“AnimBlueprintGeneratedClass’/Game/Characters/MixamoAnimBP_Zoe.MixamoAnimBP_Zoe_C’”));
Mesh->AnimBlueprintGeneratedClass = MeshAnimClass.Object;

For me this works best, this is a quote from an epic staff member somewhere in the forum.
It works in the editor and cooked builds. It is also important, he says, that one references the BlueprintGeneratedClass not the blueprint itself, similiar as one would reference a compiled class and not its source code.

As for 4.10.1 I’m safely using:

static ConstructorHelpers::FObjectFinder<UAnimBlueprint> TmpMeshAnim( TEXT( "/MyGame/Anims/Char_Anim_BP.Char_Anim_BP" ) );
this->GetMesh()->SetAnimInstanceClass( TmpMeshAnim.Object->GetAnimBlueprintGeneratedClass() );

And it works both in editor and in cooked version. So you should simply use GetAnimBlueprintGeneratedClass method.

As of 4.12/4.13/4.14 I can’t get this method to work. Works in editor but not in packaged projects.

Same here I can’t get it to work. It works in the editor but causes the game to crash when I change the animation instance

What’s the right way now? AnimBlueprintGeneratedClass give deprecation warnings as of ~4.13

I saw this in the code as of 4.16.3:

	// Get the IAnimClassInterface associated with this context, if there is one.
	// Note: This can return NULL, so check the result.
	IAnimClassInterface* GetAnimClassInterface() const
	{
		return AnimClassInterface;
	}

	// Get the Blueprint Generated Class associated with this context, if there is one.
	// Note: This can return NULL, so check the result.
	DEPRECATED(4.11, "GetAnimBlueprintClass() is deprecated, UAnimBlueprintGeneratedClass should not be directly used at runtime. Please use GetAnimClassInterface() instead.")
	UAnimBlueprintGeneratedClass* GetAnimBlueprintClass() const
	{
		return Cast<UAnimBlueprintGeneratedClass>(IAnimClassInterface::GetActualAnimClass(AnimClassInterface));
	}

This ended up working for me:

static ConstructorHelpers::FObjectFinder<UAnimBlueprintGeneratedClass> FooAnimBPObj(TEXT("AnimBlueprintGeneratedClass'/Game/FooAnimBP.FooAnimBP_C'"));
check(FooAnimBPObj.Succeeded());
FooMesh->AnimClass = FooAnimBPObj.Object;

The deprecation warning on the member was a bit different than on the accessor and said to use AnimClass. At first it failed for me but turned out to be for a different reason and worked once I had the unrelated problem fixed up.

This is working in 4.19.

It took me a while to figure this one out. Hat tip to Sion Sheevok via discord for the answer:

// .h use an array or single pointer as you like.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Animation)
TArray<UAnimBlueprintGeneratedClass*> dyn_animbps;

// Set the values in the editor to the animBP(s) you want to reference

// .cpp

GetMesh()->SetAnimInstanceClass(dyn_animbps[MyAppearance.mesh]);

in 4.26 the best way to go about this is to create a UClass pointer in your .h file like this:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = “SkeletalMesh”);
UClass* AnimBp;

and in your CPP file the following:

static ConstructorHelpers::FObjectFinder AnimBlueprintLocation(TEXT(“Class /Game/Blueprints/NewAnimBlueprint1.NewAnimBlueprint1_C’”));

//check how I did not search for an animation blueprint but for its class directly and also add the _C at the end of your reference path otherwise it will not find it and crash.

// then you can proceed as expected with:

AnimBp = AnimBlueprintLocation.Object;

SkeletalMeshComponent->SetAnimationMode(EAnimationMode::AnimationBlueprint);
SkeletalMeshComponent->SetAnimInstanceClass(AnimBp);

// I checked it both in the editor and in a packaged project. It took me some time to get this working today.
// for me the biggest learning moment was that searching for blueprints in general is something that seems to crash in a C++ project.

in 4.26 the best way to go about this is to create a UClass pointer in your .h file like this:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = “SkeletalMesh”);
UClass* AnimBp;

and in your CPP file the following:

static ConstructorHelpers::FObjectFinder AnimBlueprintLocation(TEXT(“Class /Game/Blueprints/NewAnimBlueprint1.NewAnimBlueprint1_C’”));

//check how I did not search for an animation blueprint but for its class directly and also add the _C at the end of your reference path otherwise it will not find it and crash.

// then you can proceed as expected with:

AnimBp = AnimBlueprintLocation.Object;

SkeletalMeshComponent->SetAnimationMode(EAnimationMode::AnimationBlueprint);
SkeletalMeshComponent->SetAnimInstanceClass(AnimBp);

// I checked it both in the editor and in a packaged project. It took me some time to get this working today.
// for me the biggest learning moment was that searching for blueprints in general is something that seems to crash in a C++ project.

1 Like

Hi is ConstructorHelpers::FObjectFinder not templated? For me it fails with

“E0441 argument list for class template “ConstructorHelpers::FObjectFinder” is missing”

Thanks!

Thanks for posting the solution.