Positioning ImportMesh objects

Hi there,

I’ve been trying to get my loaded meshes into the scene in a correct way, but I’m struggling to get it right.

This is my loader for now:

// >>> ScaleObj about specific point
export const MeshLoader = function name(scene:Scene, options:{path:string; filename:string; scale?:number}) {
    let scale:number = options.scale !== undefined ? options.scale : 1;
    let path = typeof options.path !== 'undefined' ? options.path : undefined;
    if (!path) { return }
    let filename = typeof options.filename !== 'undefined' ? options.filename : undefined;

    let rmvList:Array<string> = ['_tekst']; //, 'Bomenrij'
    let matCapM:StandardMaterial = matCapMat(scene, '../assets/textures/matcap/c2c8c37a7fd44867992bcae1e92ad125.jpg');

    var utilLayer = new UtilityLayerRenderer(scene);
    var gizmo = new PositionGizmo(utilLayer, 0.4);
    let impAxis = new TransformNode("axRoot");
    // impAxis.rotation = new Vector3(-Deg2Rad(90),0,0); // correct import rotation
    let model = SceneLoader.ImportMesh('', path, filename, scene, function (mesh) {
    // let bbMain = new BoundingInfo(new Vector3(), new Vector3())
    // let bb = new BoundingInfo(new Vector3, new Vector3);

    let parent = new Mesh("parent", scene);

        for (const o of mesh) {
            // bb.getBoundingInfo().encapsulateBoundingInfo(o.getBoundingInfo());
            // bbMain.encapsulateBoundingInfo(o.getBoundingInfo())
            // console.log(bbMain);

            let oMin = o.getBoundingInfo().boundingBox.minimum;
            let oMax = o.getBoundingInfo().boundingBox.maximum;

            let parentMin = parent.getBoundingInfo().boundingBox.minimum;
            let parentMax = parent.getBoundingInfo().boundingBox.maximum;

            let newMin = Vector3.Minimize(oMin, parentMin);
            let newMax = Vector3.Maximize(oMax, parentMax);

            parent.setBoundingInfo(new BoundingInfo(newMin, newMax));
            o.parent = parent;
            // try { o.material = matCapM } catch (error) { console.log(error) }
            // if (contains(o.name, rmvList)) { o.dispose() }
        let bb_center = parent.getBoundingInfo().boundingBox.center
        let bb_min = parent.getBoundingInfo().boundingBox.minimum
        let bb_centerbtm = new Vector3(-bb_center._x, -bb_min._y, -bb_center._z)
        // parent.setPivotPoint(bb_centerbtm)
        impAxis.position = bb_centerbtm;

        // console.log(parent.getPivotPoint());
        // parent.setPivotPoint(bb_centerbtm)
        // console.log(parent.getPivotPoint());
        // parent.position = Vector3.Zero()

        let scale_ing_vec = new Vector3(scale, scale, scale);
        parent.scaling = scale_ing_vec
        parent.translate(bb_centerbtm, 1)

        // CreateSphere('x', {diameter:1}).position = new Vector3(bb_center._x, bb_min._y, bb_center._z)
        // parent.position = new Vector3(-bb_center._x, -bb_min._y, -bb_center._z)
        // parent.position = bb_centerbtm

        parent.showBoundingBox = true;
        gizmo.attachedMesh = parent;

        // let minPos = bbMain.boundingBox.minimum._z
        // console.log(minPos);
        // impAxis.translate(new Vector3(0,1,0), minPos);
    return impAxis;
// MeshLoader(scene, {
//     path: '../../../../resources/models/BlenderExportTest_00/',
//     filename: 'chair_00.glb',
//     scale:1,
// })

I’ve commented out some tests, but left them there so you know what’s been tested here.
What I’ve tried to do here is the following:

  • Loading my assets based on path and filename
  • So the 3D model consists out of different meshes each time
    They’re loaded correctly but most of the models are saved with the pivot point not at the center bottom.
  • I’m generating a combined bounding box to find the combined center bottom for the complete model.
  • Each sub mesh is parented to an empty mesh
  • This mesh is parented to a gizmo

So a few things are going wrong in my opinion here.

  • I can’t seem to get the pivot point center bottom, it will remain in the original position
  • This causes that I have to scale the object before moving it to the origin
  • In some cases the model is rotated (but I guess this is with different exports, y/z as up vector)

So my question:

  • What’s the most efficient way to link imported meshes together
  • Getting the center bottom position
  • Placing the pivot point there
  • To in the end easily position the new pivot point at the origin (for ex) and scale it according to the new pivot point.

Thanks a lot!

Cc @PirateJC our resident blender expert

Not sure this is a Blender question @Deltakosh. @Pieter_van_Stee Can you provide a playground with this code so we can better help with your questions?

I meant: @PirateJC did that a lot for his own demos :wink:

1 Like

Only blender related issue here would be the up axis. But that’s not really an issue. Just have to export models consistently, unless there is a way to read from the model with which up axis it was exported.

But that wasn’t really the question here.
When importing a model, usually with multiple meshes my question was how to group or link the together in the most efficient way, place the pivot center bottom, and position the package with the pivot center bottom at the origin.

I’ll try to make a pg instance out of this to share.
Any tips in the mean time are welcome


Thanks @Pieter_van_Stee a PG would be super helpful.

In the meantime though, here are two ideas you might consider exploring:

  1. A simple parenting structure where a cube (with visibility turned off) is the parent of all of your objects could give you some control to explore.

  2. Asset containers is another thing you could explore: Asset Containers | Babylon.js Documentation

Hi @Pieter_van_Stee just checking in if you still have a issue :slight_smile:

Thanks for getting back to this issue.
Don’t think I got everything the way I wanted it, but I was working with downloaded models so I’d have to do a bit more testing with in-house models to be sure about the way they’re exported.

Will get back to this, when I’ve had the time to look into it.
Thanks again

1 Like