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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Issues using HumanPoseHandler to manually set muscle poses.

Discussion in 'Animation' started by CaffeinatedCoolaid, Sep 26, 2022.

  1. CaffeinatedCoolaid

    CaffeinatedCoolaid

    Joined:
    May 10, 2021
    Posts:
    59
    I'm having an issue trying to manually set muscle poses in Unity. I downloaded a project from Gitub
    https://github.com/umiyuki/HumanoidHandPoseHelper to get a reference on how to achieve what I need as I am not to familiar with Animation/Avatar API's. When trying to use the tool linked my character just disapears when the script is activated, I am trying to figure out why this is. Saving the clips in the tool works but any visualization does not. I'm almost thinking its broken due too differences in Unity versions as the project was made quite a while ago, although I am not seeing any code marked as Depreciated in my IDE. Any one have any ideas what might be going on?

    Side note while messing with it I tried changing my models import settings and when I turn of StripBones on the importer my character disappears in the exact same way.
     
  2. HackNFly

    HackNFly

    Joined:
    Nov 1, 2018
    Posts:
    3
    Just wanted to thank you for this script, this was exactly what I was looking for and I was able to get it to work during gameplay as well. I added an extra variable so I could use a slider to open both hands. The key was to add an update during LateUpdate after any animations might have been applied.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [ExecuteInEditMode]
    7. public class HumanoidHandPoseHelper : MonoBehaviour {
    8.  
    9.     public bool isShowEditHandPose = true;//????????????????
    10.  
    11.     Animator animator;
    12.     public string animationClipName = "";
    13.     [Range(-1.0f, 1.0f)] public float muscleValue = 0.0f;
    14.  
    15.     public static Dictionary<string, string> TraitLeftHandPropMap = new Dictionary<string, string>
    16.     {
    17.             {"Left Thumb 1 Stretched", "LeftHand.Thumb.1 Stretched"},
    18.             {"Left Thumb Spread", "LeftHand.Thumb.Spread"},
    19.             {"Left Thumb 2 Stretched", "LeftHand.Thumb.2 Stretched"},
    20.             {"Left Thumb 3 Stretched", "LeftHand.Thumb.3 Stretched"},
    21.             {"Left Index 1 Stretched", "LeftHand.Index.1 Stretched"},
    22.             {"Left Index Spread", "LeftHand.Index.Spread"},
    23.             {"Left Index 2 Stretched", "LeftHand.Index.2 Stretched"},
    24.             {"Left Index 3 Stretched", "LeftHand.Index.3 Stretched"},
    25.             {"Left Middle 1 Stretched", "LeftHand.Middle.1 Stretched"},
    26.             {"Left Middle Spread", "LeftHand.Middle.Spread"},
    27.             {"Left Middle 2 Stretched", "LeftHand.Middle.2 Stretched"},
    28.             {"Left Middle 3 Stretched", "LeftHand.Middle.3 Stretched"},
    29.             {"Left Ring 1 Stretched", "LeftHand.Ring.1 Stretched"},
    30.             {"Left Ring Spread", "LeftHand.Ring.Spread"},
    31.             {"Left Ring 2 Stretched", "LeftHand.Ring.2 Stretched"},
    32.             {"Left Ring 3 Stretched", "LeftHand.Ring.3 Stretched"},
    33.             {"Left Little 1 Stretched", "LeftHand.Little.1 Stretched"},
    34.             {"Left Little Spread", "LeftHand.Little.Spread"},
    35.             {"Left Little 2 Stretched", "LeftHand.Little.2 Stretched"},
    36.             {"Left Little 3 Stretched", "LeftHand.Little.3 Stretched"},
    37.     };
    38.  
    39.     public static Dictionary<string, string> TraitRightHandPropMap = new Dictionary<string, string>
    40.     {
    41.             {"Right Thumb 1 Stretched", "RightHand.Thumb.1 Stretched"},
    42.             {"Right Thumb Spread", "RightHand.Thumb.Spread"},
    43.             {"Right Thumb 2 Stretched", "RightHand.Thumb.2 Stretched"},
    44.             {"Right Thumb 3 Stretched", "RightHand.Thumb.3 Stretched"},
    45.             {"Right Index 1 Stretched", "RightHand.Index.1 Stretched"},
    46.             {"Right Index Spread", "RightHand.Index.Spread"},
    47.             {"Right Index 2 Stretched", "RightHand.Index.2 Stretched"},
    48.             {"Right Index 3 Stretched", "RightHand.Index.3 Stretched"},
    49.             {"Right Middle 1 Stretched", "RightHand.Middle.1 Stretched"},
    50.             {"Right Middle Spread", "RightHand.Middle.Spread"},
    51.             {"Right Middle 2 Stretched", "RightHand.Middle.2 Stretched"},
    52.             {"Right Middle 3 Stretched", "RightHand.Middle.3 Stretched"},
    53.             {"Right Ring 1 Stretched", "RightHand.Ring.1 Stretched"},
    54.             {"Right Ring Spread", "RightHand.Ring.Spread"},
    55.             {"Right Ring 2 Stretched", "RightHand.Ring.2 Stretched"},
    56.             {"Right Ring 3 Stretched", "RightHand.Ring.3 Stretched"},
    57.             {"Right Little 1 Stretched", "RightHand.Little.1 Stretched"},
    58.             {"Right Little Spread", "RightHand.Little.Spread"},
    59.             {"Right Little 2 Stretched", "RightHand.Little.2 Stretched"},
    60.             {"Right Little 3 Stretched", "RightHand.Little.3 Stretched"},
    61.     };
    62.  
    63.     public Dictionary<string, float> leftHandPoseValueMap = new Dictionary<string, float>
    64.     {
    65.             {"Left Thumb 1 Stretched", 0f},
    66.             {"Left Thumb Spread", 0f},
    67.             {"Left Thumb 2 Stretched", 0f},
    68.             {"Left Thumb 3 Stretched", 0f},
    69.             {"Left Index 1 Stretched", 0f},
    70.             {"Left Index Spread", 0f},
    71.             {"Left Index 2 Stretched", 0f},
    72.             {"Left Index 3 Stretched", 0f},
    73.             {"Left Middle 1 Stretched", 0f},
    74.             {"Left Middle Spread", 0f},
    75.             {"Left Middle 2 Stretched", 0f},
    76.             {"Left Middle 3 Stretched", 0f},
    77.             {"Left Ring 1 Stretched", 0f},
    78.             {"Left Ring Spread", 0f},
    79.             {"Left Ring 2 Stretched", 0f},
    80.             {"Left Ring 3 Stretched", 0f},
    81.             {"Left Little 1 Stretched",0f},
    82.             {"Left Little Spread", 0f},
    83.             {"Left Little 2 Stretched", 0f},
    84.             {"Left Little 3 Stretched", 0f},
    85.     };
    86.     public Dictionary<string, float> rightHandPoseValueMap = new Dictionary<string, float>
    87.     {
    88.                     {"Right Thumb 1 Stretched", 0f},
    89.             {"Right Thumb Spread", 0f},
    90.             {"Right Thumb 2 Stretched", 0f},
    91.             {"Right Thumb 3 Stretched", 0f},
    92.             {"Right Index 1 Stretched", 0f},
    93.             {"Right Index Spread", 0f},
    94.             {"Right Index 2 Stretched", 0f},
    95.             {"Right Index 3 Stretched", 0f},
    96.             {"Right Middle 1 Stretched", 0f},
    97.             {"Right Middle Spread", 0f},
    98.             {"Right Middle 2 Stretched", 0f},
    99.             {"Right Middle 3 Stretched",0f},
    100.             {"Right Ring 1 Stretched", 0f},
    101.             {"Right Ring Spread", 0f},
    102.             {"Right Ring 2 Stretched", 0f},
    103.             {"Right Ring 3 Stretched", 0f},
    104.             {"Right Little 1 Stretched", 0f},
    105.             {"Right Little Spread", 0f},
    106.             {"Right Little 2 Stretched", 0f},
    107.             {"Right Little 3 Stretched", 0f},
    108.     };
    109.  
    110. #if UNITY_EDITOR
    111.     private void OnEnable()
    112.     {
    113.         EditorApplication.update += EditorUpdate;
    114.     }
    115.  
    116.     private void OnDisable()
    117.     {
    118.         EditorApplication.update -= EditorUpdate;
    119.     }
    120. #endif
    121.  
    122.     private void Start()
    123.     {
    124.         Reset();
    125.     }
    126.  
    127.     // Use this for initialization
    128.     void Reset () {
    129.         animator = GetComponent<Animator>();//animator??
    130.  
    131.     }
    132.  
    133.     private void LateUpdate()
    134.     {
    135.         EditorUpdate();
    136.     }
    137.  
    138.     // Update is called once per frame
    139.     void EditorUpdate () {
    140.         if (!isShowEditHandPose) { return; }
    141.         if (animator == null) { return; }
    142.  
    143.         HumanPose pose=new HumanPose();
    144.         var handler = new HumanPoseHandler(animator.avatar, animator.transform);
    145.  
    146.         handler.GetHumanPose(ref pose); //?????
    147.  
    148.         //???????????
    149.         for (int i = 0; i < pose.muscles.Length; i++)
    150.         {
    151.             var muscle = HumanTrait.MuscleName[i];
    152.             if (TraitLeftHandPropMap.ContainsKey(muscle)) //??????????
    153.             {
    154.                 //pose.muscles[i] = leftHandPoseValueMap[muscle];
    155.                 pose.muscles[i] = muscleValue;
    156.             }
    157.             else if (TraitRightHandPropMap.ContainsKey(muscle)) //??????????
    158.             {
    159.                 //pose.muscles[i] = rightHandPoseValueMap[muscle];
    160.                 pose.muscles[i] = muscleValue;
    161.             }
    162.         }
    163.  
    164.         handler.SetHumanPose(ref pose); //??????
    165.     }
    166. }
     
  3. HackNFly

    HackNFly

    Joined:
    Nov 1, 2018
    Posts:
    3
    I'm having problems with it working properly with only 1 hand the other one crunches up improperly. I'll update here if I get it figured out.
     
  4. HackNFly

    HackNFly

    Joined:
    Nov 1, 2018
    Posts:
    3
    So this may be related to your issue. I tried dropping in a new character into my scene and adding the hand pose script. The character immediately darted off screen. It seems to be related to the bodyPosition vector, which is non-zero for some reason. I changed this line and the character stays in place.

    Code (CSharp):
    1.         pose.bodyPosition = Vector3.zero;
    2.         handler.SetHumanPose(ref pose); //??????