Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

mecanim blendtree and serialisation

Discussion in 'Scripting' started by ArcTron3D, Nov 27, 2015.

  1. ArcTron3D

    ArcTron3D

    Joined:
    Oct 27, 2014
    Posts:
    3
    Hi there,

    I have an editor script which builds me an animator controller
    But there is one thing confusing me about blend trees

    The the code with comments below. Any idea?

    Code (CSharp):
    1.  
    2. var mycontroller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath ("Assets/Mecanim/a.controller");
    3.  
    4. //-----------------------------------
    5. // this gets serialized but it's not what I need (or I don't understand how to use it)
    6.  
    7. BlendTree bt1;
    8. mycontroller.CreateBlendTreeInController("root-state-with-blendtree", out bt1, 0);
    9.  
    10. //-----------------------------------
    11. // I would need a blend tree in a state of any statemachine I want
    12.  
    13. var rootStateMachine = mycontroller.layers[0].stateMachine;
    14. var stateMachineA = rootStateMachine.AddStateMachine("smA");
    15. var stateA1 = stateMachineA.AddState("substate-state");
    16.  
    17. // this does not get serialized. After (restarting unity/pressing play) the blendtree is missing and I just get stateA1 with an empty .motion field
    18. BlendTree bt2 = new BlendTree();
    19. stateA1.motion = bt2;
    20.  
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    make your own serialized class that holds a blendtree?
    and what do you not get about this?
    Code (CSharp):
    1. // this gets serialized but it's not what I need (or I don't understand how to use it)
    2. BlendTree bt1;
    3. mycontroller.CreateBlendTreeInController("root-state-with-blendtree", out bt1, 0);
     
  3. ArcTron3D

    ArcTron3D

    Joined:
    Oct 27, 2014
    Posts:
    3
    It's not correct, it's in the wrong position! The BlendTree is inside the State "root-state-with-blendtree" and not inside stateA1 in stateMachineA where I need it.
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ah jes seems you can only create blend trees during runtime as new states not add one to an existing state. But you can also add the needed transitions to this new state and parameters so i guess you get the choice of settingup everything by code or everything manually...
     
  5. ArcTron3D

    ArcTron3D

    Joined:
    Oct 27, 2014
    Posts:
    3
    this is not during runtime
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    sorry i mean through code.
     
  7. mgto

    mgto

    Joined:
    May 30, 2013
    Posts:
    22
    apparently this seems a bit buggy
    Code (CSharp):
    1.  
    2. // this will cause a "key already exist" error
    3. AnimatorState testState = controller.CreateBlendTreeInController("TestBlendTree", out inairBlendTree, 0);
    4.   useStateMachine.AddState(testState, new Vector3(200,100));
    5.  
    6. // adding this will crash unity
    7.   rootStateMachine.RemoveState(testState);
    8.  
    Also there seems to be a script which can copy a controller (also using new BlendTree())
    http://forum.unity3d.com/threads/script-to-copy-statemachines-whole-controllers.332008/

    Maybe it was working some time (tested with 5.3 and it's not working)
     
  8. mgto

    mgto

    Joined:
    May 30, 2013
    Posts:
    22
    Found a (bit nasty) workaround

    Code (CSharp):
    1.  
    2. BlendTree bt;
    3. controller.CreateBlendTreeInController("bt_host", out bt, 0);
    4.  
    5. // now simply ignore the created state and use the blendtree reference where ever you need it
    6.  
    Should file a bug report though


    An interesting note:

    Creating a public BlendTree Field on a monobehaviour and assigning "bt" will show a valid reference to the BlendTree . Assigning "new BlendTree()" will show "TypeMissmatch" even though the visual studio debugger states them as the same type (UnityEngine.BlendTree) which doesn't even exist in a public manner. The type of the objects is defined as UnityEditor.Animations.BlendTree in my code. So there must be something different even if I can't find out what

    ##################

    Edit: Ok this is the proper workaround
    Code (CSharp):
    1.  
    2. private BlendTree NewBlendTree()
    3.   {
    4.   BlendTree bt;
    5.   AnimatorState tmp = mController.CreateBlendTreeInController("tmpState", out bt, 0);
    6.   tmp.motion = null;
    7.   mRootState.RemoveState(tmp);
    8.   return bt;
    9.   }
    10.  
     
    Last edited: Nov 29, 2015
    khan-amil and jister like this.
  9. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    Anybody ever submit this bug report to Unity? If so, can you provide the link? I ran into this same problem and can submit a sample project if not already done. Thanks @mgto for the workaround.