How get the animation blend weight of some state in C++

Hi Guys,

I need to know how get the current blend weight of some state on the AnimGraph/StateMachine using C++. Exactly the same thing that you can do in Blueprint (AnimBlueprint/AnimGraph) using “State Weight” node.

In UAnimInstance we have this function: float GetInstanceStateWeight(int32 MachineIndex, int32 StateIndex); exactly for that, but require the StateIndex and that is the problem. I can’t found a way to get the StateIndex from a FName.

I was trying this:

int32 MachineIndex = GetStateMachineIndex(StateMachineName);
FAnimNode_StateMachine* MachineInstance = GetStateMachineInstance(MachineIndex);
if (MachineInstance)
{
	const FBakedAnimationStateMachine* MachineDescription = MachineInstance->GetMachineDescription();
	int32 StateIndex = MachineDescription->FindStateIndex(StateName);
}

but FAnimNode_StateMachine::GetMachineDescription() is protected :frowning:

Someone can help me with this, please ?

Thanks

hellooo… someone here ? :slight_smile:

bump, bump …

… no one can point me in the right direction here ?

Hi. Came across your post here as I was looking to get the state weight myself from blueprint.
I did not manage to do that (within regular blueprint, I know it can be done in the animblueprints state and machine -graph).

Using your code I ended up with this that works, at least for me:

float APlayerCharacter::GetStateWeight( FName machineName, FName stateName ) const
{
	float weight = 0;
	UAnimInstance* animInstance = Mesh1P->GetAnimInstance();
	if (animInstance)
	{
		FAnimNode_StateMachine* machine = animInstance->GetStateMachineInstanceFromName(machineName);
		if( machine )
		{
			IAnimClassInterface* iface = IAnimClassInterface::GetFromClass( animInstance->GetClass() );
			const FBakedAnimationStateMachine* baked = animInstance->GetMachineDescription( iface, machine );

			if( baked )
			{
				int stateIdx = baked->FindStateIndex( stateName );
				if( stateIdx != INDEX_NONE )
				{
					return machine->GetStateWeight( stateIdx );
				}
			}
		}
	}

	return weight;
}

( I actually expect that there is a way to get the weight already in blueprint and that I haven’t found it yet. Anything else would just be very lacking for such an engine… )

Work like a charm! Thx