Keep original materials names after RemoveRedundant materials process and export to collada

pull/1048/head
tomacd 2016-10-27 19:23:43 +02:00
parent 54c5acd02d
commit 8e8757f800
2 changed files with 22 additions and 5 deletions

View File

@ -636,9 +636,24 @@ void ColladaExporter::WriteMaterials()
const aiMaterial* mat = mScene->mMaterials[a];
aiString name;
if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS )
if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) {
name = "mat";
materials[a].name = std::string( "m") + to_string(a) + name.C_Str();
materials[a].name = std::string( "m") + to_string(a) + name.C_Str();
} else {
// try to use the material's name if no other material has already taken it, else append #
std::string testName = name.C_Str();
size_t materialCountWithThisName = 0;
for( size_t i = 0; i < a; i ++ ) {
if( materials[i].name == testName ) {
materialCountWithThisName ++;
}
}
if( materialCountWithThisName == 0 ) {
materials[a].name = name.C_Str();
} else {
materials[a].name = std::string(name.C_Str()) + to_string(materialCountWithThisName);
}
}
for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) {
if( !isalnum_C( *it ) ) {
*it = '_';

View File

@ -177,12 +177,14 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
continue;
}
// generate new names for all modified materials
// generate new names for modified materials that had no names
const unsigned int idx = aiMappingTable[p];
if (ppcMaterials[idx]) {
aiString sz;
sz.length = ::ai_snprintf(sz.data,MAXLEN,"JoinedMaterial_#%u",p);
((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
if( ppcMaterials[idx]->Get(AI_MATKEY_NAME, sz) != AI_SUCCESS ) {
sz.length = ::ai_snprintf(sz.data,MAXLEN,"JoinedMaterial_#%u",p);
((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
}
} else {
ppcMaterials[idx] = pScene->mMaterials[p];
}