Animator Controller Layer and Default Weight

I’m making a script which automate character animation tree setup. However I have several issues.
First is default weight. I can’t set default weight for layers.
I use code

attackLayer.defaultWeight = 1.0f;

It’s does nothing. I’m still able to get data from the layer like.

AnimatorStateMachine attackStateMachine = attackLayer.stateMachine;

Works just fine.

Also if try add AvatarMask with code.

attackLayer.avatarMask = m;

Where m begin my AvatarMask, nothing happens.

Any ideas?

I know this is a very old question, but I couldn’t find a solution online and I had the same issue today. So I figured I might as well post it:

When adding a layer with AnimatorController.AddLayer("New layer");, a defaultWeight of 0 is already assigned to the layer, so changing after the layer is added won’t have any effect. What you need to do is to create the AnimatorControllerLayer first, assign it the weight you want, and then add it to the AnimatorController.

So:

var layer = new UnityEditor.Animations.AnimatorControllerLayer
{
    name = "New layer",
    defaultWeight = 1f,
    stateMachine = new UnityEditor.Animations.AnimatorStateMachine() // Make sure to create a StateMachine as well, as a default one is not created
};

controller.AddLayer(layer);
4 Likes

When I duplicate the ‘AnimatorController’ after the changes has been made, the AnimatorStateMachine in the duplicated one would disappear visually… do you have any idea?

ok, after I read a few threads, it appears that layers are assets, therefore should be saved to the disk… read (this) for further details => AnimatorController.AddLayer() doesn't create default AnimatorStateMachine

here’s how it was done:

const string controllerPath = "Assets/AnimatorController.controller";

AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath(controllerPath);

AnimatorStateMachine stateMachine = new AnimatorStateMachine
{
    name = controller.MakeUniqueLayerName("New Layer"),
    hideFlags = HideFlags.HideInHierarchy
};

controller.AddLayer(new AnimatorControllerLayer
{
    stateMachine = stateMachine,
    name = stateMachine.name,
    defaultWeight = 1f
});

AssetDatabase.AddObjectToAsset(stateMachine, controllerPath);