Automatically disabling model's facial bones for body-only animations,Disabling bones for non-facial animations automatically
Hi!
I have hundred of high-quality models (from renderpeople.com) that come originally with animations that move both the body and the face of the models. I also want to play animations (from mixamo.com) on these models. The thing is that these animations are limited to the body and not the facial bones. Therefore when playing the animations, the model's face always has a stupid default pose like for this example model.
Now I found out that disabling some transforms (neck, left eye, right eye, jaw) from the Avatar Definition makes the model's default expression much nicer but iterating manually through hundred of models really isn't that smart.
I'm working on an editor script to import all these models and configure them in one click and I'm stuck on this point. Does anyone have an idea of how to do it?
So far my approach was to create a copy of each model, this time with avatar with these bones disabled but for some reason my modifications aren't taken into account and the avatar stays the same. If you know why this change isn't taken into account or have a better approach I would be very grateful.
By the way, I had to comment the neck from the list of skeletonNamesToRemove because AvatarBuilder.BuildHumanAvatar would log an error message saying that the neck is required. That's strange since it can be manually disabled without problem from the avatar window.
Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
[ExecuteInEditMode]
public class CharactersImportation : MonoBehaviour
{
public Transform InstantiatedCharactersParent = null;
public float InstantiationDist = 1.5f;
static int instantiatedObjectIndex = 0;
private string[] skeletonNamesToRemove = new string[] {/*"neck",*/ "eye_l", "eye_r", "jaw"};
private string[] humanNamesToRemove = new string[] {"Neck", "LeftEye", "RightEye", "Jaw"};
public void CreateNonFaciallyAnimatedAvatarsForAllCharacters()
{
string mainFolder = "Assets/RP_Character";
Debug.Log("Starting procedure");
CreateNonFaciallyAnimatedAvatarsRec(mainFolder);
}
void CreateNonFaciallyAnimatedAvatarsRec(string path)
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
foreach (string f in files)
if (f.Contains("FixedExpression"))//skip the ones already done manually
{
return;
}
foreach (string f in files)
{
if (instantiatedObjectIndex >= 1)//while testing, limits this script to the first character of the list
return;
if (Path.GetExtension(f) == ".fbx")
{
string FixedExpressionName = f.Substring(0, f.Length - 4) + "_FixedExpression.fbx";
if (!AssetDatabase.CopyAsset(f, FixedExpressionName))//Substring -4 is to remove ".fbx"
Debug.LogError($"Failed to copy {f}");
else
Debug.Log($"Created {f + "_FixedExpression"}");
instantiatedObjectIndex++;
GameObject g = AssetDatabase.LoadAssetAtPath(FixedExpressionName, typeof(GameObject)) as GameObject;
Avatar FEA = g.GetComponent<Animator>().avatar;
Debug.Log("Found avatar named: " + FEA.name);
HumanBone[] headlessHuman = new HumanBone[FEA.humanDescription.human.Length - humanNamesToRemove.Length];
int copiedBonesIndex = 0;
foreach (HumanBone b in FEA.humanDescription.human)
{
bool badBoneFound = false;
foreach (string bone in humanNamesToRemove)
{
if (bone == b.humanName)
{
//Debug.LogWarning("Found bone to discard: " + b.humanName);
badBoneFound = true;
}
}
if (badBoneFound == false)
{
//Debug.Log("Acceptable will be copied at index" + copiedBonesIndex);
headlessHuman[copiedBonesIndex] = new HumanBone
{
humanName = b.humanName,
boneName = b.boneName,
limit = b.limit
};
copiedBonesIndex++;
}
}
//////////////////////////////////////////////////////// skeleton
SkeletonBone[] headlessSkeleton = new SkeletonBone[FEA.humanDescription.skeleton.Length - skeletonNamesToRemove.Length];
copiedBonesIndex = 0;
foreach (SkeletonBone b in FEA.humanDescription.skeleton)
{
bool badBoneFound = false;
foreach (string bone in skeletonNamesToRemove)
{
if (bone == b.name)
{
//Debug.LogWarning("Found bone to discard: " + b.humanName);
badBoneFound = true;
}
}
if (badBoneFound == false)
{
//Debug.Log("Acceptable will be copied at index" + copiedBonesIndex);
headlessSkeleton[copiedBonesIndex] = new SkeletonBone
{
name = b.name,
position = b.position,
rotation = b.rotation,
scale = b.scale
};
//Debug.Log("Acceptable bone copied " + headlessBones[copiedBonesIndex].humanName);
copiedBonesIndex++;
}
}
HumanDescription description = new HumanDescription()
{
armStretch = FEA.humanDescription.armStretch,
feetSpacing = FEA.humanDescription.feetSpacing,
hasTranslationDoF = FEA.humanDescription.hasTranslationDoF,
legStretch = FEA.humanDescription.legStretch,
lowerArmTwist = FEA.humanDescription.lowerArmTwist,
lowerLegTwist = FEA.humanDescription.lowerLegTwist,
upperArmTwist = FEA.humanDescription.upperArmTwist,
upperLegTwist = FEA.humanDescription.upperLegTwist,
skeleton = headlessSkeleton,
human = headlessHuman,
};
FEA = AvatarBuilder.BuildHumanAvatar(g, description);
g.GetComponent<Animator>().avatar = FEA;
AssetDatabase.SaveAssets();
}
}
foreach (string d in dirs)
{
if (d.Contains("_rigged_"))//check this is a character sub folder and not something related to animations or something else
CreateNonFaciallyAnimatedAvatarsRec(d);
}
}
}