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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move Humanoid Muscles

Discussion in 'Scripting' started by The-Little-Guy, Jan 14, 2016.

  1. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I am messing around with unity's HumanPose and HumanPoseHandler, and I am having issues.

    I have a script that contains 2 fields "root" and "amount" and nothing else. I then created this Editor class:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(Pose))]
    5. public class PoseEditor : Editor {
    6.  
    7.     HumanPoseHandler hph;
    8.     HumanPose hp;
    9.     Avatar avatar;
    10.     float[] defaultMuscles;
    11.  
    12.     Pose t;
    13.  
    14.     public void OnEnable(){
    15.         t = target as Pose;
    16.         avatar = t.GetComponent<Animator>().avatar;
    17.         hph    = new HumanPoseHandler(avatar, t.root);
    18.         hp     = new HumanPose();
    19.         hph.GetHumanPose(ref hp);
    20.         defaultMuscles = hp.muscles;
    21.     }
    22.  
    23.     public override void OnInspectorGUI(){
    24.  
    25.  
    26.         t.root = EditorGUILayout.ObjectField(new GUIContent("Root", "Usually the characters hips"), t.root, typeof(Transform), true) as Transform;
    27.         t.amount = EditorGUILayout.Slider("Amount", t.amount, -180f, 180f);
    28.  
    29.         for (var i = 0; i < defaultMuscles.Length; i++) {
    30.             hp.muscles[i] = Mathf.Clamp(t.amount, -180f, 180f);
    31.         }
    32.  
    33.         hph.SetHumanPose(ref hp);
    34.  
    35.     }
    36.  
    37. }
    as you move the "amount" slider it changes the "muscles" value. I am then setting that to "SetHumanPose", but no change is taking effect. What I thought would happen is that character would move the muscles for example open/close their finger.
     
    Last edited: Jan 14, 2016
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Anyone have any thoughts?
     
  3. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
    I think it's a bug.
     
  4. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    oh ? you don't say...
     
  5. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
    In My Project, humanPoseHandler.GetHumanPose(ref pose ) generate float[] with NaN value.
    Maybe native code pass wrong ref pointer.
     
  6. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
    Oops. HumanPose is struct , not class. If you create instance in function, function terminate object is dispeared. Because struct object is made in stack memory.
    If you use HumanPose then
    new HumanPose in class or new in coroutine.
     
  7. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
      1. using UnityEditor;
      2. using UnityEngine;

      3. [CustomEditor(typeof(Pose))]
      4. public class PoseEditor : Editor {

      5. HumanPoseHandler hph;
      6. HumanPose hp = new HumanPose();
      7. Avatar avatar;
      8. float[] defaultMuscles;
     
  8. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    192
    You must put animator.transform into rootbone transform parameter.

    Here is my sample code.

    using UnityEngine;
    using System.Collections;

    public class PoseDisplay : MonoBehaviour {

    HumanPose pose = new HumanPose();
    HumanPoseHandler handler;
    public Transform root;

    public float[] muscles;
    bool playing = false;

    public int index;

    IEnumerator PoseHandling()
    {
    pose = new HumanPose();
    while (playing )
    {
    yield return false;
    Debug.Log(HumanTrait.MuscleName[index] + " is " + pose.muscles[index]);

    }
    }

    // Use this for initialization
    void Start () {
    playing = true;
    muscles = new float[92];
    // StartCoroutine(PoseHandling());
    }

    void OnDestroy()
    {
    playing = false;
    }

    // Update is called once per frame
    void LateUpdate () {
    // pose.muscles = muscles;

    HumanPose targetPose = new HumanPose();
    targetPose.bodyPosition = root.position;
    targetPose.bodyRotation = root.rotation;
    targetPose.muscles = new float [ HumanTrait.MuscleName.Length];
    System.Array.Copy( muscles , targetPose.muscles , muscles.Length );

    if ( handler == null )
    {
    handler = new HumanPoseHandler(GetComponent<Animator>().avatar, root);
    }

    handler.SetHumanPose(ref targetPose);
    Debug.Log(HumanTrait.MuscleName[index] + " is " + targetPose.muscles[index]);


    }
    }
     
    Last edited: May 18, 2016
    samdutter and Zju-George like this.
  9. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
  10. NumesSanguis

    NumesSanguis

    Joined:
    Nov 9, 2017
    Posts:
    6