From 2034b8807d6c50788a37fb268afa59805e9e5bcf Mon Sep 17 00:00:00 2001
From: Mathieu Westphal <mathieu.westphal@kitware.com>
Date: Mon, 17 Feb 2025 08:36:33 +0100
Subject: [PATCH] OBJImporter: Better default TexturePath

---
 IO/Import/vtkOBJImporter.cxx        |  8 +++++++-
 IO/Import/vtkOBJImporterInternals.h | 26 ++++++++++++++++----------
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/IO/Import/vtkOBJImporter.cxx b/IO/Import/vtkOBJImporter.cxx
index 5f76c1955d3..47564142619 100644
--- a/IO/Import/vtkOBJImporter.cxx
+++ b/IO/Import/vtkOBJImporter.cxx
@@ -211,7 +211,8 @@ vtkOBJPolyDataProcessor::vtkOBJPolyDataProcessor()
   this->FileName = "";
   this->MTLFileName = "";
   this->DefaultMTLFileName = true;
-  this->TexturePath = "./";
+  this->TexturePath = "";
+  this->DefaultTexturePath = true;
   this->VertexScale = 1.0;
   this->SuccessParsingFiles = 1;
   this->SetNumberOfInputPorts(0);
@@ -483,6 +484,11 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
     }
   }
 
+  if (this->DefaultTexturePath)
+  {
+    this->SetTexturePath(vtksys::SystemTools::GetFilenamePath(this->FileName).c_str());
+  }
+
   int mtlParseResult;
   this->parsedMTLs = ParseOBJandMTL(this->MTLFileName, mtlParseResult);
   if (this->parsedMTLs.empty())
diff --git a/IO/Import/vtkOBJImporterInternals.h b/IO/Import/vtkOBJImporterInternals.h
index 1f43bfb7727..46aa2efe2b5 100644
--- a/IO/Import/vtkOBJImporterInternals.h
+++ b/IO/Import/vtkOBJImporterInternals.h
@@ -57,7 +57,7 @@ public:
     {
       return;
     }
-    FileName = std::string(arg);
+    this->FileName = std::string(arg);
   }
   void SetMTLfileName(const char* arg)
   {
@@ -69,27 +69,32 @@ public:
     {
       return;
     }
-    MTLFileName = std::string(arg);
+    this->MTLFileName = std::string(arg);
     this->DefaultMTLFileName = false;
   }
   void SetTexturePath(const char* arg)
   {
-    TexturePath = std::string(arg);
-    if (TexturePath.empty())
+    this->TexturePath = std::string(arg);
+    if (this->TexturePath.empty())
+    {
       return;
+    }
 #if defined(_WIN32)
     const char sep = '\\';
 #else
     const char sep = '/';
 #endif
-    if (TexturePath.at(TexturePath.size() - 1) != sep)
-      TexturePath += sep;
+    if (this->TexturePath.at(this->TexturePath.size() - 1) != sep)
+    {
+      this->TexturePath += sep;
+    }
+    this->DefaultTexturePath = false;
   }
-  const std::string& GetTexturePath() const { return TexturePath; }
+  const std::string& GetTexturePath() const { return this->TexturePath; }
 
-  const std::string& GetFileName() const { return FileName; }
+  const std::string& GetFileName() const { return this->FileName; }
 
-  const std::string& GetMTLFileName() const { return MTLFileName; }
+  const std::string& GetMTLFileName() const { return this->MTLFileName; }
 
   vtkSetMacro(VertexScale, double);
   vtkGetMacro(VertexScale, double);
@@ -132,8 +137,9 @@ protected:
 
   std::string FileName;    // filename (.obj) being read
   std::string MTLFileName; // associated .mtl to *.obj, typically it is *.obj.mtl
-  bool DefaultMTLFileName; // tells whether default of *.obj.mtl to be used
+  bool DefaultMTLFileName; // tells whether default MTL should be used
   std::string TexturePath;
+  bool DefaultTexturePath; // tells whether default texture path should be used
   int SuccessParsingFiles;
 
 private:
-- 
GitLab