Skip to content

Commit

Permalink
Merge pull request #91 from uezo/develop
Browse files Browse the repository at this point in the history
Change path to face configuration file to relative path #89
  • Loading branch information
uezo authored Nov 15, 2020
2 parents 9b5d439 + 6f38dc8 commit f790d8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
51 changes: 33 additions & 18 deletions ChatdollKit/Editor/FaceClipEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -13,6 +13,8 @@
public class FaceClipEditor : Editor
{
// For face configuration
private string previousJsonPath;
private string currentJsonPath;
private List<FaceClip> faceClips;
private int previousSelectedFaceIndex;
private int selectedFaceIndex;
Expand All @@ -33,7 +35,10 @@ public override void OnInspectorGUI()

if (skinnedMeshRenderer != null)
{
if (string.IsNullOrEmpty(modelController.FaceConfigurationFile) || !File.Exists(modelController.FaceConfigurationFile))
previousJsonPath = currentJsonPath;
currentJsonPath = Path.Combine(Application.dataPath, modelController.FaceConfigurationFile);

if (string.IsNullOrEmpty(modelController.FaceConfigurationFile) || !File.Exists(currentJsonPath))
{
EditorGUILayout.HelpBox("Create new face configuration file to use face capture tool.", MessageType.Info, true);
if (GUILayout.Button("Create"))
Expand All @@ -44,6 +49,10 @@ public override void OnInspectorGUI()
if (!string.IsNullOrEmpty(newFilePath))
{
File.WriteAllText(newFilePath, JsonConvert.SerializeObject(new List<FaceClip>()));
if (newFilePath.StartsWith(Application.dataPath))
{
newFilePath = newFilePath.Substring(Application.dataPath.Length + 1);
}
modelController.FaceConfigurationFile = newFilePath;
}
}
Expand All @@ -55,18 +64,22 @@ public override void OnInspectorGUI()
return;
}

var path = modelController.FaceConfigurationFile;

// Reset flag to determine selection changed
var selectionChanged = false;

// Initial load
if (faceClips == null)
if (faceClips == null || currentJsonPath != previousJsonPath)
{
faceClips = GetFacesFromFile(path);
faceClips = GetFacesFromFile(currentJsonPath);
if (faceClips.Count > 0)
{
selectionChanged = true;
selectedFaceIndex = 0;
}
else
{
selectedFaceIndex = -1;
currentFaceName = string.Empty;
}
}

Expand All @@ -83,7 +96,7 @@ public override void OnInspectorGUI()
// Remove face
if (GUILayout.Button("Remove", ButtonLayout))
{
if (faceClips.Count > 0 && RemoveFace(path, faceClips[selectedFaceIndex].Name))
if (faceClips.Count > 0 && RemoveFace(currentJsonPath, faceClips[selectedFaceIndex].Name))
{
if (faceClips.Count == 0)
{
Expand All @@ -109,20 +122,19 @@ public override void OnInspectorGUI()

// Add or update face
EditorGUILayout.BeginHorizontal();

currentFaceName = GUILayout.TextField(currentFaceName);

EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(currentFaceName) || string.IsNullOrEmpty(currentFaceName.Trim()));
if (GUILayout.Button("Capture", ButtonLayout))
{
// Update and save FaceClip with current weights of SkinnedMeshRenderer
if (UpdateFace(path, new FaceClip(currentFaceName, skinnedMeshRenderer)))
if (!string.IsNullOrEmpty(currentFaceName.Trim()))
{
// Change selected index to new item
selectedFaceIndex = faceClips.Select(f => f.Name).ToList().IndexOf(currentFaceName);
// Update and save FaceClip with current weights of SkinnedMeshRenderer
if (UpdateFace(currentJsonPath, new FaceClip(currentFaceName, skinnedMeshRenderer)))
{
// Change selected index to new item
selectedFaceIndex = faceClips.Select(f => f.Name).ToList().IndexOf(currentFaceName);
}
}
}
EditorGUI.EndDisabledGroup();
EditorGUILayout.EndHorizontal();

// Change current name and blend shapes when selection changed
Expand Down Expand Up @@ -208,8 +220,11 @@ private List<FaceClip> GetFacesFromFile(string path)

try
{
var facesJson = File.ReadAllText(path);
storedFaces = JsonConvert.DeserializeObject<List<FaceClip>>(facesJson);
var faceJsonString = File.ReadAllText(path);
if (faceJsonString != null)
{
storedFaces = JsonConvert.DeserializeObject<List<FaceClip>>(faceJsonString);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -491,4 +506,4 @@ class VisemeTarget
public SkinnedMeshRenderer SkinnedMeshRenderer { get; set; }
public List<int> ShapeKeyIndexes { get; set; }
}
}
}
8 changes: 7 additions & 1 deletion ChatdollKit/Scripts/Model/ModelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,13 @@ public void AddFace(string name, Dictionary<string, float> weights, bool asDefau
// Load faces from config file
public void LoadFacesFromFile(string configFilePath = null)
{
var path = configFilePath ?? FaceConfigurationFile;
var path = !string.IsNullOrEmpty(configFilePath) ? configFilePath : Application.dataPath + "/" + FaceConfigurationFile;
if (!File.Exists(path))
{
Debug.LogWarning("Face configuration file does not exist: " + path);
return;
}

foreach (var faceClip in JsonConvert.DeserializeObject<List<FaceClip>>(File.ReadAllText(path)))
{
var asDefault = faceClip.Name.ToLower() == "default" || faceClip.Name.ToLower() == "neutral";
Expand Down

0 comments on commit f790d8e

Please sign in to comment.