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 = '_';
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];
}