Unity - custom Export and Import FBX Data
Asked Answered
V

1

2

I am trying to write custom data inside a FBX file from 3dsmax at export time. This be might 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.

Vizierate answered 2/9, 2015 at 8:5 Comment(0)
N
3

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.

Nonbeliever answered 2/9, 2015 at 17:22 Comment(4)
Thanks @theodox, this seems to be a nice solution. But as I am new to this subject, I have some difficulties about finding a way to write JSON data inside a custom attribute to an object in 3dsmax. I also thought that using the FBX SDK would allow to write any custom data in the FBX but how could it be possible to parse it inside Unity.Vizierate
Alright, I found the User Defined object properties panel in 3dsmax so I can add the JSON and parse it inside Unity using the UDP3DSMAX entry. But is there a way to add new custom properties and set them ?Vizierate
It seems that custom attributes are also exported and available using OnPostprocessGameObjectWithUserProperties.Vizierate
Sounds like youve got it - a string custom attribute containing escaped JSON then parsed in C#Nonbeliever

© 2022 - 2024 — McMap. All rights reserved.