Parsing .OBJ

I’m currently parsing some .obj files in Grasshopper - pushing all the informations into the corresponding properties. Lets say the file has 100000 vertices (“v”) the face-information (“f”) but 105000 texture-coordinates (“vt”) from a mtl.

Collecting all the vt information in Mesh.TextureCoordinateList - reordering with the vt-information from the faces (f v-index/vt-index) → SetTextureCoordinate(v-index, Mesh.TextureCoordinateList[vt-index]) works fine and produces a valid mesh.

But Rhino somehow behaves different, when using the internal file-import in gh and creates additional vertices. (now 105000 and “different” from the first mesh)

I didn’t found this piece of code in rhino-common and can’t understand the magic behind the additional vertex-creation.

Thanks for your help.

The reason there are more textures than vertices is because some vertices have more than one texture. You will find that the list of vertices and textures associated with each face calls out different textures with the same vertex as you look thru all the face entries of the .obj file. So to make a mesh out of this information you have to add a new vertex whenever a face calls out a different texture for a vertex that has already been used to create a mesh face. And then you have to ensure that the new vertex has the same index as the extra texture.

I have done this in a Python script which I can send you once I get back to the office.

Regards,
Terry.

Hey Terry,

this would be great!

Best Ronny

Ronny,

Here is the Python script:
OBJ_Import_Example_For_Forum.py (116.8 KB)
Look at lines 2180 to 2209 for details on how to deal with extra textures.

The script also supports calling a C++ DLL for higher performance. But that gets into more complications. So first I recommend you look over the Python code. It gets complicated enough. It also uses parallel execution for higher performance so this makes it a bit more difficult as well but the parallelism is only in a few places.

After you master your Python script for importing a mesh from an .obj file and want more performance, then I can try and set you up with the DLL if you have a Windows machine (the DLL does not work on Apple).

Regards,
Terry.