Search Unity

Changing configuration of BlendTrees ChildMotions in code

Discussion in 'Animation' started by AlanGameDev, Jul 19, 2018.

  1. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Hello there.

    I have some animations here and I just want to map them into a 3D tree (composed of 3 2D trees) but I need to do that in code in order to make it easy to tweak and modify, I'm not having success though.

    The motions are created from AnimationClips that are loaded directly from a file containing them (using AssetDatabase.LoadAllAssetsAtPath and casting). The clips themselves are created by a custom importer since the source animation isn't fluid and there are many regions of the timeline to select.
    I am looping those clips and adding a new child in the BlendTree for each of them, I did notice that there are two overloads of the AddChild method that take parameters to configure the motion, but they aren't sufficient for my usage and setting the ChildMotion directly doesn't seem to have any effect, here's some sample code:
    Code (CSharp):
    1. for (var index = 0; index < tree.children.Length; index++) //tree is a BlendTree
    2. {
    3.     ChildMotion motion = tree.children[index];
    4.     motion.position = Random.insideUnitCircle;
    5.     tree.children[index] = motion;
    6.     Debug.Log("changing position of motion "+motion.motion.name);
    7. }
    That snippet prints all the animations in a given tree, but doesn't affect them at all.

    Is that not supported in Unity or do I have to do something else in order to make that work? Please note that I'm reassigning the struct from index so even if it's immutable and value-only it should be replacing the entire data anyway, and also the type of that collection is ChildMotion so I can't simply modify it and re-add to the tree since the AddChild takes a Motion which doesn't hold the configurations I need to set.

    I've tried to track down the memory on the native process to see where it's going but I couldn't make sense of the layout. A Vector2 in memory is easy to identify but it doesn't seem to be getting into the native side of the engine at all, it's just IEEE754 single precision so the data should be exactly the same. Unfortunately I don't have source access anymore to debug that. I'm using Unity 5 and I've tried many versions I have here from 5.4.0b15 to the latest and the problem happens in all of them.

    Currently this is a bit of a blocker in our project.

    Thanks in advance.
     
  2. XiaHong1

    XiaHong1

    Joined:
    Mar 31, 2022
    Posts:
    1
    upload_2024-3-6_16-1-37.png

    tree.children is copy of the list of the blend tree child , you need to set children like this

    ChildMotion[] children = tree.children;
    ArrayUtility.Add<ChildMotion>(ref children, new ChildMotion()
    {
    timeScale = 1f,
    motion = motion,
    position = position,
    threshold = threshold,
    directBlendParameter = "Blend"
    });
    tree.children = children;