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.
  2. Dismiss Notice

Modifying UVs on a sharedMesh from a .blend file

Discussion in 'Scripting' started by OwenG, Jul 20, 2015.

  1. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    Hi everyone,

    I've been working on our art pipeline and I've run into a problem I don't know how to solve. If anyone has any ideas, I'd love to hear them! :)

    I've written a postprocessor that acts on .blend models. In in, I grab all the meshes and modify their UVs when they import into Unity, like so:

    Code (CSharp):
    1. var meshUVs = curMesh.sharedMesh.uv; // curMesh is a MeshFilter
    2. for (int v = 0; v < meshUVs.Length; v++)
    3. {
    4.     meshUVs[v] = new Vector2(newU, newV);
    5. }
    6. curMesh.sharedMesh.uv = meshUVs;
    7.  
    And this updates my meshes in game. It updates all instances in the scene, which exactly what I want. So far so good...

    However, if I save the scene, close Unity and reopen it, the UVs on my meshes all revert back to the values from the .blend file. I've also tried calling SetDirty() and SaveAssets, but that hasn't worked either.

    Is it possible to modify the sharedMesh in this way and have it save? Or do I need to do something like write out a new prefab for each .blend file on import and modify the sharedMesh there instead? I'm open to any ideas anyone has.

    Thanks!
    Owen
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    What exactly is your goal? Do you want to modify the mesh only within the Unity editor, or do you want to create a game where the user can modify the mesh?
     
  3. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    I'll be creating models in Blender, importing them into unity, where I need to muck about with the UVs on import in code. I'm also writing editor tools that allow me to further work with the UVs. The player won't do any of this, it's all editor-side.

    Owen
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    We use a ModelImportPostProcessor:AssetPostprocessor that edits/strips uv channels at import time via the OnPostprocessModel(GameObject go) method and it works fine for us updating a game object's sharedMesh(es). We don't call any SetDirty or AssetDatabase save stuff and the mesh updates persist after import and any save/reload in the scene or editor. So, we're not exactly doing anything special after updating the shared mesh, just editing it.

    FYI we're currently importing .fbx and .c4d files - we're not using Blender.
     
  6. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    Interesting. I'm currently doing the UV manipulation in OnPostprocessAllAssets. I wonder if that's too late during the asset import pipeline. I wonder if I do it during OnPostprocessModel if the sharedmesh is still changeable.

    That wouldn't solve the problem where I have to modify the mesh UVs in the editor...but would potentially solve the ones I modify at import time if it works...

    Owen
     
  7. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    Yeah, maybe so - your original code snippet didn't say what callback you were doing the processing on which is why I mentioned our usage. OnPostprocessModel(GameObject go) is called as needed during each model file import and says that it builds a temporary gameobject after import but before saving (to the asset database), so it just kinda worked for us without any problems.

    I expect that if you're editing the sharedMesh at other times in the Editor you'd need to reference the sharedMesh asset on disk (lookup via the game object, or perhaps something like AssetDatabase.GUIDToAssetPath) and jump through the AssetDatabase hoops of setting dirty, saving, refreshing, etc.
     
  8. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    Yeah, sorry, should have included the callback. I'll have to look at more of the AssetDatabase stuff and see if I can get it to write back to the database somehow.

    Maybe I'm not marking the right object dirty when trying to save. I'll have to dig in a bit further.

    Thanks!
    Owen
     
  9. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    The editor scripting is a bit tricky, eh. Every time I look at someone else's editor script for something I learn something new that I hadn't seen before :) The official docs and examples are pretty light on most things...

    ps. hope FutureGrind is going well :)
     
  10. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    Thanks! Yeah, things are chugging along. I'm reworking our asset pipeline to make a bunch of stuff we want to do with colour easier/possible. Then I'll be getting back into making art stuff. :)

    Owen