BabylonJS/Babylon.js · error

${context}: Failed to find index (${index})

Error message

${context}: Failed to find index (${index})

What it means

glTFLoader's static Get<T>() helper indexes into a loaded glTF array (buffers, images, nodes, etc.) and throws when the array is missing, the index is undefined, or no item exists at that index. This guards the loader against malformed assets referencing out-of-range or absent resources.

Source

Thrown at packages/dev/loaders/src/glTF/2.0/glTFLoader.pure.ts:115

interface IWithMetadata {
    metadata: any;
    _internalMetadata: any;
}

/**
 * Helper class for working with arrays when loading the glTF asset
 */
export class ArrayItem {
    /**
     * Gets an item from the given array.
     * @param context The context when loading the asset
     * @param array The array to get the item from
     * @param index The index to the array
     * @returns The array item
     */
    public static Get<T>(context: string, array: ArrayLike<T> | undefined, index: number | undefined): T {
        if (!array || index == undefined || !array[index]) {
            throw new Error(`${context}: Failed to find index (${index})`);
        }

        return array[index];
    }

    /**
     * Gets an item from the given array or returns null if not available.
     * @param array The array to get the item from
     * @param index The index to the array
     * @returns The array item or null
     */
    public static TryGet<T>(array: ArrayLike<T> | undefined, index: number | undefined): Nullable<T> {
        if (!array || index == undefined || !array[index]) {
            return null;
        }

        return array[index];
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Validate the glTF asset with a validator (e.g. glTF-Validator) to find dangling indices
  2. Re-export the asset from the original DCC tool / exporter
  3. Log `context` from the message — it names which array/index failed, then inspect that part of the JSON
  4. Ensure loading completed (await the load promise) before code that resolves indices runs

Example fix

// before
const buffer = buffers[gltf.bufferViews[i].buffer]; // may be undefined
// after
const idx = gltf.bufferViews[i].buffer;
if (!buffers || buffers[idx] === undefined) throw new Error(`bufferView ${i} references missing buffer ${idx}`);
const buffer = buffers[idx];
Defensive patterns

Strategy: type-guard

Validate before calling

function validateIndex<T>(array: ArrayLike<T> | undefined, index: number | undefined, context: string): void {
    if (!array || index == null || !(index in array) || array[index] == null) {
        throw new Error(`${context}: missing item at index ${index} — validate the asset with glTF-Validator before loading`);
    }
}

Type guard

function hasIndex<T>(array: ArrayLike<T> | undefined, index: number | undefined): array is ArrayLike<T> {
    return !!array && index != null && index >= 0 && index < array.length && array[index] != null;
}

Try / catch

try {
    await gltfLoader.loadAsync(container, url);
} catch (e) {
    if (String(e.message).includes("Failed to find index")) {
        console.error(`Malformed glTF asset: ${e.message} — re-export or validate with glTF-Validator`);
    } else throw e;
}

Prevention

When it happens

Trigger: A glTF JSON references an index that doesn't exist — e.g. a mesh primitive accessor index beyond `accessors.length`, `bufferView.buffer` pointing past `buffers`, or `scene.nodes` containing an index not in `nodes`; also calling Get with undefined index/array while data is still loading.

Common situations: Corrupt or hand-edited glTF files; assets produced by buggy exporters leaving dangling indices; partial loads where binary data hasn't arrived but JSON indices are resolved; using loader state before the JSON is parsed.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/3232fe1161e2066a. Report an issue: GitHub.