How to import user defined attributes from FBX files

First let's look at how user defined attributes can be added in Maya.
2670293--188417--AddAttributeMenu.png 2670293--188418--AddAttributeDialog.png
2670293--188419--AttributeEditor.png
Here I've added string attribute named StringData and vector attribute named VectorData.

And the attached example project shows you how to write import time post processor script.

Editor/CustomImportProcessor.cs:

using UnityEngine;
using UnityEditor;
using System.IO;

class CustomImportProcessor : AssetPostprocessor {
    void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] names, System.Object[] values) {
        ModelImporter importer = (ModelImporter)assetImporter;
        var asset_name = Path.GetFileName(importer.assetPath);
        Debug.LogFormat("OnPostprocessGameObjectWithUserProperties(go = {0}) asset = {1}", go.name, asset_name);
        string str = null;
        Vector3 vec3 = Vector3.zero;
        for (int i = 0; i < names.Length; i++) {
            var name = names[i];
            var val = values[i];
            switch (name) {
            case "StringData":
                str = (string)val;
                break;
            case "VectorData":
                vec3 = (Vector3)(Vector4)val;
                break;
            default:
                Debug.LogFormat("Unknown Property : {0} : {1} : {2}", name, val.GetType().Name, val.ToString());
                break;
            }
        }
        if (str != null || vec3 != Vector3.zero) {
            var udh = go.AddComponent<UserDataHolder>();
            if (str != null) {
                udh.StringData = str;
            }
            udh.VectorData = vec3;
        }
    }
}

UserDataHodler.cs:

using UnityEngine;

public class UserDataHolder : MonoBehaviour {
    public string StringData;
    public Vector3 VectorData;
}

2670293--188421--FbxUserDataSample.png
Note: You will want to open and edit WithMyData.fbx in the archive directly with Maya.

2670293--188422--FbxUserDataSample.zip (16.6 KB)

4 Likes

Passing by just to let you know you have shared something really useful, thanks.

1 Like