Uncaught Error: Missing method 'Mesh' when trying to use MeshWriter

Hello

I created a Babylon Scene that uses MeshWriter and this is all working fine when using the Javascript libraries. I then tried to move my scene to Typescript using WebPack. I installed both babylonJS and MeshWriter using npm. BabylonJS is working fine but when I include MeshWriter, I get the following error

bundleName.js:12196 Uncaught Error: Missing method ‘Mesh’
at _ (meshwriter.min.js:2)
at Object.eval (meshwriter.min.js:2)
at Object.eval (meshwriter.min.js:2)
at Object.eval (meshwriter.min.js:2)
at D (meshwriter.min.js:1)
at eval (meshwriter.min.js:1)
at eval (meshwriter.min.js:2)
at Object…/node_modules/meshwriter/dist/meshwriter.min.js (bundleName.js:11896)
at webpack_require (bundleName.js:12193)
at fn (bundleName.js:12404)

I have the following includes in my typescript file

import “@babylonjs/core/Debug/debugLayer”;
import “@babylonjs/inspector”;
import “@babylonjs/loaders/glTF”;
import “@babylonjs/gui”;
import * as BABYLON from “@babylonjs/core”;
import * as $ from ‘jquery’;
import * as MeshWriter from “meshwriter”;

This is the MeshWriter code that causes the issue

            var letterheight = (d.height + d.width) * 2;
            
 
                let Writer = MeshWriter(scene, { scale: 1 });
                let text1 = new Writer(
                    'A' + String(_aisles).padStart(2, '0'),
                    {
                        "anchor": "left",
                        "letter-height": letterheight,
                        "color": "#1C3870",
                        "position": {
                            "x": (-d.width * (d.size / 2)) + d.gap + (letterheight / 2) + (letterheight / d.height),
                            "z": -d.depth * (d.size / 2),
                            "y": 0
                        }

                    });

What I can do to resolve this issue?

@TheLeftover can answer that the best, as he is the one who wrote the MeshWriter, but from a quick glance at the code, it seems like it expects BABYLON to be in the global namespace.

A quick search came up with this - MeshWriter integration in Angular+Babylon project , so it does seem like you will need to define window.BABYLON yourself to use it.

RaananW, it used to require a global BABYLON object. However, if you check all the way down to the bottom of the READEME (GitHub - briantbutton/meshwriter: Babylon Mesh Writer) under “optimizing memory”, it describes a way to pass the methods into MeshWriter.

I can see that the documentation coulda been clearer on that point . . . wonder who wrote that?

The syntax is showed below. Also you can take a look at this playground (https://www.babylonjs-playground.com/#PL752W#384) to see the syntax being used.

        Writer = MeshWriter(scene, {scale:scale, methods:myMethodsObject});

@mark.rowland, I hope this solves your problem.

1 Like

@TheLeftover , Thanks for your reply but I still have issues. The playground URL you posted doesn’t work for me. Does it actually work for you?

I did try the following but it still didn’t work for me

let Writer = MeshWriter(scene, { scale: 1 , methods: BABYLON});

Please can you elaborate further on what you feel is needed to resolve this issue

Huh.

Pretty surprised about the playground link. What happens when you click it?

Assuming that you still get the same error message, let me ask this question: does the methods object (in this case, BABYLON) have the method ‘Mesh’?

I checked NPM for a version error and saw no problem.

@TheLeftover , The Link you provided and the one in the documentation is https://www.babylonjs-playground.com/#PL752W#384. I have just spotted the issue with the link, it should be this https://playground.babylonjs.com/#PL752W#384

This is now working for me and I can see the example and download it and run it fine but it won’t work in the project I have which is based on

Getting Set Up | Babylon.js Documentation (babylonjs.com)

I discovered that if I use the BabylonJS Editor and add MeshWriter into a scene, it is working although you don’t see the results in the editor itself. If you hit Run, you do see them

@TheLeftover , I finally managed to resolve the issue without the use of the BabylonJS Editor. The issue was because I had the MeshWriter import after the BabylonJs inspector import.

When I reversed this so that MeshWriter was before Inspector as below, the issue was resolved

import * as MeshWriter from “meshwriter”;
import “@babylonjs/inspector”;