From 09962210177be8680c4f859e6a33a775a15908a2 Mon Sep 17 00:00:00 2001 From: Jan Honsbrok Date: Wed, 5 Nov 2025 08:24:41 +0100 Subject: [PATCH] fix: write textures to folder of PBRT file (#6099) * fix: write textures to folder of PBRT file --- code/Pbrt/PbrtExporter.cpp | 20 +++++++++++++------- code/Pbrt/PbrtExporter.h | 5 ++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/code/Pbrt/PbrtExporter.cpp b/code/Pbrt/PbrtExporter.cpp index 07bf1f056..0cc30e668 100644 --- a/code/Pbrt/PbrtExporter.cpp +++ b/code/Pbrt/PbrtExporter.cpp @@ -93,17 +93,22 @@ void ExportScenePbrt(const char *pFile, IOSystem *pIOSystem, const aiScene *pSce const ExportProperties *) { std::string path = DefaultIOSystem::absolutePath(std::string(pFile)); std::string file = DefaultIOSystem::completeBaseName(std::string(pFile)); + std::string texturesPath = path; + if (!texturesPath.empty()) { + texturesPath+=pIOSystem->getOsSeparator(); + } + texturesPath+="textures"; // initialize the exporter - PbrtExporter exporter(pScene, pIOSystem, path, file); + PbrtExporter exporter(pScene, pIOSystem, path, file, texturesPath); } } // end of namespace Assimp -static void create_embedded_textures_folder(const aiScene *scene, IOSystem *pIOSystem) { +static void create_embedded_textures_folder(const aiScene *scene, IOSystem *pIOSystem, const std::string &texturesPath) { if (scene->mNumTextures > 0) { - if (!pIOSystem->Exists("textures")) { - if (!pIOSystem->CreateDirectory("textures")) { + if (!pIOSystem->Exists(texturesPath)) { + if (!pIOSystem->CreateDirectory(texturesPath)) { throw DeadlyExportError("Could not create textures/ directory."); } } @@ -112,11 +117,12 @@ static void create_embedded_textures_folder(const aiScene *scene, IOSystem *pIOS PbrtExporter::PbrtExporter( const aiScene *pScene, IOSystem *pIOSystem, - const std::string &path, const std::string &file) : + const std::string &path, const std::string &file, const std::string &texturesPath) : mScene(pScene), mIOSystem(pIOSystem), mPath(path), mFile(file), + mTexturesPath(texturesPath), mRootTransform( // rotates the (already left-handed) CRS -90 degrees around the x axis in order to // make +Z 'up' and +Y 'towards viewer', as in default in pbrt @@ -134,7 +140,7 @@ PbrtExporter::PbrtExporter( ) * mRootTransform; // Export embedded textures. - create_embedded_textures_folder(mScene, mIOSystem); + create_embedded_textures_folder(mScene, mIOSystem, mTexturesPath); for (unsigned int i = 0; i < mScene->mNumTextures; ++i) { aiTexture* tex = mScene->mTextures[i]; @@ -684,7 +690,7 @@ std::string PbrtExporter::CleanTextureFilename(const aiString &f, bool rewriteEx } // Expect all textures in textures - fn = std::string("textures") + mIOSystem->getOsSeparator() + fn; + fn = mTexturesPath + mIOSystem->getOsSeparator() + fn; // Rewrite extension for unsupported file formats. if (rewriteExtension) { diff --git a/code/Pbrt/PbrtExporter.h b/code/Pbrt/PbrtExporter.h index 71a906e87..bc596e5d2 100644 --- a/code/Pbrt/PbrtExporter.h +++ b/code/Pbrt/PbrtExporter.h @@ -74,7 +74,7 @@ class PbrtExporter { public: /// Constructor for a specific scene to export PbrtExporter(const aiScene *pScene, IOSystem *pIOSystem, - const std::string &path, const std::string &file); + const std::string &path, const std::string &file, const std::string &texturesPath); /// Destructor virtual ~PbrtExporter() = default; @@ -114,6 +114,9 @@ private: /// Name of the file (without extension) where the scene will be exported const std::string mFile; + /// Path of the textures directory (inlcuding seperator) + const std::string mTexturesPath; + // A private set to keep track of which textures have been declared std::set mTextureSet;