Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ObjReader - load .obj files at runtime

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

  1. pajamajama

    pajamajama

    Joined:
    Oct 22, 2012
    Posts:
    66
    Thank you :)
     
  2. errolaw

    errolaw

    Joined:
    Dec 8, 2013
    Posts:
    3
    Hey Eric,

    I bought it under asset store yesterday. would like to know what setting should be when I want to import obj from a web server. I can load the model under Unity when development (my one is 4.2). however, when I export to webplayer. the loading message just keep show without the model. is there any secriuty should I put on my apache server? thanks.

    for example 2, I edit the obj file name to http://192.168.1.101/model.obj
    and export under build setting to web player, then copy all the exported files to the root of above ip.

    please advise. thanks!
     
    Last edited: Mar 24, 2014
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  4. errolaw

    errolaw

    Joined:
    Dec 8, 2013
    Posts:
    3
    Thanks for your quick response!

    I tired to put crossdomain.xml under my web server root. however, for example 2, it still keep loading. I reboot server and clear all browser cache already. would it be unity player setting or some other setting i did wrong. thanks.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I can't really offer any support for web server stuff I'm afraid; all I know is what the docs say. I'd recommend starting a topic about it, or doing more research and see if someone had an answer for your situation.

    --Eric
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A small update:

    ObjReader 2.2.1

    Fixes:
    • Fixed issue with lines combined with "\" potentially not being sanitized correctly.

    Also 2.2 is out on the asset store now (2.2.1 submitted).

    --Eric
     
  7. errolaw

    errolaw

    Joined:
    Dec 8, 2013
    Posts:
    3
    Hi Eric,

    about the load of obj under web player. I tired to play around with your source code. I edit the www under GetWWWFiles section to below and I can load the obj content successfully. Just display the text instead of createobject and show the model. do you think we can focus on that part cause I am not really show it is the root cause? please advise. thanks!

    var www = new WWW(objFilePath); --> WWW www = new WWW(objFilePath);
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm afraid I don't understand the question. As far as the code you posted, those two lines are the same thing.

    --Eric
     
  9. jainam

    jainam

    Joined:
    Oct 20, 2013
    Posts:
    9
    is there a way I could play animations and audio clips with the imported OBJ file on the fly?
     
  10. gheeler

    gheeler

    Joined:
    Nov 7, 2012
    Posts:
    18
    I have all my models online so I wrote a php script to split objs to overcome this.

    Some of my obj files are quite large 100mb + so my app was freezing while loading those. I wrote in a small feature that was pretty easy to implement.
    I have an overload of CreateObjects that you pass the vert count so far to adjust the arrays accordingly so it can take a complete object from anywhere in the file.

    Then I have a coroutine that reads in lines with a streamreader, gets at least 10000 lines then until the end of the current object and calls CreateObjects. This also updates a 'Progress' property by counting the bytes from the total found with FileInfo.

    All for the sake of a smooth loading bar! If you're interested I can send them on to you.
     
    adamazing likes this.
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    .obj files don't support animation. As far as audio goes, once an .obj file is imported, you have standard a GameObject (or array of GameObjects), so you can do whatever you like with them, same as any other GameObject.

    Sure, sounds interesting.

    --Eric
     
  12. adre

    adre

    Joined:
    Mar 28, 2014
    Posts:
    38
    Hello, i finally made it work.
    I just have one question: when the object is placed on the scene, it gets the name of the maya shader group instead of the name of the file or the mesh.
    Where can i change this?
    I need it to assign the name of the file to the gameobject created, to find it in the scene and add some components when is created.

    Thanks.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm not familiar with Maya, but the name is taken from the group name that it finds in the .obj file, so I imagine there's some way to change the shader group name in Maya so it uses the name you specify?

    --Eric
     
  14. africanrhino

    africanrhino

    Joined:
    Aug 15, 2013
    Posts:
    11
    hi
    i'm loading string objFileName = "http://zion.vrsites.com/assets/moon.obj";
    i think the obj has earth0.mtl implied.
    i'm getting :
    Error loading http://zion.vrsites.com/assets/./earth0.mtl: 404 Not Found
    UnityEngine.Debug:LogError(Object)
    <GetWWWFiles>c__Iterator0:MoveNext()


    is there anyway i can override the texture or default to a supplied texture if the material fails? i'm using objreader to read foreign non verified models, there is no way to guarantee how much of the obj is supplied beyond the model itself.
    please help.
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    OK, I can make an option to have a fallback in case the MTL file is missing.

    --Eric
     
  16. adamazing

    adamazing

    Joined:
    Aug 15, 2013
    Posts:
    2
    I've had the same problem with huge files and I'd be extremely grateful if you could send me the details too.
     
  17. TomPK7

    TomPK7

    Joined:
    May 15, 2014
    Posts:
    6
    Hi, I was wondering if anyone here would be able to provide a code example in C# showing how you would get a game object reference to an imported object?

    The program I'm working on takes the url of a .obj file as input from the user so I don't implicitly know the name of the object being imported. I need my script to be able to change the texture,material,position rotation and scale of the imported model based on user input as well. So far I have been able to get the .obj to load at 0,0,0 but I haven't been able to reference it or assign it to anything in order to access it's components.

    Thanks in advance.
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The standard Convert functions return a GameObject[] array, so you just use that for the references. ConvertFileAsync returns ObjData, which has a .gameObjects property, which again is a GameObject[] array that you use to reference objects.

    --Eric
     
  19. TomPK7

    TomPK7

    Joined:
    May 15, 2014
    Posts:
    6
    Thanks for the quick reply. I've tried to access that property with no success. I feel I must have made a mistake in my setup or syntax. I was hoping that seeing a working example might help me identify my mistake.

    Right now I have something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Xml;
    4.  
    5. public ObjReader ObjRdr;
    6.  
    7. void Start()
    8. {
    9.    GameObject Obj;
    10.    Obj = SetModel(Obj);
    11. }
    12.  
    13. GameObject SetModel(GameObject Obj)
    14. {
    15.     string src = "http://vrsites.com/vrassets/AssetObjects/vehicles/ship/ship.obj";
    16.     Obj = ObjRdr.ConvertFileAsync(src,true).gameObjects[0];
    17.     return Obj;
    18. }
    I get a null reference at line 16 even though the model is loaded and visible in the scene.
     
    Last edited: Jun 20, 2014
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That won't work since you're returning immediately without waiting for ConvertFileAsync to finish. Use coroutines and maybe delegates depending on what you want exactly. The included Example2-WWW scene shows coroutines (the text changes to "import completed" when it's done).

    --Eric
     
  21. TomPK7

    TomPK7

    Joined:
    May 15, 2014
    Posts:
    6
    Sorry I really don't mean to be a problem. I don't know enough of java script to make sense of the included examples.
     
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's 95% the same, although in C# you'd use "yield return null" instead of "yield".

    --Eric
     
  23. TomPK7

    TomPK7

    Joined:
    May 15, 2014
    Posts:
    6
    Thanks for your help and your patience. I think I have it figured out.
     
  24. TomPK7

    TomPK7

    Joined:
    May 15, 2014
    Posts:
    6
    Its working perfectly. Thanks again for your help.
     
  25. Adri2013

    Adri2013

    Joined:
    Jun 22, 2014
    Posts:
    8
    Hi,

    I bought ObjReader, but don't know where to start. I am not really experienced in programming.

    I would like to add components like a rigid body and a mesh collider to the imported model, and give the model the same coordinates as the position character controller.



    Any suggestions are welcome :).

    best
    A3Sh
     
  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Since ConvertFile returns a GameObject array, you can do stuff like

    Code (javascript):
    1. var foo = ConvertFile (filePath);
    2. foo[0].transform.position = someOtherObject.transform.position;
    --Eric
     
  27. Adri2013

    Adri2013

    Joined:
    Jun 22, 2014
    Posts:
    8
    Hi Eric,

    Thank you. This is what I did, based on example script 3, and it works.

    Code (JavaScript):
    1. var foo = ObjReader.use.ConvertFile (objFileName, true, standardMaterial, transparentMaterial);
    2. var hand = gameObject.FindWithTag("MainCamera");
    3.  
    4.  loadingText.enabled = false;
    5.        
    6.  foo[0].AddComponent ("Rigidbody");
    7.  foo[0].AddComponent ("BoxCollider");
    8.  foo[0].transform.position = hand.transform.position;
     
  28. Adri2013

    Adri2013

    Joined:
    Jun 22, 2014
    Posts:
    8
    Hi,

    I have another question I am trying to figure out.

    I would like to create a script that monitors the folder on a webserver, or specific file path, and as soon a new model with specific name and number, is saved in the folder, the script will import the model.

    For example with another tool I create an obj model. I save it in this specified folder, with the name model1. As soon as it is in the folder it gets automatically imported. After 10 minutes I create another obj model, and save it with the name model2. As soon as this model is in the folder, it will be automatically imported in the game. And so on.

    Any suggestions are welcome.

    best
    A3Sh
     
  29. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd be better off starting a topic about that, since it's not specifically about ObjReader. It would be best if this topic could be kept to ObjReader support only, thank you.

    --Eric
     
  30. LIVENDA

    LIVENDA

    Joined:
    Oct 31, 2013
    Posts:
    275
    Hi, just purchased this there is an error in importing, smoothing groups are not retained and there also seems to be a strange problem with vertices. I have tried many obj export options and they all have the import error. You can see from the image below, the one on the left is the imported obj and the right one is imported within editor FBX format.

    objimport.jpg
    Please look at this issue as soon as possible, as it is unusable at this stage.
     
  31. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ObjReader doesn't have an option to split vertices, which is what I'm assuming you're doing for the fbx import (using the smoothing angle slider in Unity). The problem with the smoothing angle thing is that you have no direct control over what gets split and what doesn't, so it's best to split vertices in your 3D app.

    --Eric
     
  32. LIVENDA

    LIVENDA

    Joined:
    Oct 31, 2013
    Posts:
    275
    Hi,

    We are not doing anything special, just exporting from 3dsMax default settings and the smoothing settings are not retained. When using fbx there is no issue and we are not changing the smoothing angle slider in the editor. We have tested with another obj importer from the Asset store and it seems to be working fine. Are you able to fix this? Thanks.
     
  33. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Can you post the OBJ above? I'm curious and would like to see if there's anything odd about it. (I've nothing to do with Eric's OBJ importer, but it could help him diagnose the issue, too.)
     
  34. LIVENDA

    LIVENDA

    Joined:
    Oct 31, 2013
    Posts:
    275
    This is very easy to replicate. Just create a tube object in 3dsmax then set the smoothing angle to zero, try to get a faceted look, then just export to obj with default settings. After you can compare the obj version to a version exported to Unity in FBX format and you will see they are completely different.
     
  35. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I don't own 3DS Max, so it's not all that easy to replicate.
     
  36. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A new version of ObjReader is out on my site; should be available soon on the asset store:

    ObjReader 2.3

    Additions:
    • UseMTLFallback option, in case an OBJ file specifies a MTL file but it's missing, in which case the standard material will be used instead.
    • AutoCenterOnOrigin option, to make meshes be physically centered around (0, 0, 0) regardless of the position in the OBJ file.

    Fixes:
    • MTL files which use "tr" for transparency instead of "d" are supported.

    --Eric
     
  37. LIVENDA

    LIVENDA

    Joined:
    Oct 31, 2013
    Posts:
    275
    You do not need 3dsmax any modeling application will do. Model something and try to give it a faceted look and/or play around with the smoothing settings. There also seems to be an issue not just with smoothing but vertices not being joined properly as you can see from the image I have posted. As is the objects imported with this tool have incorrect light calculation, just compare it to it's FBX version.
     
  38. LIVENDA

    LIVENDA

    Joined:
    Oct 31, 2013
    Posts:
    275
    Have you fixed the issue I have reported above. Thanks.
     
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Please read your PMs. I'm not sure how Max works, but in Blender the usual technique is to mark seams as sharp where you want hard edges, and use the edge split modifier.

    --Eric
     
  40. Virtualware

    Virtualware

    Joined:
    Sep 17, 2012
    Posts:
    75
    Hi Eric,

    I have found a small bug. It's very easy to workaround, but maybe you could change it, so we won't need to recode with each new version release:

    Maya exported both OBJ and MTL files support names and paths with spaces. As you are just getting the first occurrence of string after splitting the line, paths are broken, and in our case textures are not found.

    Anyway I have to say that this stuff works great!, and it is really simple to integrate.

    Grettings,

    Sergio Barrera - CTO at Virtualware.
     
  41. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Hmm, I've tested it with spaces in the .obj and .mtl file names, and it seems to work fine. The .mtl file name is retrieved by using anything between the "mtllib" token and the end of the line, so it's not splitting on spaces. You can send me an example of a non-working file via PM.

    --Eric
     
  42. Virtualware

    Virtualware

    Joined:
    Sep 17, 2012
    Posts:
    75
    Check map_Kd section, I had the problem with a texture named "grayscale ramp.png", with an space between grayscale and ramp

    newmtl initialShadingGroup
    illum 4
    Kd 0.00 0.00 0.00
    Ka 0.00 0.00 0.00
    Tf 1.00 1.00 1.00
    map_Kd grayscale ramp.png
    Ni 1.00

    I know that it is not very common to use spaces in filenames, but some exporters work this way.
     
  43. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh, sorry, the texture file name, not the mtl file name. I've released 2.3.1, which fixes that issue.

    --Eric
     
  44. Virtualware

    Virtualware

    Joined:
    Sep 17, 2012
    Posts:
    75
    Thanks!

    I have found another issue:

    I post the mtl contents to help with the explanation:

    newmtl TEXTURA_Coche_01_SG
    illum 4
    Kd 0.00 0.00 0.00
    Ka 0.00 0.00 0.00
    Tf 1.00 1.00 1.00
    map_Kd Coche.png
    Ni 1.00
    Ks 0.93 0.93 0.93
    Ns 92.24

    newmtl initialShadingGroup
    illum 4
    Kd 0.50 0.50 0.50
    Ka 0.00 0.00 0.00
    Tf 1.00 1.00 1.00
    Ni 1.00

    After the definition of a material with "map_Kd texture", if the next material in file sequence does not have texture, obj loader set the same texture as previous material, instead of "none", I suppose there will be an initialization problem.
     
  45. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    OK, that's fixed for 2.3.2. Any other issues, so I don't need to make a 2.3.3? ;)

    --Eric
     
  46. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    New version:

    ObjReader 2.3.2

    Fixes:
    • Fixed issue with MTL entries that contained no texture picking up the texture from the previous entry that did contain a texture.

    --Eric
     
  47. hkha

    hkha

    Joined:
    Jul 21, 2012
    Posts:
    6
    Hello, I love your Vectrosity plugin .. Now I need a runtime importer, I was wondering if ObjReader supports vertex color data ?

    Thanks.
     
  48. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As far as I know that's not supported by the .obj format.

    --Eric
     
  49. hkha

    hkha

    Joined:
    Jul 21, 2012
    Posts:
    6
    Ah .. thanks Eric you are right, it seems some apps do allow import/export of .obj files with vertex colors. But they are breaking the standards. I guess I have to look for a ply importer.
    On more question. Does your asset also includes source code ? Just in case we end up writing/modifying a importer to match our needs.

    Thanks again :)
     
  50. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, all my assets always include source code.

    --Eric