- Home /
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?
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612224319im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
How to add items to the Component references context menu? 0 Answers
EditorGUI add SortingLayer-like list to custom Editor 1 Answer