Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 5: Unable to set directBlendParameter values in DirectBlendTree created by editor script

Discussion in 'Animation' started by TrickyHandz, Mar 12, 2015.

  1. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    I am working on an editor extension to speed up my workflow which involves creating AnimatorController assets with direct Blendtrees. The following happens in the code:

    - AnimatorController created [SUCCESSFUL]
    - Appropriate Parameters are added to AnimatorController [SUCCESSFUL]
    - Appropriate layers are added to controller [SUCCESSFUL]
    - AnimatorStates created using BlendTrees of BlendTreeType.Direct [SUCCESSFUL]
    - AnimationClips added to BlendTrees [SUCCESSFUL]
    - DirectBlendParameter set for each motion in BlendTree.children [FAILED]

    To set the directBlendParameter I am iterating through all children of the blendTree and assigning a string value from the based on the parameters I added to the AnimatorController. That portion of the code looks a bit like this:

    Code (csharp):
    1.  
    2. // NOTE: List<string> parameterList is used in another area of
    3. // the code to provide the names of all the AnimatorController.parameters
    4. // of type AnimatorControllerParameterType.Float which is successful
    5.  
    6. for(int i=0; i<targetBlendTree.children.Length; i++)
    7. {
    8.     targetBlendTree.children[i].directBlendParameter = parameterList[i];
    9. }
    10.  
    11.  
    All the ChildMotion properties are successfully set before the loop, but the directBlendParameter value remains set to the default value of "Blend". As ChildMotion.directBlendParameter is a public string, I don't see why this isn't working. Is there an undocumented method to do this or should I report this as a bug in the UnityEditor.Animations API?
     
    nonopblic likes this.
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    BlendTree.children return an array of ChildMotion which is a struct.
    Since struct is c# are always passed by copy you are actually changing only the copy, not the real object.

    Try this
    Code (CSharp):
    1. ChildMotion[] children = targetBlendTree.children;
    2. for(int i=0; i<children .Length; i++)
    3. {
    4.     children[i].directBlendParameter = parameterList[i];
    5. }
    6. targetBlendTree.children = children;
     
    nonopblic, EddieLopes and TrickyHandz like this.
  3. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    Thank you so much for this. I completely overlooked the fact that ChildMotion is a struct. I'll be trying out this solution as soon as I'm back at my development machine.
     
  4. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    Thought I would update this since I have had a chance to test it out now. This worked brilliantly!! I can't thank you enough @Mecanim.Dev
     
    theANMATOR2b likes this.