RemapAnimationBindingsTool.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR && UNITY_IMGUI
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEditorInternal;
  8. using UnityEngine;
  9. namespace Animancer.Editor.Tools
  10. {
  11. /// <summary>[Editor-Only] [Pro-Only]
  12. /// A <see cref="AnimancerToolsWindow.Tool"/> for changing which bones an <see cref="AnimationClip"/>s controls.
  13. /// </summary>
  14. /// <remarks>
  15. /// <strong>Documentation:</strong>
  16. /// <see href="https://kybernetik.com.au/animancer/docs/manual/tools/remap-animation-bindings">
  17. /// Remap Animation Bindings</see>
  18. /// </remarks>
  19. /// https://kybernetik.com.au/animancer/api/Animancer.Editor.Tools/RemapAnimationBindingsTool
  20. ///
  21. [Serializable]
  22. public class RemapAnimationBindingsTool : AnimationModifierTool
  23. {
  24. /************************************************************************************************************************/
  25. [SerializeField] private List<string> _NewBindingPaths;
  26. [NonSerialized] private List<List<EditorCurveBinding>> _BindingGroups;
  27. [NonSerialized] private List<string> _OldBindingPaths;
  28. [NonSerialized] private bool _OldBindingPathsAreDirty;
  29. [NonSerialized] private ReorderableList _OldBindingPathsDisplay;
  30. [NonSerialized] private ReorderableList _NewBindingPathsDisplay;
  31. /************************************************************************************************************************/
  32. /// <inheritdoc/>
  33. public override int DisplayOrder => 5;
  34. /// <inheritdoc/>
  35. public override string Name => "Remap Animation Bindings";
  36. /// <inheritdoc/>
  37. public override string HelpURL => Strings.DocsURLs.RemapAnimationBindings;
  38. /// <inheritdoc/>
  39. public override string Instructions
  40. {
  41. get
  42. {
  43. if (Animation == null)
  44. return "Select the animation you want to remap.";
  45. if (_OldBindingPaths.Count == 0)
  46. {
  47. if (Animation.humanMotion)
  48. return "The selected animation only has Humanoid bindings which cannot be remapped.";
  49. return "The selected animation does not have any bindings.";
  50. }
  51. return "Enter the new paths to change the bindings into then click Save As.";
  52. }
  53. }
  54. /************************************************************************************************************************/
  55. /// <inheritdoc/>
  56. public override void OnEnable(int index)
  57. {
  58. base.OnEnable(index);
  59. _BindingGroups = new();
  60. _OldBindingPaths = new();
  61. _NewBindingPaths ??= new();
  62. if (Animation == null)
  63. _NewBindingPaths.Clear();
  64. _OldBindingPathsDisplay = AnimancerToolsWindow.CreateReorderableStringList(_OldBindingPaths, "Old Binding Paths");
  65. _NewBindingPathsDisplay = AnimancerToolsWindow.CreateReorderableStringList(_NewBindingPaths, "New Binding Paths", (area, i) =>
  66. {
  67. var color = GUI.color;
  68. var path = _NewBindingPaths[i];
  69. if (path != _OldBindingPaths[i])
  70. GUI.color = new(0.15f, 0.7f, 0.15f, 1);
  71. path = EditorGUI.TextField(area, path);
  72. GUI.color = color;
  73. return path;
  74. });
  75. }
  76. /************************************************************************************************************************/
  77. /// <inheritdoc/>
  78. protected override void OnAnimationChanged()
  79. {
  80. base.OnAnimationChanged();
  81. _OldBindingPathsAreDirty = true;
  82. }
  83. /************************************************************************************************************************/
  84. /// <inheritdoc/>
  85. public override void DoBodyGUI()
  86. {
  87. base.DoBodyGUI();
  88. GatherBindings();
  89. GUILayout.BeginHorizontal();
  90. {
  91. GUILayout.BeginVertical();
  92. GUI.enabled = false;
  93. _OldBindingPathsDisplay.DoLayoutList();
  94. GUI.enabled = true;
  95. GUILayout.EndVertical();
  96. GUILayout.BeginVertical();
  97. _NewBindingPathsDisplay.DoLayoutList();
  98. GUILayout.EndVertical();
  99. }
  100. GUILayout.EndHorizontal();
  101. GUI.enabled = Animation != null;
  102. GUILayout.BeginHorizontal();
  103. {
  104. GUILayout.FlexibleSpace();
  105. if (GUILayout.Button("Reset"))
  106. {
  107. AnimancerGUI.Deselect();
  108. AnimancerToolsWindow.RecordUndo();
  109. _NewBindingPaths.Clear();
  110. _OldBindingPathsAreDirty = true;
  111. }
  112. if (GUILayout.Button("Copy All"))
  113. {
  114. AnimancerGUI.Deselect();
  115. CopyAll();
  116. }
  117. if (GUILayout.Button("Paste All"))
  118. {
  119. AnimancerGUI.Deselect();
  120. PasteAll();
  121. }
  122. if (GUILayout.Button("Save As"))
  123. {
  124. if (SaveAs())
  125. {
  126. _OldBindingPathsAreDirty = true;
  127. }
  128. }
  129. }
  130. GUILayout.EndHorizontal();
  131. }
  132. /************************************************************************************************************************/
  133. /// <summary>Gathers the bindings from the <see cref="AnimationModifierTool.Animation"/>.</summary>
  134. private void GatherBindings()
  135. {
  136. if (!_OldBindingPathsAreDirty)
  137. return;
  138. _OldBindingPathsAreDirty = false;
  139. _BindingGroups.Clear();
  140. _OldBindingPaths.Clear();
  141. if (Animation == null)
  142. {
  143. _NewBindingPaths.Clear();
  144. return;
  145. }
  146. var isHumanoid = Animation.humanMotion;
  147. AnimationBindings.OnAnimationChanged(Animation);
  148. var bindings = AnimationBindings.GetBindings(Animation);
  149. Array.Sort(bindings, (a, b) =>
  150. {
  151. var result = EditorUtility.NaturalCompare(a.path, b.path);
  152. if (result != 0)
  153. return result;
  154. return EditorUtility.NaturalCompare(a.propertyName, b.propertyName);
  155. });
  156. string previousPath = null;
  157. List<EditorCurveBinding> previousGroup = null;
  158. for (int i = 0; i < bindings.Length; i++)
  159. {
  160. var binding = bindings[i];
  161. if (isHumanoid &&
  162. string.IsNullOrEmpty(binding.path) &&
  163. IsHumanoidBinding(binding.propertyName))
  164. continue;
  165. var path = binding.path;
  166. if (path == previousPath)
  167. {
  168. previousGroup.Add(binding);
  169. continue;
  170. }
  171. previousPath = path;
  172. previousGroup = new() { binding };
  173. _BindingGroups.Add(previousGroup);
  174. _OldBindingPaths.Add(path);
  175. if (_NewBindingPaths.Count < _OldBindingPaths.Count)
  176. _NewBindingPaths.Add(path);
  177. }
  178. if (_NewBindingPaths.Count > _OldBindingPaths.Count)
  179. _NewBindingPaths.RemoveRange(_OldBindingPaths.Count, _NewBindingPaths.Count - _OldBindingPaths.Count);
  180. }
  181. /************************************************************************************************************************/
  182. private static HashSet<string> _HumanoidBindingNames;
  183. /// <summary>Is the `propertyName` one of the bindings used by Humanoid animations?</summary>
  184. private static bool IsHumanoidBinding(string propertyName)
  185. {
  186. _HumanoidBindingNames ??= new()
  187. {
  188. "RootT.x", "RootT.y", "RootT.z",
  189. "RootQ.x", "RootQ.y", "RootQ.z", "RootQ.w",
  190. "LeftFootT.x", "LeftFootT.y", "LeftFootT.z",
  191. "LeftFootQ.x", "LeftFootQ.y", "LeftFootQ.z", "LeftFootQ.w",
  192. "RightFootT.x", "RightFootT.y", "RightFootT.z",
  193. "RightFootQ.x", "RightFootQ.y", "RightFootQ.z", "RightFootQ.w",
  194. "LeftHandT.x", "LeftHandT.y", "LeftHandT.z",
  195. "LeftHandQ.x", "LeftHandQ.y", "LeftHandQ.z", "LeftHandQ.w",
  196. "RightHandT.x", "RightHandT.y", "RightHandT.z",
  197. "RightHandQ.x", "RightHandQ.y", "RightHandQ.z", "RightHandQ.w",
  198. "Spine Front-Back", "Spine Left-Right", "Spine Twist Left-Right",
  199. "Chest Front-Back", "Chest Left-Right", "Chest Twist Left-Right",
  200. "UpperChest Front-Back", "UpperChest Left-Right", "UpperChest Twist Left-Right",
  201. "Neck Nod Down-Up", "Neck Tilt Left-Right", "Neck Turn Left-Right",
  202. "Head Nod Down-Up", "Head Tilt Left-Right", "Head Turn Left-Right",
  203. "Left Eye Down-Up", "Left Eye In-Out",
  204. "Right Eye Down-Up", "Right Eye In-Out",
  205. "Jaw Close", "Jaw Left-Right",
  206. "Left Upper Leg Front-Back", "Left Upper Leg In-Out", "Left Upper Leg Twist In-Out",
  207. "Left Lower Leg Stretch", "Left Lower Leg Twist In-Out",
  208. "Left Foot Up-Down", "Left Foot Twist In-Out",
  209. "Left Toes Up-Down",
  210. "Right Upper Leg Front-Back", "Right Upper Leg In-Out", "Right Upper Leg Twist In-Out",
  211. "Right Lower Leg Stretch", "Right Lower Leg Twist In-Out",
  212. "Right Foot Up-Down", "Right Foot Twist In-Out",
  213. "Right Toes Up-Down",
  214. "Left Shoulder Down-Up", "Left Shoulder Front-Back",
  215. "Left Arm Down-Up", "Left Arm Front-Back", "Left Arm Twist In-Out",
  216. "Left Forearm Stretch", "Left Forearm Twist In-Out",
  217. "Left Hand Down-Up", "Left Hand In-Out",
  218. "Right Shoulder Down-Up", "Right Shoulder Front-Back",
  219. "Right Arm Down-Up", "Right Arm Front-Back", "Right Arm Twist In-Out",
  220. "Right Forearm Stretch", "Right Forearm Twist In-Out",
  221. "Right Hand Down-Up", "Right Hand In-Out",
  222. "LeftHand.Thumb.Spread", "LeftHand.Thumb.1 Stretched", "LeftHand.Thumb.2 Stretched", "LeftHand.Thumb.3 Stretched",
  223. "LeftHand.Index.Spread", "LeftHand.Index.1 Stretched", "LeftHand.Index.2 Stretched", "LeftHand.Index.3 Stretched",
  224. "LeftHand.Middle.Spread", "LeftHand.Middle.1 Stretched", "LeftHand.Middle.2 Stretched", "LeftHand.Middle.3 Stretched",
  225. "LeftHand.Ring.Spread", "LeftHand.Ring.1 Stretched", "LeftHand.Ring.2 Stretched", "LeftHand.Ring.3 Stretched",
  226. "LeftHand.Little.Spread", "LeftHand.Little.1 Stretched", "LeftHand.Little.2 Stretched", "LeftHand.Little.3 Stretched",
  227. "RightHand.Thumb.Spread", "RightHand.Thumb.1 Stretched", "RightHand.Thumb.2 Stretched", "RightHand.Thumb.3 Stretched",
  228. "RightHand.Index.Spread", "RightHand.Index.1 Stretched", "RightHand.Index.2 Stretched", "RightHand.Index.3 Stretched",
  229. "RightHand.Middle.Spread", "RightHand.Middle.1 Stretched", "RightHand.Middle.2 Stretched", "RightHand.Middle.3 Stretched",
  230. "RightHand.Ring.Spread", "RightHand.Ring.1 Stretched", "RightHand.Ring.2 Stretched", "RightHand.Ring.3 Stretched",
  231. "RightHand.Little.Spread", "RightHand.Little.1 Stretched", "RightHand.Little.2 Stretched", "RightHand.Little.3 Stretched",
  232. };
  233. return _HumanoidBindingNames.Contains(propertyName);
  234. }
  235. /************************************************************************************************************************/
  236. /// <summary>Copies all of the <see cref="_NewBindingPaths"/> to the system clipboard.</summary>
  237. private void CopyAll()
  238. {
  239. var text = StringBuilderPool.Instance.Acquire();
  240. for (int i = 0; i < _NewBindingPaths.Count; i++)
  241. {
  242. text.AppendLine(_NewBindingPaths[i]);
  243. }
  244. EditorGUIUtility.systemCopyBuffer = text.ReleaseToString();
  245. }
  246. /// <summary>Pastes the string from the system clipboard into the <see cref="_NewBindingPaths"/>.</summary>
  247. private void PasteAll()
  248. {
  249. using var reader = new StringReader(EditorGUIUtility.systemCopyBuffer);
  250. for (int i = 0; i < _NewBindingPaths.Count; i++)
  251. {
  252. var line = reader.ReadLine();
  253. if (line == null)
  254. return;
  255. _NewBindingPaths[i] = line;
  256. }
  257. }
  258. /************************************************************************************************************************/
  259. /// <inheritdoc/>
  260. protected override void Modify(AnimationClip animation)
  261. {
  262. for (int iGroup = 0; iGroup < _BindingGroups.Count; iGroup++)
  263. {
  264. var oldPath = _OldBindingPaths[iGroup];
  265. var newPath = _NewBindingPaths[iGroup];
  266. if (oldPath == newPath)
  267. continue;
  268. var group = _BindingGroups[iGroup];
  269. for (int iBinding = 0; iBinding < group.Count; iBinding++)
  270. {
  271. var binding = group[iBinding];
  272. if (binding.isPPtrCurve)
  273. {
  274. var curve = AnimationUtility.GetObjectReferenceCurve(animation, binding);
  275. AnimationUtility.SetObjectReferenceCurve(animation, binding, null);
  276. binding.path = newPath;
  277. AnimationUtility.SetObjectReferenceCurve(animation, binding, curve);
  278. }
  279. else
  280. {
  281. var curve = AnimationUtility.GetEditorCurve(animation, binding);
  282. AnimationUtility.SetEditorCurve(animation, binding, null);
  283. binding.path = newPath;
  284. AnimationUtility.SetEditorCurve(animation, binding, curve);
  285. }
  286. }
  287. }
  288. }
  289. /************************************************************************************************************************/
  290. }
  291. }
  292. #endif