From 48eac350712598508a04c4f01013ac0a8b2ba99d Mon Sep 17 00:00:00 2001
From: Sankhesh Jhaveri <sankhesh.jhaveri@kitware.com>
Date: Mon, 13 Jan 2025 10:18:27 -0500
Subject: [PATCH] GLTF document loader caches a default scene

---
 Documentation/release/dev/gltf-documentloader-scene.md | 4 ++++
 IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx         | 6 ++++++
 2 files changed, 10 insertions(+)
 create mode 100644 Documentation/release/dev/gltf-documentloader-scene.md

diff --git a/Documentation/release/dev/gltf-documentloader-scene.md b/Documentation/release/dev/gltf-documentloader-scene.md
new file mode 100644
index 00000000000..5a6ba3b5e0d
--- /dev/null
+++ b/Documentation/release/dev/gltf-documentloader-scene.md
@@ -0,0 +1,4 @@
+## GLTF document loader default scene
+
+The `vtkGLTFDocumentLoader` ensures that the internal cache of scenes is non-empty for consumer code
+that have to rely on checking validity of the default scene.
diff --git a/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx b/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
index d70da8486d2..dcb51743202 100644
--- a/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
+++ b/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
@@ -1473,6 +1473,12 @@ bool vtkGLTFDocumentLoaderInternals::LoadModelMetaData(
   if (!vtkGLTFUtils::GetIntValue(root, "scene", this->Self->GetInternalModel()->DefaultScene))
   {
     int nbScenes = static_cast<int>(this->Self->GetInternalModel()->Scenes.size());
+    if (nbScenes < 1)
+    {
+      // In case the file had no scenes, add an empty scene so that downstream code doesn't need to
+      // check that its default scene index is valid
+      this->Self->GetInternalModel()->Scenes.resize(1);
+    }
     if (this->Self->GetInternalModel()->DefaultScene < 0 ||
       this->Self->GetInternalModel()->DefaultScene >= nbScenes)
     {
-- 
GitLab