AssetContainer: crash in instantiateModelsToScene when using predicate

I have some models that share one babylon file and single skeleton.
Think various selectable hair styles like this:

avatar.babylon
- torsoMesh
- headMesh
- hair1Mesh
- hair2Mesh
- hair3Mesh
- skeleton

All the meshes are assigned the same skeleton.
I am trying to load them like this:

    const i = assetContainer.instantiateModelsToScene(name => name, false, {
        predicate: entity => ["torsoMesh", "headMesh", "hair1Mesh", "skeleton"].includes(entity.name)
    });

Please note, that only one of the 3 hair styles in selected in the predicate.
In such a situation (a skeleton is instantiated, but some meshes that have that skeleton assigned are not), instantiateModelsToScene() crashes here, because copy is not defined

            for (const m of this.meshes) {
                if (m.skeleton === s && !m.isAnInstance) {
                    const copy = storeMap[conversionMap[m.uniqueId]] as Mesh;
/* โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“ */
                    if (copy.isAnInstance) {
/* โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘ */
                        continue;
                    }
                    copy.skeleton = clone;

                    if (alreadySwappedSkeletons.indexOf(clone) !== -1) {
                        continue;
                    }

                    alreadySwappedSkeletons.push(clone);

                    // Check if bones are mesh linked
                    for (const bone of clone.bones) {
                        if (bone._linkedTransformNode) {
                            bone._linkedTransformNode = storeMap[conversionMap[bone._linkedTransformNode.uniqueId]];
                        }
                    }
                }
            }

I believe that checking if copy is null should be enough to fix it.
I am sorry I can not share the concrete model, but the playground below demonstrates the crash.

1 Like

Thanks a lot for the thorough review, will be fixed by Fix asset Container crash with predicate by sebavan ยท Pull Request #13720 ยท BabylonJS/Babylon.js ยท GitHub

3 Likes