Search Unity

Blendshapes to sliders automatically

Discussion in 'Scripting' started by DigitalAdam, Jan 19, 2019.

  1. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,209
    Is it possible and does anyone have a script that will view the blendshapes on a mesh, and automatically convert them to sliders? I'm not a programmer, so any help would greatly be appreciated.

    Thanks!
     
  2. x1alphaz1

    x1alphaz1

    Joined:
    Dec 19, 2020
    Posts:
    23
    Hey
    did u find a solution ?
     
    Bauschen likes this.
  3. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,209
    I did not unfortunately.
     
  4. x1alphaz1

    x1alphaz1

    Joined:
    Dec 19, 2020
    Posts:
    23
    ouch!!! i know it's 2 years later do u still need it ?
     
    Last edited: Feb 18, 2021
    Bauschen likes this.
  5. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,209
    Haha I lived without it for all that time... but Im sure me as well as others could use it!
     
  6. x1alphaz1

    x1alphaz1

    Joined:
    Dec 19, 2020
    Posts:
    23
    sure mate:
    just to mention i have 1000s of blendshapes so i kinda needed some form of database to categorise them so i used a simple csv file to achieve that.
    the steps i followed:
    1-save blendshapes into a file. either manually or by script cose later i might be reading them from a server or something to allow enable/disable the ones u need without having having to update the game.

    2- Function to load the text file
    Code (CSharp):
    1.    private string[] loadTextFile(string fileName)
    2.     {
    3.         //string path = "Assets/data/Morphnames.txt";
    4.         string path = fileName;
    5.         string[] lines = System.IO.File.ReadAllLines(path);
    6.         return lines;
    7.     }
    3- load the textfile in memory
    Code (CSharp):
    1. textFile = loadTextFile("Assets/data/Morphnames.csv");
    4- loop through the lines one by one and instantiate a slider prefab onto the GUI using the 2 functions below:

    first function is to loop through the actual blendshapes for that character to match the ones that exist in the previously loaded textfile. if they do exist, we instantiate our slider prefab.

    Code (CSharp):
    1. public string[] getBlendShapeNames(GameObject obj, string category)
    2.     {
    3.         SkinnedMeshRenderer body = obj.GetComponent<SkinnedMeshRenderer>();
    4.         Mesh m = body.sharedMesh;
    5.         string[] arr;
    6.         arr = new string[m.blendShapeCount];
    7.         for (int i = 0; i < m.blendShapeCount; i++)
    8.         {
    9.             string s = m.GetBlendShapeName(i);
    10.             //print("Blend Shape: " + i + " " + s); // Blend Shape: 4 FightingLlamaStance
    11.             arr[i] = s;
    12.             createUIObject(m.GetBlendShapeName(i), i, category);
    13.         }
    14.         bool ss = WriteLine("./", "BodyBlendshapes.txt", arr);
    15.         return arr;
    16.     }


    The createUIObject simply loads a previously created prefab from the assets and assigns the values as well as read the current values from blendshapes.
    Code (CSharp):
    1. private void createUIObject(string morphName, int morphid, string category)
    2.     {
    3.       string[] line = new string[6];  //6 columns in the csv file
    4.  
    5.        //colunms in the csv textfile
    6.         int findex;
    7.         string fmorphname = "";
    8.         string ffriendlyname = "";
    9.         string fCategory = "";
    10.         string fCTRL_Type = ""; if u wanna have toggles, sliders or colorpickers
    11.         int fenabled = 0, fminValue = 0, fMaxValue = 0, fCurValue = 0;
    12.  
    13.         for (int i = 0; i < textFile.Length; i++)
    14.         {
    15.             line = textFile[i].Split(',');
    16.             if (line[1] == morphName) //match morphname
    17.             {
    18.                 findex = Int32.Parse(line[0]);
    19.                 fmorphname = line[1];
    20.                 ffriendlyname = line[2];
    21.                 fenabled = Int32.Parse(line[3]);
    22.                 fCategory = line[4];
    23.                 fCTRL_Type = line[5];
    24.                 fminValue = Int32.Parse(line[6]);
    25.                 fMaxValue = Int32.Parse(line[7]);
    26.                 fCurValue = Int32.Parse(line[8]);
    27.                 //print("fmorphname : " + fmorphname + " " + fCategory);
    28.             }
    29.         }
    30.  
    31.         if (fCategory != category)
    32.         {
    33.             return;
    34.         }
    35.         //if (fCTRL_Type == "slider")
    36.         {
    37.             GameObject newUIObject = Instantiate(PrefabSlider, this.transform);
    38.             newUIObject.name = "s_" + ffriendlyname;
    39.             Slider s = GameObject.Find(newUIObject.name + "/Slider").GetComponent<Slider>();
    40.             s.minValue = fminValue;
    41.             s.maxValue = fMaxValue;
    42.             s.value = skinMesh.GetBlendShapeWeight(morphid);
    43.            TMPro.TextMeshProUGUI t = GameObject.Find(newUIObject.name + "/Text_TMP").GetComponent<TMPro.TextMeshProUGUI>();
    44.             t.text = ffriendlyname;
    45.             newUIObject.transform.SetParent(MainLeftPanel.transform, false);
    46.             s.onValueChanged.AddListener( // creating the listener function
    47.                 delegate {
    48.                     skinMesh.SetBlendShapeWeight(morphid, s.value);
    49.                 });
    50.         }
    51.     }
    sample csv file:
    Code (CSharp):
    1. index,MorphName,friendlyName,enabled,Category,CTRL_Type,minValue,MaxValue,curValue
    2. 0,bodyheight, Height,0,none,slider,-100,100,0
    3. 1,armslength,Arms Length,0,none,slider,-100,100,0

    i know it's bit messy but saves alot of time... someone can actually makes it faster by optimizing the code but for now it's basically working and takes around 1 sec to load 800 blendshapes
     
    Bauschen likes this.
  7. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,209
    Thanks for this. I'll give it a shot when I have time!