Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How to import user defined attributes from FBX files

Discussion in 'Asset Importing & Exporting' started by a_p_u_r_o, Jun 9, 2016.

  1. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    First let's look at how user defined attributes can be added in Maya.
    AddAttributeMenu.png AddAttributeDialog.png
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.IO;
    4.  
    5. class CustomImportProcessor : AssetPostprocessor {
    6.     void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] names, System.Object[] values) {
    7.         ModelImporter importer = (ModelImporter)assetImporter;
    8.         var asset_name = Path.GetFileName(importer.assetPath);
    9.         Debug.LogFormat("OnPostprocessGameObjectWithUserProperties(go = {0}) asset = {1}", go.name, asset_name);
    10.         string str = null;
    11.         Vector3 vec3 = Vector3.zero;
    12.         for (int i = 0; i < names.Length; i++) {
    13.             var name = names[i];
    14.             var val = values[i];
    15.             switch (name) {
    16.             case "StringData":
    17.                 str = (string)val;
    18.                 break;
    19.             case "VectorData":
    20.                 vec3 = (Vector3)(Vector4)val;
    21.                 break;
    22.             default:
    23.                 Debug.LogFormat("Unknown Property : {0} : {1} : {2}", name, val.GetType().Name, val.ToString());
    24.                 break;
    25.             }
    26.         }
    27.         if (str != null || vec3 != Vector3.zero) {
    28.             var udh = go.AddComponent<UserDataHolder>();
    29.             if (str != null) {
    30.                 udh.StringData = str;
    31.             }
    32.             udh.VectorData = vec3;
    33.         }
    34.     }
    35. }
    36.  
    UserDataHodler.cs:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class UserDataHolder : MonoBehaviour {
    4.     public string StringData;
    5.     public Vector3 VectorData;
    6. }
    7.  
    FbxUserDataSample.png
    Note: You will want to open and edit WithMyData.fbx in the archive directly with Maya.
     

    Attached Files:

    mgear, Camarent and Jingle-Fett like this.
  2. piXelicidio_

    piXelicidio_

    Joined:
    Mar 13, 2015
    Posts:
    6
    Passing by just to let you know you have shared something really useful, thanks.