# Sandbox: loading obj never ends

**URL:** <https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610>\
**Category:** Bugs\
**Tags:** sandbox, obj\
**Created:** [August 7, 2024, 7:39am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610 "2024-08-07T07:39:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [August 7, 2024, 7:39am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/1 "2024-08-07T07:39:33Z")

</div>

OS: Windows 10  
GPU: GTX 1050 2GB  
Browser: Chrome and Firefox  
Ver: Babylon.js v7.19.1  
Url: [https://sandbox.babylonjs.com/](https://sandbox.babylonjs.com/)  
Expected: Loaded successfully (or tell the user that it’s not supported)

 ![online3dviewer](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/b/3/b3ed6b597f63836e0acb5368e600d82aedf9cbb4.png)

Actual: Loading never ends

 ![sandbox](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/e/5ee13348a1593292e330b391ef05ad56ac7664f3.png)

Model attached here:  
[lines\_with\_vt.zip](https://forum.babylonjs.com/uploads/short-url/aDXOZue9RPlCLRlxGZ6XiScRLdG.zip) (235 Bytes)

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 7, 2024, 9:50am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/2 "2024-08-07T09:50:51Z")

</div>

> [@kzhsw](#):
>
> Expected: Loaded successfully

Your .obj file doesn’t have UVs. That’s why it doesn’t load the file. @sebavan @deltakosh should we support UVless OBJs?

Solution, export the OBJ with UVs:

> **[Babylon.js Playground](https://playground.babylonjs.com/#QLACPN%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

> [@kzhsw](#):
>
> (or tell the user that it’s not supported)

The console displays an error.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 7, 2024, 1:59pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/3 "2024-08-07T13:59:57Z")

</div>

We should (and to be honest I thought we were already:))

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 7, 2024, 2:46pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/4 "2024-08-07T14:46:42Z")

</div>

Ok, I’ll take care of it.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 7, 2024, 2:55pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/5 "2024-08-07T14:55:59Z")

</div>

Love you to the moon and back 😉

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 7, 2024, 7:17pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/6 "2024-08-07T19:17:28Z")

</div>

We have a bigger issue here with the `obj` not just the Exception when there are no UVs.

The `objLoader` is faking lines by setting the mesh’s material `wireframe` to `true`.

It works when the obj contains `l` elements defined by exactly two vertices and the line doesn’t share vertices. This works with `wireframe`:

```auto
l 1 2
l 3 4
l 5 6

```

3 lines w/o vertex sharing.

However `obj` suport multiple vertices in the `l` element. Both examples produces a mesh with shared vertices.

```auto
l 1 2 3 4 5 6 1

```

```auto
l 1 2
l 2 3
l 3 4
l 4 5
l 5 6
l 6 1

```

`wireframe` is not applicable with these.

I’ll need to create a `LineMesh`/`GreasedLineMesh` from the `l` elements or should we draw the mesh in `MATERIAL_LineListDrawMode`. Which approach do you prefer guys?

Thanks!

@Deltakosh @sebavan

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 7, 2024, 7:32pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/7 "2024-08-07T19:32:54Z")

</div>

Probably the latter?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [August 7, 2024, 8:54pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/8 "2024-08-07T20:54:07Z")

</div>

same

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 7, 2024, 9:05pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/9 "2024-08-07T21:05:30Z")

</div>

I’ll need to add new vertices to the resulting mesh. The number of vertices in the `obj` will be different as the number of vertices of the mesh on scene  
since `l 1 2 3 4` will result into

```auto
l 1 2
l 2-cloned 3
l 3-cloned 4

```

Are you OK with this?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 7, 2024, 9:32pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/10 "2024-08-07T21:32:03Z")

</div>

No problem!

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 8, 2024, 6:01am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/11 "2024-08-08T06:01:08Z")

</div>

Guys, I decided to choose the easiest way to make lines working.  
No material change, no `LINES` mode rendering for the mesh. These parts remained untouched. So it still uses the `wireframe` trick to render the lines.

I just preprocess the lines elements from the OBJ. See the description:

> <https://github.com/BabylonJS/Babylon.js/pull/15382#issue-2454922614>
>
> Since \`l 1 2\` works but \`l 1 2 3 ...\` with more than 2 vertices doesn't work, th…e line data are preprocessed and \`l 1 2 3 4\` is split into multiple \`l 1 2\` \`l 2 3\` \`l 3 4\`. After this action the whole OBJ is processed.

> Lines and polygons are described in terms of their points, while curves and surfaces are defined with control points and other information depending on the type of curve. The format supports rational and non-rational curves, including those based on Bezier, B-spline, Cardinal (Catmull-Rom splines), and Taylor equations.

Source, OBJ format: [https://people.computing.clemson.edu/~dhouse/courses/405/docs/brief-obj-file-format.html](https://people.computing.clemson.edu/~dhouse/courses/405/docs/brief-obj-file-format.html)

There could be other issues in the future. For example mixing lines and faces in one `obj` file are not supported. We don’t support curves.

Displays the OP’s OBJ file:

> **[Babylon.js Playground](https://playground.babylonjs.com/?snapshot=refs%2Fpull%2F15382%2Fmerge#QLACPN%2313)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```js
l 1/1 2/2
l 3/3 4/4
l 5/5 6/6 7/7 8/8 5/5

```

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [August 8, 2024, 9:40am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/12 "2024-08-08T09:40:49Z")

</div>

Also, if loading invalid (e.g. truncated) obj, it would still stuck there.

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 8, 2024, 3:28pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/13 "2024-08-08T15:28:15Z")

</div>

Oh so you want a visual feedback on the screen, like a messsage saying there was an error loading the file.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 8, 2024, 4:24pm UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/14 "2024-08-08T16:24:33Z")

</div>

yeah but that’s not the engine goal 🙂

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [August 9, 2024, 12:25am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/15 "2024-08-09T00:25:36Z")

</div>

In case of the sandbox, yes. In case of the api, Promise should be rejected, or `onError` callback should be called, in case of invalid (or unsupported) obj files

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 9, 2024, 12:49am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/16 "2024-08-09T00:49:28Z")

</div>

I could agree with that

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 10, 2024, 6:28am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/17 "2024-08-10T06:28:56Z")

</div>

Added error message when trying to load a corrupted OBJ file:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/8/d/8d21e98ed155e4787a21651c54c94a7ab3f4bb77.jpeg)

> <https://github.com/BabylonJS/Babylon.js/pull/15403>
>
> Added global error handler for unhandled rejections and exceptions.
> Displays an… error message in case of such errors directly in the sandbox and hides the loader.

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [August 12, 2024, 12:57am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/18 "2024-08-12T00:57:56Z")

</div>

Thanks, that fixes the loading of the file in the main thread, but:

1. Loading bad obj file will still result in never-ending loading with error only in console.  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/b/3/b307aa76535a770bbac14a14d7f7dc15813d6444.png)  
[bad\_obj.zip](https://forum.babylonjs.com/uploads/short-url/h0gDVj03y0hzNP9dAoW4XkkSPPe.zip) (225 Bytes)
2. What about devs using babylon.js? Not all of them can use a global error listener to handle. Since most [methods](https://github.com/BabylonJS/Babylon.js/blob/11537810d961231017e0f9ad1fe027290526e47a/packages/dev/loaders/src/OBJ/objFileLoader.ts#L175) in OBJFileLoader returns Promise, the Promise should be rejected, instead of throwing uncaught errors.

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 12, 2024, 10:19am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/19 "2024-08-12T10:19:11Z")

</div>

> [@kzhsw](#):
>
> ult in never-ending loading with error only in console.

Works for me locally. I’ll check what’s the issue.

> [@kzhsw](#):
>
> What about devs using babylon.js? Not all of them can use a global error listener to handle. Since most [methods](https://github.com/BabylonJS/Babylon.js/blob/11537810d961231017e0f9ad1fe027290526e47a/packages/dev/loaders/src/OBJ/objFileLoader.ts#L175) in OBJFileLoader returns Promise, the Promise should be rejected, instead of throwing uncaught errors.

I’ll create a PR for this.

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 12, 2024, 10:37am UTC](https://forum.babylonjs.com/t/sandbox-loading-obj-never-ends/52610/21 "2024-08-12T10:37:54Z")

</div>

> [@roland](#):
>
> Works for me locally. I’ll check what’s the issue.

Interestingly when I load the Sandbox from the PR snapshot:

> **[Babylon.js Sandbox - View glTF, glb, obj and babylon files](https://sandbox.babylonjs.com/?snapshot=refs%2Fpull%2F15403%2Fmerge)**
>
> Viewer for glTF, glb, obj and babylon files powered by Babylon.js

which registers this handler:

```auto
window.onunhandledrejection = (event) => {
    // eslint-disable-next-line no-console
    console.error("Unhandled promise rejection:", event.reason);

    return true;
};

```

after the page loads  
`window.onunhandledrejection` is `null`.

Let’s wait for the PR merge and check the result.
