Skip to content

Commit

Permalink
Fix issue where code segfaults when surface material is missing
Browse files Browse the repository at this point in the history
Ignores missing material and warns user of bad FBX

Only edge case is handled while previously working models should operate as before
  • Loading branch information
ProjectBarks authored and zellski committed Jul 18, 2019
1 parent 70136c6 commit 3daf2b7
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/fbx/materials/FbxMaterials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "RoughnessMetallicMaterials.hpp"
#include "TraditionalMaterials.hpp"

static int warnMtrCount = 0;

FbxMaterialsAccess::FbxMaterialsAccess(
const FbxMesh* pMesh,
const std::map<const FbxTexture*, FbxString>& textureLocations)
Expand Down Expand Up @@ -43,9 +45,16 @@ FbxMaterialsAccess::FbxMaterialsAccess(
continue;
}

FbxSurfaceMaterial* surfaceMaterial =
auto* surfaceMaterial =
mesh->GetNode()->GetSrcObject<FbxSurfaceMaterial>(materialNum);

if (!surfaceMaterial) {
if (++warnMtrCount == 1) {
fmt::printf("Warning: Reference to missing surface material.\n");
fmt::printf(" (Further warnings of this type squelched.)\n");
}
}

if (materialNum >= summaries.size()) {
summaries.resize(materialNum + 1);
}
Expand All @@ -57,7 +66,8 @@ FbxMaterialsAccess::FbxMaterialsAccess(
if (materialNum >= userProperties.size()) {
userProperties.resize(materialNum + 1);
}
if (userProperties[materialNum].empty()) {
if (surfaceMaterial && userProperties[materialNum].empty()) {

FbxProperty objectProperty = surfaceMaterial->GetFirstProperty();
while (objectProperty.IsValid()) {
if (objectProperty.GetFlag(FbxPropertyFlags::eUserDefined)) {
Expand Down Expand Up @@ -97,6 +107,9 @@ const std::vector<std::string> FbxMaterialsAccess::GetUserProperties(const int p
std::unique_ptr<FbxMaterialInfo> FbxMaterialsAccess::GetMaterialInfo(
FbxSurfaceMaterial* material,
const std::map<const FbxTexture*, FbxString>& textureLocations) {
if (!material) {
return nullptr;
}
std::unique_ptr<FbxMaterialInfo> res =
FbxStingrayPBSMaterialResolver(material, textureLocations).resolve();
if (res == nullptr) {
Expand Down

0 comments on commit 3daf2b7

Please sign in to comment.