From 2fdad915cc325bea109500f2af0aca59d1772e68 Mon Sep 17 00:00:00 2001 From: Marco Callieri Date: Thu, 1 Feb 2018 17:45:01 +0100 Subject: [PATCH] mtllib fix some tools do not add the mtllib line in the header, and simply call the mtl file like the main object file. Added a fall-back code to cover these cases --- wrap/io_trimesh/import_obj.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index 9c45188d..9ffe8e6c 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -615,6 +615,15 @@ namespace vcg { } else if ((header.compare("usemtl")==0) && (tokens.size() > 1)) // material usage { + // emergency check. If there are no materials, the materail library failed to load or was not specified + // but there are tools that save the material library with the same name of the file, but do not add the + // "mtllib" definition in the header. So, we can try to see if this is the case + if ((materials.size() == 1)&&(materials[0].materialName == "")){ + std::string materialFileName(filename); + materialFileName.replace(materialFileName.end()-4, materialFileName.end(), ".mtl"); + LoadMaterials(materialFileName.c_str(), materials, m.textures); + } + std::string materialName; if (tokens.size() == 2) materialName = tokens[1]; //play it safe