Merge pull request #3894 from crud89/master

Add export property to control blob names.
pull/3917/head^2
Kim Kulling 2021-05-27 15:16:01 +02:00 committed by GitHub
commit 57b1040a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 19 deletions

View File

@ -343,9 +343,11 @@ const aiExportDataBlob* Exporter::ExportToBlob( const aiScene* pScene, const cha
delete pimpl->blob; delete pimpl->blob;
pimpl->blob = nullptr; pimpl->blob = nullptr;
} }
auto baseName = pProperties ? pProperties->GetPropertyString(AI_CONFIG_EXPORT_BLOB_NAME, AI_BLOBIO_MAGIC) : AI_BLOBIO_MAGIC;
std::shared_ptr<IOSystem> old = pimpl->mIOSystem; std::shared_ptr<IOSystem> old = pimpl->mIOSystem;
BlobIOSystem* blobio = new BlobIOSystem(); BlobIOSystem *blobio = new BlobIOSystem(baseName);
pimpl->mIOSystem = std::shared_ptr<IOSystem>( blobio ); pimpl->mIOSystem = std::shared_ptr<IOSystem>( blobio );
if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName(), pPreprocessing, pProperties)) { if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName(), pPreprocessing, pProperties)) {

View File

@ -194,8 +194,14 @@ class BlobIOSystem : public IOSystem {
friend class BlobIOStream; friend class BlobIOStream;
typedef std::pair<std::string, aiExportDataBlob *> BlobEntry; typedef std::pair<std::string, aiExportDataBlob *> BlobEntry;
public: public:
BlobIOSystem() { BlobIOSystem() :
baseName{AI_BLOBIO_MAGIC} {
}
BlobIOSystem(const std::string &baseName) :
baseName(baseName) {
} }
virtual ~BlobIOSystem() { virtual ~BlobIOSystem() {
@ -207,27 +213,32 @@ public:
public: public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
const char *GetMagicFileName() const { const char *GetMagicFileName() const {
return AI_BLOBIO_MAGIC; return baseName.c_str();
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
aiExportDataBlob *GetBlobChain() { aiExportDataBlob *GetBlobChain() {
const auto magicName = std::string(this->GetMagicFileName());
const bool hasBaseName = baseName != AI_BLOBIO_MAGIC;
// one must be the master // one must be the master
aiExportDataBlob *master = nullptr, *cur; aiExportDataBlob *master = nullptr, *cur;
for (const BlobEntry &blobby : blobs) { for (const BlobEntry &blobby : blobs) {
if (blobby.first == AI_BLOBIO_MAGIC) { if (blobby.first == magicName) {
master = blobby.second; master = blobby.second;
master->name.Set(hasBaseName ? blobby.first : "");
break; break;
} }
} }
if (!master) { if (!master) {
ASSIMP_LOG_ERROR("BlobIOSystem: no data written or master file was not closed properly."); ASSIMP_LOG_ERROR("BlobIOSystem: no data written or master file was not closed properly.");
return nullptr; return nullptr;
} }
master->name.Set("");
cur = master; cur = master;
for (const BlobEntry &blobby : blobs) { for (const BlobEntry &blobby : blobs) {
if (blobby.second == master) { if (blobby.second == master) {
continue; continue;
@ -236,9 +247,13 @@ public:
cur->next = blobby.second; cur->next = blobby.second;
cur = cur->next; cur = cur->next;
// extract the file extension from the file written if (hasBaseName) {
const std::string::size_type s = blobby.first.find_first_of('.'); cur->name.Set(blobby.first);
cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s + 1)); } else {
// extract the file extension from the file written
const std::string::size_type s = blobby.first.find_first_of('.');
cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s + 1));
}
} }
// give up blob ownership // give up blob ownership
@ -283,6 +298,7 @@ private:
} }
private: private:
std::string baseName;
std::set<std::string> created; std::set<std::string> created;
std::vector<BlobEntry> blobs; std::vector<BlobEntry> blobs;
}; };

View File

@ -205,16 +205,22 @@ struct aiExportDataBlob {
void *data; void *data;
/** Name of the blob. An empty string always /** Name of the blob. An empty string always
indicates the first (and primary) blob, * indicates the first (and primary) blob,
which contains the actual file data. * which contains the actual file data.
Any other blobs are auxiliary files produced * Any other blobs are auxiliary files produced
by exporters (i.e. material files). Existence * by exporters (i.e. material files). Existence
of such files depends on the file format. Most * of such files depends on the file format. Most
formats don't split assets across multiple files. * formats don't split assets across multiple files.
*
If used, blob names usually contain the file * If used, blob names usually contain the file
extension that should be used when writing * extension that should be used when writing
the data to disc. * the data to disc.
*
* The blob names generated can be influenced by
* setting the #AI_CONFIG_EXPORT_BLOB_NAME export
* property to the name that is used for the master
* blob. All other names are typically derived from
* the base name, by the file format exporter.
*/ */
C_STRUCT aiString name; C_STRUCT aiString name;

View File

@ -1075,6 +1075,23 @@ enum aiComponent
*/ */
#define AI_CONFIG_EXPORT_POINT_CLOUDS "EXPORT_POINT_CLOUDS" #define AI_CONFIG_EXPORT_POINT_CLOUDS "EXPORT_POINT_CLOUDS"
/**
* @brief Specifies the blob name, assimp uses for exporting.
*
* Some formats require auxiliary files to be written, that need to be linked back into
* the original file. For example, OBJ files export materials to a separate MTL file and
* use the `mtllib` keyword to reference this file.
*
* When exporting blobs using #ExportToBlob, assimp does not know the name of the blob
* file and thus outputs `mtllib $blobfile.mtl`, which might not be desired, since the
* MTL file might be called differently.
*
* This property can be used to give the exporter a hint on how to use the magic
* `$blobfile` keyword. If the exporter detects the keyword and is provided with a name
* for the blob, it instead uses this name.
*/
#define AI_CONFIG_EXPORT_BLOB_NAME "EXPORT_BLOB_NAME"
/** /**
* @brief Specifies a gobal key factor for scale, float value * @brief Specifies a gobal key factor for scale, float value
*/ */