Search Unity

Resolved Custom motion transfer constraint

Discussion in 'Animation Rigging' started by MariusLangeland, Apr 5, 2021.

  1. MariusLangeland

    MariusLangeland

    Joined:
    Sep 5, 2018
    Posts:
    7
    How do you go about enabling bi-directional motion transfer for a custom constraint?
     
  2. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi, Adding bi-directional motion transfer for your custom constraints will requires a bit of manual labor. As specified in the documentation, not all constraints implement both "transfer to skeleton" and "transfer to constraint", and enabling the latter requires implementing an inverse constraint to "undo" the constraint effect.

    I recommend having a look at an existing constraint to grasp the workflow. The TwoBoneIKConstraint would be a very good example. Have a look at `TwoBoneIKConstraintEditor.cs` to see how we hook the constraint with the baking api and `TwoBoneIKInverseConstraint.cs` + `TwoBoneIKInverseConstraintJob.cs` to see how the inverse constraint is implemented.

    To hook up your constraint, you need to create a BakeParameters class for your constraint (link) a bit like so:

    Code (CSharp):
    1. [BakeParameters(typeof(MyCustomConstraint))]
    2. class MyCustomConstraintBakeParameters : BakeParameters<MyCustomConstraint>
    3. {
    4.   public override bool canBakeToSkeleton => true;
    5.   public override bool canBakeToConstraint => true; // Only enable this if your constraint is inversable.
    6.  
    7.   public override IEnumerable<EditorCurveBinding> GetSourceCurveBindings(RigBuilder rigBuilder, MyCustomConstraint constraint)
    8.   {
    9.     // Return the list of bindings driving your constraint evaluation
    10.   }
    11.  
    12.    public override IEnumerable<EditorCurveBinding> GetConstrainedCurveBindings(RigBuilder rigBuilder, MyCustomConstraint constraint)
    13.   {
    14.     // Return the list of bindings driven by your constraint.
    15.   }
    16. }