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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ObjReader - load .obj files at runtime

Discussion in 'Assets and Asset Store' started by Eric5h5, May 7, 2013.

  1. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The ConvertFile functions return an array of GameObjects, so in that example script, you have that assigned to the variable objData. So the first GameObject would be objData.gameObjects[0], etc.

    --Eric
     
  2. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    Hi Eric.
    I used "ObjReader.use.ConvertFile" Function.
    After then, I need to "progress" and "isDone".but, "ConvertFile" is not have them.
    So, I try use "ConvertFileAsync" Function.
    this code.
    Code (CSharp):
    1.  
    2. public IEnumerator ReadModel(string filePath, bool boolean, Material standMat, string itemName)
    3. {
    4.    filePath = "file://" + filePath;
    5.    ObjReader.ObjData objData = ObjReader.use.ConvertFileAsync(filePath, true, standardMaterial);
    6.  
    7.    while(!objData.isDone)
    8.    {
    9.       if(loadPercent != objData.progress*100)
    10.       {
    11.          Debug.LogError(itemName + "Loading..." + (objData.progress*100).ToString("f0") + "%");
    12.          loadPercent = objData.progress*100;
    13.       }
    14.       yield return null;
    15.    }
    16.    GameObject[] GoArray = objData.gameObjects;
    17. }
    18.  
    I'm get Error about loading : "Couldn't open file".
    What's wrong with me?

    Thank you :)
     
    Last edited: Jan 26, 2016
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The file doesn't exist at the path you're supplying.

    --Eric
     
  4. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    But, I try "ConvertFile". It's work.
    Not changed path.
    I just used "ConvertFileAsync", then changed "file://" + path.

    Thank you.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's Unity telling you that it can't open the file, not a UFB error. If I use a known working path with ConvertFile, and change it to ConvertFileAsync with "file://" + path, it works fine. So the conclusion must be that your path is wrong, I'm afraid.

    --Eric
     
  6. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    I've been looking for a good Obj importer. Most of the one's i've played around with simply make 3 vertices/UVs for each face. This results in models having a lot of vertices.

    Neighboring faces often share the same vertices and UVs. How does ObjReader handle faces that share UVs and vertices? Does it try to keep the vertex count low in any way, or does it give each face it's own vertices and UVs?
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It splits vertices as necessary for UV seams. If not necessary, it doesn't split, as in this cube:

    Screen Shot 2016-01-27 at 5.37.19 AM.png

    --Eric
     
  8. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    Great, that's exactly what I need. I just got it and it doesn't disappoint.
     
  9. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    Here is my Error.

    Error loading file://C:/Users/CEO/Documents/SPSim/Resources/Objectmodeler/Output/Models/booth/booth.obj: Couldn't open file /Users/CEO/Documents/SPSim/Resources/Objectmodeler/Output/Models/booth/booth.obj
    UnityEngine.Debug:LogError(Object)
    <GetWWWFiles>c__Iterator0:MoveNext()

    I don't understand, path is wrong. I try typing <File:// + 'path'> url inputbox in my webBrowser. It's found folder. and booth.obj file is there.
    What can I do?
    Thank you.:)

    p.s : What is UFB? is Unity File Browser?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oops, replace UFB with ObjReader. ;) Maybe Unity isn't allowed to access the file for some reason, or something like that. As mentioned, I have no issues using ConvertFileAsync with local files here. Since this doesn't seem to be specifically an ObjReader issue, but rather it's Unity itself that won't load the file, perhaps you could get more help if you start a topic about opening files with file:// using the WWW class, since unfortunately I'm about out of suggestions. Maybe try a different file, just in case.

    --Eric
     
  11. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    I find Another way.
    replace "file://" with "file:///"

    Please See my Code.
    Code (CSharp):
    1.  
    2. public IEnumerator ReadModel(string filePath, bool boolean, Material standMat, Material transMat, string itemName)
    3. {
    4.     filePath = "file:///" + filePath;
    5.     objData = ObjReader.use.ConvertFileAsync(filePath, true, standMat, transMat);
    6.     StartCoroutine("CheckProgress");
    7.     Selected_Item = new GameObject(itemName);                                   Selected_Item.transform.parent = uC.localParent.transform;
    8.  
    9.     if(objData.isDone)
    10.     {
    11.         foreach(GameObject g in objData.gameObjects)
    12.         {
    13.             g.transform.parent = Selected_Item.transform;
    14.         }
    15.         yield return objData;
    16.     }
    17. }
    18.  
    19. private IEnumerator CheckProgress()
    20. {
    21.     while(!objData.isDone)
    22.     {
    23.         Debug.LogError(itemName + "Loading..." + (objData.progress*100).ToString("f0") + "%");
    24.         yield return new WaitForSeconds(0.5f);
    25.         if(loadPercent != objData.progress*100 || loadPercent == 0f)
    26.         {
    27.             loadPercent = objData.progress*100;
    28.             Debug.LogError(itemName + "Loading..." + (objData.progress*100).ToString("f0") + "%");
    29.         }
    30.     }
    31.     Debug.LogError(itemName + "Loading..." + (objData.progress*100).ToString("f0") + "%");
    32. }
    33.  
    It's work find. But, Always "objData.isDone" is false.
    I try "Debug.LogError" about "objData.progress".
    I don't found 100%.

    I want isDone true, then "objData.gameobjects(GameObject[])" set parent.

    Thank you.
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I doubt using file:// with WWW would have any progress, since it finishes in one frame with local files. The WWW class is meant for internet access, where you don't know how long things will take to download. Honestly I don't really understand why you're doing this; you should probably just stick with ConvertFile.

    --Eric
     
  13. LRobergeon

    LRobergeon

    Joined:
    Oct 22, 2015
    Posts:
    5
    Hi,

    I have an important question !
    I have a directory on my android smartphone which contains 2 files, one is a .obj model and the other one is .mtl material about this model.

    Is your plugin worked on android smartphone ?
    How can I load the model with its material that is in my phone directory (in Application.persistanceDataPath) and show it into my application at runtime ?

    Thx for your help !
    ----
    Loïc
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I works with Android; mtl files are loaded automatically if they're referenced by the .obj file.

    --Eric
     
  15. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    I'm using 'ConvertFile' to load many files at once.
    It's not a problem to load few of simple 'Obj' files.
    When the size or quantity increases, Unity stops responding.
    to fix this problem I've used "Corountine" to load UI which would show current Loading state into Progress UI.
    Also, when file is "isDone" state, it goes to next state.
    if you have a good idea please let me know.
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It won't make any difference, since ObjReader isn't multi-threaded. If you want, you can wait a frame between each ConvertFile call, but there is no progress you can display otherwise. The progress for ConvertFileAsync is only for downloading; the conversion itself is identical: the model is either 0% converted or 100%, nothing in-between.

    --Eric
     
  17. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    then is it possible to show that load has been terminated for this model using 'WaitForEndOfFrame()', everytime 'ConvertFile' is called?
    I dont know whether There is a problem with the code or Frame is being pushed because of certain Obj File is too big. Print(Debug.Log) was done everytime 'ConvertFile' is called but still it turns to 'no respond' state and after all Load is terminated, then Print(Debug) happens all at once.
    if Frame is being pushed due to Obj file being too big, I would like your advice on this matter
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    When you call ConvertFile, nothing else happens until it's done. After you call it, the model is complete and there is no need to wait for anything. How long it takes depends on the complexity of the file. As I mentioned, you can wait a frame between each ConvertFile call. It sounds like you're running many ConvertFile calls in one frame, in which case the frame will take as long as it takes to run them all.

    --Eric
     
  19. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    Hi Eric.
    I have a another problem.(import more than this problem).
    So, I finish The another Problem. and I'll try that(your advice).
    And then finish, I'll tell you about result or any question.
    Thank you. and have a nice day :)
     
  20. LRobergeon

    LRobergeon

    Joined:
    Oct 22, 2015
    Posts:
    5
    Hi,

    Thx a lot for this asset, it works as well !

    But i have a little problem, when i want to see my OBJ, if i zoom on it,, my camera see inside the OBJ
    and i just want to see the OBJ entirely :s

    Can we change it ?

    thank you
    ----
    Loïc
     
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ObjReader is only for importing .obj files; what you do with the mesh is up to you. You can do whatever you want, since the result is just a standard GameObject with a mesh renderer.

    --Eric
     
  22. Max01

    Max01

    Joined:
    Jun 23, 2014
    Posts:
    22
    Hi Eric,
    Your package is amazing, it works like a charme!
    I wonder if after download models from WWW it could be possible (IOS-DROID) to save them locally so next time I run the app I find models already downloaded and, if it is, could you put me into the right way to figure it out, please?

    Thank You!
     
    Last edited: Feb 16, 2016
  23. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Thank you! You could try an .obj exporter script; a quick search turns up this and this. Although serializing the mesh would probably be a better approach (like this).

    --Eric
     
  24. Max01

    Max01

    Joined:
    Jun 23, 2014
    Posts:
    22
    Thanks Eric!
    Support feedback in just a few hours, great!
    I'll get throughout all your suggestions, but just for a quick recap I've got an app running on both IOS and Androrid devices that should need to save locally, on devices, ( .obj) meshes users just downloaded from a server through your package. Please don't get bored if I'll need some more advices on that topic even if they' re not strictly related to your asset.

    Thanks again!
     
    Last edited: Feb 16, 2016
  25. ngng2306

    ngng2306

    Joined:
    Mar 2, 2016
    Posts:
    3
    Dear Eric,

    I am so new for a unity development, then i saw this assets is work on Android that what it want.
    Then my plan is app would be able to load the .obj model from my server or local storage,
    then i would display it and able to move, scale and rotate that i like. and background would open a camera to capture
    real time images. It is little bit similar to AR. i am so worry about is that able to do above things.
    i don't know the detail, i hope it works. Any ideas? Thanks Eric.
     
  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Once ObjReader has loaded an .obj file, it's turned into a GameObject like any other GameObject; what you do with that is up to you.

    --Eric
     
  27. LRobergeon

    LRobergeon

    Joined:
    Oct 22, 2015
    Posts:
    5
    Hello,

    Thanks for the plugin.

    I have a little problem. When i try to import an OBJ with his texture, if the texture are in a different folder, the model is load with pink texture (it don't find the png/jpg in subfolder)
    Can we precise the path of textures ? (the path in the .mtl is correct)

    Thanks a lot
     
  28. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I just tried it with a texture in a subfolder, and it works fine. Make sure the path and file name are 100% correct.

    --Eric
     
  29. LRobergeon

    LRobergeon

    Joined:
    Oct 22, 2015
    Posts:
    5
    Sorry wrong question,

    It's works perfectly with Unity editor !

    but when i try to build my application on Android, the textures doesn't show :s

    thanks for your fast response ^^
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I would assume the path is not correct on Android.

    --Eric
     
  31. LRobergeon

    LRobergeon

    Joined:
    Oct 22, 2015
    Posts:
    5
    I tried to load only one texture in my .mtl file and it works (the texture is a .jpg file).
    The problem is that the application doesn't succeed to load all textures shown in the .mtl file.
    I use the model from this website https://3dwarehouse.sketchup.com/?hl=fr

    I need to load lots of textures, do you have a solution?
     
  32. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The models I've tried that use multiple textures work fine. It could be that you have combine multiple groups turned on, but not use submeshes when combining, which naturally wouldn't work since a single material can't have multiple textures of the same type.

    --Eric
     
  33. nocoast

    nocoast

    Joined:
    Jul 17, 2013
    Posts:
    3
    I understand this imports OBJs at runtime, I was wondering if it was possible to import OBJs through update too? i.e. the mesh could be externally changing while the program is running and I would like to be able to reimport the mesh as it changes.

    Not asking how to do it, just wondering if it is possible to do it with this asset?

    Thanks!
     
  34. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It creates a new GameObject when importing an .obj file, but if the file changes, you could destroy the old GameObject and import a new one.

    --Eric
     
  35. uniteiro

    uniteiro

    Joined:
    Mar 11, 2014
    Posts:
    12
    hi,
    it is possible to implement Json3d? and what would be the cost? I use your pluggin to 2 years and now the customer asked to implement Json3d beyond OBJ
     
  36. cadgarcia

    cadgarcia

    Joined:
    Jul 23, 2015
    Posts:
    1
    Hello. I also have the same need.
    Attached is the code of a table format json3d who like to read and manipulate the Unity3D.
    I look back, thanks.
    Garcia
     

    Attached Files:

  37. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That would require a new utility, since it doesn't look remotely similar to the .obj format. It's not something I would implement, sorry.

    --Eric
     
  38. davegirdy1

    davegirdy1

    Joined:
    Jun 13, 2015
    Posts:
    6
    The asset normally works great for me... but I usually hit the 65,000 vertices limit on my .obj files. Is there any plans handle this use case? Or does anybody have a script that can rewrite an .obj file to use multiple objects? Maybe that could work, if I don't combine the meshes. Thanks.
     
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It works now, if you don't check "combine multiple groups" and each group is <65K vertices.

    --Eric
     
  40. petkovas

    petkovas

    Joined:
    Oct 12, 2013
    Posts:
    28
    Can i load an .obj-file in Unity from a (WSGI) stream with the help of ObjReader?

    I would like to send an GET request for the execution of a Python script that would return as a result an obj-file. And i am looking for a way to load that obj-file in Unity as a GameObject.
     
  41. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ObjReader turns .obj files into meshes, where the .obj file is a string, so as long as you obtain the string somehow, ObjReader doesn't know or care where it came from.

    --Eric
     
  42. petkovas

    petkovas

    Joined:
    Oct 12, 2013
    Posts:
    28
    Does that mean that ObjReader works with a variable/object with type string for its input? Can all lines of the obj-file be sent to ObjReader as one string?

    What format is that string?
     
    Last edited: May 1, 2016
  43. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    All the lines must be sent as one string, which contains the .obj file contents. (Other possibilities are passing in a string which contains the path of the file locally, or via the web.) I'm not really sure what you mean by format, since it's an .obj file, which is text.

    --Eric
     
  44. davegirdy1

    davegirdy1

    Joined:
    Jun 13, 2015
    Posts:
    6
    Thanks for the response. The problem is that my OBJ file has more than 65K vertices, and also not split up into groups. It would be awesome if the OBJReader could handle big OBJ files by making multiple meshes. For now, I can only load objects within the limit.
     
  45. Rednalreden

    Rednalreden

    Joined:
    Aug 16, 2013
    Posts:
    3
    I've recently bought this package and I could also use this. The models from one of our clients are quite large and not separated into groups of < 65k verts. I've experimented a bit in the source code and I think it should be possible to implement. Is this something we can expect?
     
  46. Keehan

    Keehan

    Joined:
    Aug 18, 2012
    Posts:
    23
    Hello! Just bought this product and using the ExternalFile.js I was wondering how I would apply a Box Collider to an externally created .obj which uses this script:
    Code (JavaScript):
    1. function Start () {
    2.     objFileName = Application.dataPath + "/ObjReader/Sample Files/" + objFileName;
    3.     ObjReader.use.ConvertFile (objFileName, true, standardMaterial, transparentMaterial);
    4.     objFileName.AddComponent("MeshCollider");
    5. }
    Thanks :)
     
  47. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to add a MeshCollider component (note, don't use quotes in AddComponent) to the object returned by ConvertFile. The script you have can't work since you're attempting to add a component to a string, not a GameObject.

    --Eric
     
  48. Keehan

    Keehan

    Joined:
    Aug 18, 2012
    Posts:
    23
    This is my function and every last created object is successfully stored in var objData : GameObject[];
    Code (JavaScript):
    1.       if (objData != null && objData.gameObjects != null) {
    2.      for (var i = 0; i < objData.gameObjects.Length; i++) {
    3.        Destroy (objData.gameObjects[i]);
    4.      }
    5.    }
    6.    objFilePath = Application.dataPath + "/ObjReader/Sample Files/" + objFileName;
    7.    objData = ObjReader.use.ConvertFile (objFilePath, true, standardMaterial, transparentMaterial);
    8.    objData[i].AddComponent(MeshCollider);
    Using this script, I export the project and the objects are not created in the build version. I've tried putting the .obj in multiple folders including Resources/ObjReader/Sample Files/ but no luck
     
    Last edited: May 12, 2016
  49. rahharya

    rahharya

    Joined:
    Jun 6, 2016
    Posts:
    8
    Thanks for the Assets.
    It works very well.

    I have a Problem with the Skript.
    How to add a Component (Skript or Rigid Body) to the .obj File that i opened in the Runtime?

    Thanks
    -Yohanes-
     
  50. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use AddComponent with a GameObject returned by the ConvertFile function.

    --Eric