Morph 3D Tutorial Part 2: Changing blendshapes at runtime 18 comments

This tutrial’s got outdated with M3D 1.6 release. Might still be useful but not entirely accurate.

In the last part we set up our scene and added a character controller to our MCSMaleLite model. Now we can proceed to setting blendshapes at runtime.

First we need a UI and a slider to modify the blendshapes. Select Create -> UI -> Canvas in the hierarchy view. Now right click on the newly created canvas and select UI -> Slider. It should put a slider in the center of the UI. Select the slider in hierarchy view and click the anchor button then select the top left anchor. Also set the position to 120, -30, 0. Since the blendshape value for Morph 3D can take a number between 0 and 100 we need to set the Max Value in the Slider (Script) section in Inspectior view to 100.


Next thing we need is a script to communicate the slider change to MCSMaleLite model. Create a folder named “Scripts” under Assets in project view and create a C# Script in it named “UIController” then drag the script over the Canvas in hierarchy view. This will add the script to this object. Double click the script to open it in editor. First add MORPH3D import and two variables for holding the MCSModel and manager to it.

GameObject mcsModel will hold our MCSMaleLite reference, and manager will hold a reference to its M3DCharacterManager. In Start() method we need to get the manager reference from mmcsModel like this:

Unless we assign the MCSMaleLite to mcsModel beforehand as soon as we run the game you’ll see UnassignedReferenceException in Unity console. If you click on the Canvas object in hierarchy view and scroll down in the Inspector view you should see our UI Controller (Script). And conveniently enough you’ll see a field Mcs Model under it. This is our GameObject mcsModel in the script. It shows in Unity editor because we marked it with [SerializeField]. (Your other option is to make it public instead of private then you wouldn’t need the [SerializeField] but it is having your variables private when you can is always better.)

Drag the MCSMaleLite object from the hierarchy view to Mcs Model slot under UI Controller (Script) section.


Go back to C# editor and add this methodto UIController script:

Now we need to bind the value changed event of the slider to this public function. Go back to Unity editor and click Slider under the Canvas in hierarchy view. Scroll down in the Inspector view and find On Value Changed (Single) section in the bottom. Click the + at the bottom right. It will bring an Object slot. Drag the Canvas object from hierarchy to this slot. We are binding the Canvas object because the function we need to bind is in the UIController script that we added to the Canvas object. If you click the combobox you should see UIController in the list. select UIController -> SetBlendshapeValue (not the SetBlendshapeValue(float) under the Static Parameters section. That wouldn’t dynamically pass the slider value, instead it would provide you with a field to set the value statically)


Now run the project and move the slider. You should see that MCSMaleLite is updating the blendshape at runtime.

For the demonstration purposes this slider changes the FBMHeavy attribute statically. But you can easily add other sliders and other functions to alter different blendshapes. The blendshape names can be found under MORPH3D -> Resources folder. If you click on the M3D_BlendshapeNames you should see the list in the Inspector view. Actual blendshape name that you need to use is not the one in text fields but the labels on the left.


For convenience here is the complete UIController script:

In the next tutorial part we’ll have a look at adding the MCSModel dynamically to the scene at runtime. We’ll also add clothing and hair from prefabs again at runtime by scripts.

Links to the other parts of the tutorial:

[Total: 1    Average: 5/5]
  • Hi,

    There is a method in manager, you can do something like:

    int currentValue = manager.GetBlendshapeByID(“id”).currentValue;

    I am not sure if the id string is the same with the name string for set function though. There is also a manager.GetAllBlendshapes() method. You can get the list and check the names/ids in a loop.

    MCS 2.0 is supposed to be released soon (for quite some time). Keep in mind that these could be changed with 2.0.

    Take care.

    • Raj Sharma

      Hey, thanks for the answer. I’d a hard time cracking this myself. I wanted to know if there is a place where all the “noob” queries are already answered. For Morph3D I mean. I’ve seen the documentation already and it’s pretty hard to navigate.

  • Raj Sharma

    Hey, this is awesome! I have one more question though, how do I “get” the morph value at runtime? I can’t find a function similar to SetBlendshapeValue (“FBMHeavy”, weightVal); which instead of setting, returns the value. Maybe something like GetBlendshapeValue (“FBMHeavy”);

  • Jess

    Thanks this really helped. How to save this data so when I move to another scene the character will be saved as the player?

    • To save your character data (like morphs or current equipment etc.) you do whatever you do for other things that you need to keep between scenes. There is no MCS way of storing it. But you can take a look at this.

  • DavidP

    When I try this code (with blendshape PHMMouthSmileOpen set to 50), the effect persists only momentarily, perhaps a single frame. Is it necessary to set blendshape in Update() to persist such changes?

  • Is there any list with all the blend shape ids ? for example afraid , confused and such.

    • DavidP

      In the Hierarchy pane, select MCSFemale, and then over in its inspector pane, open “M3D Character Manager (Script)”. Then open Blendshape Groups and any group under that.

  • Alexey

    How to make a lot of Slider
    Set an example please

    • You need to add more sliders to UI and attach a new method to change the respective morph. Each new slider would require another method.

      It could be possible to generate sliders at runtime against a blendshape list but positioning at runtime would be a pain.

      • Marek Adamczyk

        Positioning will not be a pain if you use Scroll Rect and layout groups 🙂

  • Pingback: Morph 3D Tutorial Part 3: Adding MCSMaleLite to scene and adding / changing clothes at runtime | o3n Games()

  • Pingback: Morph 3D Tutorial | o3n Games()

  • Pingback: Morph 3D Tutorial Part 1: Setting up the scene | o3n Games()