Import MeshWriter extension in typescript project

Hello,

i am trying to add MeshWriter extension to my project. I am using typescript and npm modules, i have installed:

npm i @babylonjs/core

and i am importing stuff from babylon:

import {Scene, ...} from '@babylonjs/core';

now i want to add MeshWriter, so i installed it with:

npm i meshwriter

and i am trying to use it:

import MeshWriter from 'meshwriter';
let Writer = new MeshWriter(scene, { scale: 0.1, defaultFont: "Arial" });
let text1 = new Writer(
    "text",
    ...config
);

new Writer() fails:

BABYLON is not defined
    at c (meshwriter.min.js:7)
    at new MeshWriter (meshwriter.min.js:7

So mesh writer expects global BABYLON, but it is not defined in my setup.
Any ideas how to solve this? Thanks

L

Yeah, that’s an interesting issue…

The only workaround I see is importing the BABYLON namespace and setting window.BABYLON. Something along the lines of this:

import * as BABYLON from '@babylonjs/core';
(window as any).BABYLON = BABYLON;

The MesWriter has no UMD / modules support (at least not according to the code) so you will need to define babylon globally.

You can try submitting an issue here - GitHub - briantbutton/meshwriter: Babylon Mesh Writer , the developer might have a better solution

1 Like