How to use OBJFileLoader with vertex colors in ES6/Vite setup?

I’m using Vite with Typescript as my build-system.
I exported a mesh in Blender to OBJ-format, with colors included.
I managed to get OBJ-file loading working by including this in the typescript file, where I import the mesh :

import * as BABYLON from '@babylonjs/core/Legacy/legacy';
import '@babylonjs/loaders/OBJ/objFileLoader';

However, to get colors of the imported obj file working (I don’t use a material or texture), the documentation says I should add the following:

BABYLON.OBJFileLoader.IMPORT_VERTEX_COLORS = true;

But this doesn’t work with my setup. BABYLON.OBJFileLoader is undefined.

Any idea how to enable vertex colors in this case?

UPDATE:
It seems the Blender exporter to OBJ is not including colors after all, so it could never work anyway. When I display the file in 3D Viewer it’s grayscale. Has anybody ever managed to use/make an OBJ file with colors?

Hey @BobDylan , how are you doing?

I found this question that seems to show how to do it in Blender:

mesh - Export Model With Material Colors to MeshLab - Blender Stack Exchange

Can you see if that works for you?

Also for the Babylon.js loader to be available you would have to import it as:

import { OBJFileLoader } from '@babylonjs/loaders/OBJ/objFileLoader';

And use it as:

OBJFileLoader.IMPORT_VERTEX_COLORS = true;

Thanks for the reply, the code works now, but I still can’t seem to export a working model from Blender to .obj format. I tried all options I could find. Windows 3D Viewer still doesn’t show the colors, though, and importing the model into Babylon fails somehow:
Unable to load from meshes/tree.obj: OK

I finally gave up on using .obj, and went for the .babylon format instead.

2 Likes