Search Unity

Custom Export and Import FBX Data

Discussion in 'Scripting' started by MaT227, Sep 2, 2015.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    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.
     
  2. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thank you for your answer. I also get 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:

    Code (CSharp):
    1. public void OnPostprocessGameObjectWithUserProperties(GameObject incomingGameObject, string[] incomingPropetyNames, object[] incomingValues)
    2. {
    3.   var thisModelImporter = this.assetImporter as ModelImporter;
    4.   var meta_key = "your_attribute_name";
    5.   if (incomingPropetyNames.Contains(meta_key))
    6.   {
    7.   string raw_data = (string)incomingValues[Array.IndexOf(incomingPropetyNames, meta_key)];
    8.   // use the data here....
    9.   }
    10. }
    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.