• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EmmetOT · Mar 03, 2020 at 11:29 AM · editoreditor-scriptingfbxfbximportercontextmenu

Creating Context Menu Item for FBX files

I would like to add a context menu option for imported .FBX files. I previously had the same functionality in the AssetImporter, but I don't want this functionality to happen for all .FBXs imported. I don't want it to be automatic.

The specific functionality is just that I want to set the .FBX's animation clips' masks to 'create from this model' and set the avatar mask to every child bone.

Here's the method I used in the Asset Importer:

 /// <summary>
 /// Automatically set this model importer's mask type to 'CreateFromThisModel' and set the mask
 /// to the entire hierarchy.
 /// </summary>
 public static bool CreateMaskFromThisModel(this ModelImporter modelImporter, GameObject model)
 {
     bool changed = false;
 
     ModelImporterClipAnimation[] clips = modelImporter.clipAnimations;
 
     if (clips == null || clips.Length == 0)
         clips = modelImporter.defaultClipAnimations;
 
     ModelImporterClipAnimation[] newAnims = new ModelImporterClipAnimation[clips.Length];
 
     for (int i = 0; i < clips.Length; i++)
     {
         if (clips[i].maskType != ClipAnimationMaskType.CreateFromThisModel)
         {
             newAnims[i] = clips[i];
             newAnims[i].maskType = ClipAnimationMaskType.CreateFromThisModel;
 
             changed = true;
         }
     }
 
     AvatarMask mask = new AvatarMask();
     mask.AddTransformPath(model.transform, true);
 
     for (int i = 0; i < clips.Length; i++)
     {
         if (clips[i].maskSource == null || clips[i].maskSource.transformCount != mask.transformCount)
         {
             clips[i].ConfigureClipFromMask(mask);
             changed = true;
         }
     }
 
     modelImporter.clipAnimations = clips;
 
     return changed;
 }



This works. However, I seem to have no way to access the model importer after the import, at will. This is as far as I've gotten for my context menu.

 [MenuItem("Assets/Create Mask From This Model", isValidateFunction: true)]
 private static bool CheckIfFBX()
 {
     if (Selection.gameObjects.Length == 0)
         return false;
 
     for (int i = 0; i < Selection.gameObjects.Length; i++)
         if (!AssetDatabase.GetAssetPath(Selection.gameObjects[i]).ToLowerInvariant().EndsWith(".fbx"))
             return false;
 
     return true;
 }
 
 [MenuItem("Assets/Create Mask From This Model")]
 private static void CreateMaskFromThisModel()
 {
     for (int i = 0; i < Selection.gameObjects.Length; i++)
     {
         // get importer, clips, avatar mask, anything?
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding to GameObject context menu WITHOUT [MenuItem(...)] 1 Answer

Unity soft locks on FBX Import,Unity Soft Locks on Import 0 Answers

How to avoid 2019.3 FBX Import missing Inspector UI? 1 Answer

Hide default editor Gizmo for move, rotete, scale 0 Answers

Event when exiting PolygonCollider2D edit mode 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges