Search Unity

Finding Path of mesh within an Asset (when mesh names are the same)

Discussion in 'Editor & General Support' started by Sannyasi, May 22, 2017.

  1. Sannyasi

    Sannyasi

    Joined:
    Nov 20, 2012
    Posts:
    36
    For those not keen on reading much, here is the quick version....

    I am trying to save a text file which lists all files and meshes used within an asset prefab so I can clean up the data set.... but here's the kicker... not all mesh names are unique and I can't find a way to uniquely identify the mesh used in the scene.


    The full explanation.....

    I have a project that has gone on too long, with too many client changes, too many artists cross working, and too many applications used for modeling a particular asset, and things are getting very unruly.
    At last count there are over 400 different mesh objects spread amongst more than 50 fbx files. There are meshes within some files that are not being used anymore and or are being replaced with alterations / fixes in other files, and I'd like to get back to one clean data set.

    To do so I was going to write an editor script to catalog the meshes used within a scene hierarchy tracking the mesh name and the fbx file they came from into a text file. I would then via python reassembly the disparate pieces into one data set by importing the files into Maya and keeping only the parts I need.

    The problem I have is that some of this data came from Maya initially, which allows the user to have duplicate names for objects in the same file (so long as they are in a different section of the hierarchy).
    Using functions within AssetDatabase, PrefabUtility, etc.. I can get the sharedMesh name, and the fbx file it comes from, but when there are two or more meshes with the same name within the FBX file, I can find no way to differentiate them....

    I really don't want to go down the road of writing an .OBJ exporter due to things like edge smoothing, UV maps, and the like, which would certainly complicate and potentially alter the data from its current state.

    Any suggestions on how I can fully resolve which meshes I need?
    PS (the object in the scene does not necessarily match the original fbx prefab's hierarchy since this is a prefab created from many different files - so marching through the hierarchy as it is in the scene will not work)
     
  2. bjennings76

    bjennings76

    Joined:
    Jun 15, 2013
    Posts:
    6
    I recently had this same problem and for the life of me couldn't figure out how to get the original path to the mesh inside an .fbx, but then it occurred to me: You can treat the .fbx file like a normal prefab. You just can't save it. So you can search it for the path. Here's my solution:

    Code (CSharp):
    1. private static string GetMeshPath(string modelPath, MeshFilter prefabFilter) {
    2.     GameObject model = AssetDatabase.LoadAssetAtPath<GameObject>(modelPath);
    3.  
    4.     if (!model) {
    5.         Debug.LogWarning("No game object found at " + modelPath, prefabFilter);
    6.         return null;
    7.     }
    8.  
    9.     MeshFilter modelFilter = null;
    10.  
    11.     foreach (MeshFilter filter in model.GetComponentsInChildren<MeshFilter>(true)) {
    12.         if (filter.sharedMesh == prefabFilter.sharedMesh) {
    13.             modelFilter = filter;
    14.             break;
    15.         }
    16.     }
    17.  
    18.     if (!modelFilter) {
    19.         Debug.LogWarning("No game object found for " + prefabFilter, prefabFilter);
    20.         return null;
    21.     }
    22.  
    23.     Transform transform = modelFilter.transform;
    24.     string path = transform.name;
    25.  
    26.     while (transform.parent) {
    27.         transform = transform.parent;
    28.         path = transform.name + "/" + path;
    29.     }
    30.  
    31.     return path;
    32. }
     
    Last edited: Jun 17, 2017
    Thundernerd and Sannyasi like this.
  3. Sannyasi

    Sannyasi

    Joined:
    Nov 20, 2012
    Posts:
    36
    Interesting tactic... Been busy on other things since I posted the question. I'll check this out when I get a chance...
    Thanks for the reply!
     
  4. Sannyasi

    Sannyasi

    Joined:
    Nov 20, 2012
    Posts:
    36
    And it works... Thanks CthulhuComplex for the solve.

    It does look like some hierarchical info is not kept on the Unity side (missing empty root group nodes within the raw fbx), so there may still be some resolve logic needed within Maya via python, but definitely doable.

    Thanks again!