From 84c097e95d25b320c426656f75de8ab53666433c Mon Sep 17 00:00:00 2001 From: uezo Date: Sun, 15 Nov 2020 22:26:32 +0900 Subject: [PATCH] Change path to face configuration file to relative path #89 - Before: Absolute path (full path) - After: relative path from `Application.dataPath` NOTE: Developer should change the value of `FaceConfigurationFile` like below (e.g. MacOS): - Before: /Users/path/to/project/Assets/Resources/faceConfig.json - After: Resources/faceConfig.json --- ChatdollKit/Editor/FaceClipEditor.cs | 34 ++++++++++++++------ ChatdollKit/Scripts/Model/ModelController.cs | 8 ++++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ChatdollKit/Editor/FaceClipEditor.cs b/ChatdollKit/Editor/FaceClipEditor.cs index e44666c..5a6a6a3 100644 --- a/ChatdollKit/Editor/FaceClipEditor.cs +++ b/ChatdollKit/Editor/FaceClipEditor.cs @@ -13,6 +13,8 @@ public class FaceClipEditor : Editor { // For face configuration + private string previousJsonPath; + private string currentJsonPath; private List faceClips; private int previousSelectedFaceIndex; private int selectedFaceIndex; @@ -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")) @@ -44,6 +49,10 @@ public override void OnInspectorGUI() if (!string.IsNullOrEmpty(newFilePath)) { File.WriteAllText(newFilePath, JsonConvert.SerializeObject(new List())); + if (newFilePath.StartsWith(Application.dataPath)) + { + newFilePath = newFilePath.Substring(Application.dataPath.Length + 1); + } modelController.FaceConfigurationFile = newFilePath; } } @@ -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; } } @@ -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) { @@ -115,7 +128,7 @@ public override void OnInspectorGUI() if (!string.IsNullOrEmpty(currentFaceName.Trim())) { // Update and save FaceClip with current weights of SkinnedMeshRenderer - if (UpdateFace(path, new FaceClip(currentFaceName, skinnedMeshRenderer))) + if (UpdateFace(currentJsonPath, new FaceClip(currentFaceName, skinnedMeshRenderer))) { // Change selected index to new item selectedFaceIndex = faceClips.Select(f => f.Name).ToList().IndexOf(currentFaceName); @@ -207,8 +220,11 @@ private List GetFacesFromFile(string path) try { - var facesJson = File.ReadAllText(path); - storedFaces = JsonConvert.DeserializeObject>(facesJson); + var faceJsonString = File.ReadAllText(path); + if (faceJsonString != null) + { + storedFaces = JsonConvert.DeserializeObject>(faceJsonString); + } } catch (Exception ex) { diff --git a/ChatdollKit/Scripts/Model/ModelController.cs b/ChatdollKit/Scripts/Model/ModelController.cs index 9e119d5..884b541 100644 --- a/ChatdollKit/Scripts/Model/ModelController.cs +++ b/ChatdollKit/Scripts/Model/ModelController.cs @@ -612,7 +612,13 @@ public void AddFace(string name, Dictionary 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>(File.ReadAllText(path))) { var asDefault = faceClip.Name.ToLower() == "default" || faceClip.Name.ToLower() == "neutral";