Search Unity

Question Directly Change Baked Rotation of obj file?

Discussion in 'Scripting' started by Mashimaro7, May 28, 2023.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Is there anyway to change the baked rotation of an obj file? I'm downloading a mesh via replicate's shap.e API and i need to change it to x -90 for the rotation to be proper, but is there any way to alter the baked rotation directly through script?

    I know I could just open the obj in blender or something, but I'd prefer to be able to do it directly. Idk if this is even possible? The obj spawns the mesh as a child of an empty object, so i'm temp fixing it by rotating this when instanitating it, but i wonder if there's a better solution?

    Thanks
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    It is possible but it's quite drastic, because it could be that all vertices in your mesh are already baked with that rotation in mind, meaning that you would have to write a full OBJ reader and writer, to make such a sweeping change. That's completely, umm, unproductive to say the least.

    That said, I believe Unity has an OBJ reader/writer as a package somewhere (maybe I'm wrong about this).
     
  3. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Hmm, okay. Sounds a little bit more painful than it's worth... maybe ill just rotate the child upon spawning it lol, do you know how to spawn an obj file as an object? Resources.load is returning "null" when i input the file location
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    Files themselves are not valid objects in Unity. To make sense of an OBJ file, it has to go through File Importer, and in this process it becomes a Unity mesh. Every file you see in 'Project' is already interpreted in some way, and treated as Asset. This is why Unity boots up for some time (depending on the size of your project), among other things it scans the assets, recompiles and reimports as necessary.

    That said, nothing stops you from manually opening and reading/writing the file in C#. But it's a long rabbit hole from that point on. If you're a beginner, you won't achieve much. I can only advise you to not fight against Unity, but adjust your workflow to work with it.

    Resources.Load is not intended for Assets in the general sense. It is intended for a subselection of ready-to-go runtime assets, like prefabs, textures, sounds. Sure you can also place ScriptableObject and fonts there, even txt files, but not OBJs. That would require an editor-only importer feature which isn't available in runtime.

    However you can use your own OBJ importer, I'm sure there is something available, and work with that during runtime. But I'm pretty much sure that's an overkill for whatever it is that you're doing.

    Again, I think you cannot do that via Resources.Load.
     
    Last edited: May 28, 2023
    Mashimaro7 likes this.
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    Btw to eleborate on this. Resources.Load specifically returns only types that inherit from UnityEngine.Object.
    This is why you're getting null, it's a fake null object meaning that the asset you're aiming for isn't there and probably wasn't even added to Resources pool.

    If you check Resources more thoroughly, you'll see that it has a method FindObjectsOfTypeAll so you can write something like this to see what's actually acknowledged inside the Resources pool
    Code (csharp):
    1. foreach(var obj in Resources.FindObjectsOfTypeAll<UnityEngine.Object>()) {
    2.   Debug.Log(obj.name);
    3. }
    Sadly you can't check the assets' paths without using the editor code.
     
    Mashimaro7 likes this.