Custom Export and Import FBX Data

Hi there,

I am trying to write custom data inside a FBX file from 3dsmax at export time. This might be possible even if I don’t know right now how to do that.

But my question would be how can I read those custom FBX data inside Unity on import.

Thanks a lot.

Here is a great answer from theodox on Stackoverflow.

The easy way to do this is to add a custom attribute to an object in
the Maya/Max scene and then use the Unity
AssetPostprocessor

to find the attribute and parse it’s data.

AssetPostprocessor has an
OnPostprocessGameObjectWithUserProperties
callback which will be fired for every transform in your fbx file
which has a custom attribute applied. You’ll want to find your
attribute in the postprocessor and do something:

public void OnPostprocessGameObjectWithUserProperties(GameObject incomingGameObject, string[] incomingPropetyNames, object[] incomingValues)
{
    var thisModelImporter = this.assetImporter as ModelImporter;
    var meta_key = "your_attribute_name";
    if (incomingPropetyNames.Contains(meta_key))
    {
        string raw_data = (string)incomingValues[Array.IndexOf(incomingPropetyNames, meta_key)];
        // use the data here....
    }
}

It’s annoying that there’s no standard way to create file-level
metadata, I usually just attach my ‘top level’ data to something like
the root of the model in the FBX file. You can pass complex data by
storing things as a JSON blob and passing the JSON as a string
attribute; that’s usually easier to maintain than a web of multiple
attribute names and types.

Be warned that FBX tends to mangle long attibute names - I’m not sure
where the cutoff is but extremely long attribute names sometime get
truncated to a meaningless string like `FBXASC_012345" if they exceed
the magic character count.

The only thing you cant do easily this way is vertex or face level
data; while you could pack that sort of thing into custom attributes,
there’s not a rock-solid guarantee that the indices you see in
Max/Maya will be the same as the ones in the FBX. For that type of
data vertex colors or extra UV channels are good places to store extra
information, though it’s up to you to come up with a way of deciding
what a given color or UV means.