Advertisement
xra

Fix Masks

xra
Mar 6th, 2016
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. //saves you from agonizing manual clicking "Fix Mask" on each user-created clip,
  2. //when you have updated/edited the original FBX changing the skeleton hierarchy
  3.  
  4. //drop this in your Scripts/Editor folder,
  5. //select FBX's and right click context menu Fix Animation Masks
  6. //tested on Unity 4.7
  7.  
  8. //minor update, sets all transforms of the avatarMask to active (assuming you are just using 'Mask Definition Create From This Model' with all transforms active (Default behavior)
  9.  
  10. using UnityEditor;
  11. using UnityEditorInternal;
  12. using UnityEngine;
  13. using System;
  14. using System.Reflection;
  15.  
  16. public class FixMask
  17. {
  18. [MenuItem("Assets/Fix Animation Masks")]
  19. private static void Init()
  20. {
  21. UnityEngine.Object[] selection = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
  22. foreach( UnityEngine.Object obj in selection )
  23. {
  24. string path = AssetDatabase.GetAssetPath( obj );
  25. ModelImporter mi = AssetImporter.GetAtPath(path) as ModelImporter;
  26.  
  27. Type modelImporterType = typeof(ModelImporter);
  28.  
  29. MethodInfo updateTransformMaskMethodInfo = modelImporterType.GetMethod("UpdateTransformMask", BindingFlags.NonPublic | BindingFlags.Static);
  30.  
  31. ModelImporterClipAnimation[] clipAnimations = mi.clipAnimations;
  32. SerializedObject so = new SerializedObject(mi);
  33. SerializedProperty clips = so.FindProperty("m_ClipAnimations");
  34.  
  35. AvatarMask avatarMask = new AvatarMask();
  36. avatarMask.transformCount = mi.transformPaths.Length;
  37. for( int i=0; i<mi.transformPaths.Length; i++ )
  38. {
  39. avatarMask.SetTransformPath(i,mi.transformPaths[i]);
  40. avatarMask.SetTransformActive(i,true);
  41. }
  42.  
  43. for( int i=0; i<clipAnimations.Length; i++ )
  44. {
  45. SerializedProperty transformMaskProperty = clips.GetArrayElementAtIndex(i).FindPropertyRelative("transformMask");
  46. updateTransformMaskMethodInfo.Invoke(mi, new System.Object[]{avatarMask, transformMaskProperty});
  47. }
  48. so.ApplyModifiedProperties();
  49.  
  50. AssetDatabase.ImportAsset(path);
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement