Search Unity

Is there any guarantee as to the Order of Execution of RigConstraints?

Discussion in 'Animation Rigging' started by AndrewKaninchen, May 6, 2019.

  1. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    This is actually a more specific question than I wanted to ask, which would probably be something more alike: is there a way to guarantee AnimationJobs run in their correct order of dependency?

    upload_2019-5-6_15-15-12.png

    While looking for a while at the source code of this preview, I found this part, which serially connects all rig playables contained in a rig, which results in something like this in the Aim example (which has 2 IK constraints and 2 aim constraints):

    upload_2019-5-6_15-17-14.png

    So, my question is, having in mind I didn't read all of the code yet (it's so much code) and don't perfectly understand the APIs yet: does this serial way of plugging and the way the jobs are created and bound to their playables somehow guarantee they execute in order?
     
  2. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hey Andrew,

    It's a really good question!

    So there are two ways to manage the order in which constraints execute.
    1. Set up multiple rigs in the RigBuilder components. Rigs will be executed in the order they've been added in the RigBuilder.
    2. Reorder Game Objects with constraints in the Rig hierarchy. The constraints are retrieved depth first in the hierarchy and respect component order if in the same game object.

    You can refer to the following documentation for more details:
    https://docs.unity3d.com/Packages/com.unity.animation.rigging@0.1/manual/index.html
     
  3. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    Thanks for the answers! I could swear I'd seen someone talk about this hierarchy ordering in the video about this system, good to know I was not remembering it wrong.

    I had not seen this manual. Feel kind of stupid now, because it explains a lot of stuff I took quite some time to figure out just by looking at the examples and code. A lesson for the future, I guess.

    But what I still don't understand is how this guarantee is made in the code. I've read some more of it now and still don't quite get it. I can see how the rig is built in the correct order through the hierarchy, as expected, but I can't yet see how the Jobs are guaranteed to run in order from there.
     
  4. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    The Playable Graphs for constraints are assembled in the `Build()` function of the RigBuilder component.

    The Playable Graph Visualizer gives you an idea of how the graphs are built, but I understand it can be difficult to interpret without further knowledge. Looking at the example you posted on this thread, the first graph syncs the scene values in the animation stream in order for constraints to use them in calculations. The second graph represents the constraints used in the Aim example. So those would be lfik (TwoBoneIK), rfik (TwoBoneIK), aimHead (Aim) and aimGun (Aim) in that order.

    The graphs are registered in the Animator component in the order their outputs were created and are set to read from the previous inputs which allows constraints to be evaluated as a post-process step on the animation (e.g. state-machine)

    Hoping this explains it better :)
     
  5. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    I think I got it. So, by setting the playables created from the constraints to read previous inputs (this, I assume)

    upload_2019-5-8_14-35-43.png

    they are only evaluated as-needed, and therefore run their related jobs in order, as defined by the way they are plugged in the PlayableGraph (serially, in the example above)? And this is all decided by how the AnimationScriptPlayable class is implemented?

    Sorry for all of these questions, I really need to understand how all of this works at some level to do what I'm trying to do. And, again, thanks for the answers!
     
  6. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Not quite. The order in which constraints are evaluated is defined by the graph itself. A playable graph is always evaluated in the order it's connected in (from right to left). A single AnimationScriptPlayable node reads from the stream, evaluates its logic and writes back in the stream which is then read by the next AnimationScriptPlayable node in line and so on.

    As for AnimationStreamSource.PreviousInputs, it just means that the first node of that playable graph will read its input values from the last evaluated graph (in this case, the syncSceneToStream graph). Without this, you would get the Animator default values instead.
     
  7. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    149
    Ok, I think now I got it.

    I'll go back to coding what I'm trying to do. Of I get any useful results I'll probably post it somewhere around this sub-forum (as I estimate a chance of around 99.7% of running into more problems while trying to do so).

    Thank you very, very much for your help.
     
  8. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    This is great to know! I have a follow up question about evaluation order. I have a modular rig with many sub-rigs that also contain rig builders. I understand that constraints within a rigbuilder will be evaluated in scene order, is this also true for the rig builders as well?
    Thanks!!
     
  9. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    @backwheelbates

    Not necessarily. Only nested Animators are guaranteed to evaluate after a parent Animator as they need to know the final position they'll be in before animating.

    Otherwise, the animation system will evaluate Animator hierarchies in parallel, so they shouldn't depend from one another.
     
    backwheelbates likes this.
  10. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    Thanks for the info @simonbz !

    What do you recommend for situations where there are two interdependent animation rigs, ie, a humanoid driving a vehicle (two animation rigs, interdependent animation). How can I sync animation between two separate animation systems then? or would a combined vehicle+character rig be the only solution?

    Thanks again!!