{"record":{"id":"1ae18efb730c50dd","repo":"BabylonJS/Babylon.js","slug":"cannot-get-the-last-selected-variant-on-a-gltf-mes","errorCode":null,"errorMessage":"Cannot get the last selected variant on a glTF mesh that does not have the ${NAME} extension","messagePattern":"Cannot get the last selected variant on a glTF mesh that does not have the (.+?) extension","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_variants.pure.ts","lineNumber":167,"sourceCode":"    }\r\n\r\n    /**\r\n     * Reset back to the original before selecting a variant.\r\n     * @param rootNode The glTF root node\r\n     */\r\n    public reset(rootNode: TransformNode): void {\r\n        KHR_materials_variants.Reset(rootNode);\r\n    }\r\n\r\n    /**\r\n     * Gets the last selected variant name(s) or null if original.\r\n     * @param rootNode The glTF root node\r\n     * @returns The selected variant name(s).\r\n     */\r\n    public static GetLastSelectedVariant(rootNode: TransformNode): Nullable<string | string[]> {\r\n        const extensionMetadata = this._GetExtensionMetadata(rootNode);\r\n        if (!extensionMetadata) {\r\n            throw new Error(`Cannot get the last selected variant on a glTF mesh that does not have the ${NAME} extension`);\r\n        }\r\n\r\n        return extensionMetadata.lastSelected;\r\n    }\r\n\r\n    /**\r\n     * Gets the last selected variant name(s) or null if original.\r\n     * @param rootNode The glTF root node\r\n     * @returns The selected variant name(s).\r\n     */\r\n    public getLastSelectedVariant(rootNode: TransformNode): Nullable<string | string[]> {\r\n        return KHR_materials_variants.GetLastSelectedVariant(rootNode);\r\n    }\r\n\r\n    private static _GetExtensionMetadata(rootNode: Nullable<TransformNode>): Nullable<IExtensionMetadata> {\r\n        return rootNode?._internalMetadata?.gltf?.[NAME] || null;\r\n    }\r\n\r","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_variants.pure.ts#L149-L185","documentation":"KHR_materials_variants stores per-variant mesh data in the node's extension metadata. GetLastSelectedVariant reads the metadata's 'lastSelected' record from the root TransformNode. If the mesh was never loaded with the KHR_materials_variants extension (or the wrong node is passed), there is no metadata and Babylon throws this error instead of returning a misleading null.","triggerScenarios":"CallingKHRMaterialExtensions.GetLastSelectedVariant(rootNode) on a TransformNode whose metadata lacks the extension's metadata entry — e.g. the glTF asset never declared the KHR_materials_variants extension, or the node passed is not the glTF root node created by the loader.","commonSituations":"Passing a child mesh or an unrelated TransformNode instead of the root node returned by the loader; loading a model that uses other KHR extensions but not materials_variants; constructing meshes procedurally and assuming variant APIs exist.","solutions":["Pass the glTF root TransformNode returned by SceneLoader.ImportMesh/LoadAssetContainerAsync, not a child mesh.","Verify the .gltf/.glb file actually declares KHR_materials_variants in extensionsUsed.","Check GetExtensionMetadata (or node.metadata.gltfExtensions) for the extension before calling the API; fall back to selecting variants via the loader's promises otherwise."],"exampleFix":"// before\nconst variant = KHRMaterialExtensions.GetLastSelectedVariant(anyMesh);\n// after\nconst variant = KHRMaterialExtensions.GetLastSelectedVariant(rootNodes[0]); // actual glTF root with variant metadata","handlingStrategy":"validation","validationCode":"const meta = KHRMaterialExtensions.GetExtensionMetadata(rootNode);\nif (!meta || !meta.variants) throw new Error('Asset does not use KHR_materials_variants');","typeGuard":"function hasVariantMetadata(node: TransformNode): boolean {\n  return !!(node.metadata as any)?.gltf?.extensions?.['KHR_materials_variants'];\n}","tryCatchPattern":null,"preventionTips":["Always pass the glTF root node (result[0] of transformNodes from ImportMesh) to variant APIs.","Check the asset's extensionsUsed for KHR_materials_variants before using variant APIs.","Wrap variant selection in a check for metadata presence at load time."],"tags":["gltf","extensions","metadata","invalid-argument"],"backgroundTag":"missing-gltf-extension","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}