3

When I import meshes, I get the material, but can't access the file name of the texture. The .mtl file explicitly shows the filename for the texture. In the code, it shows a texture count of 1 but the filename field shows an empty string and fullPath outputs "*0". In the mTexture it does show the texture file extension ".png" but not the filename of the texture itself. Thanks for any help.

    if (scene->HasMaterials())
    {
        for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
        {
            aiMaterial* material = scene->mMaterials[i];
            aiString name;
            material->Get(AI_MATKEY_NAME, name);
            aiReturn texFound = scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, i, &name);

            if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
            {
                aiString path;
                if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
                {
                    std::string fullPath = path.data;


                }
            }
        }
    }
Jonathan
  • 91
  • 1
  • 8

2 Answers2

3

If anyone is curious to how I solved my problem, it is shown below. In the .mtl file I had to add map_Kd diffusefile.png to add a diffuse file so then assimp could pick up the texture file.

        if (scene->HasMaterials())//True when number of materials is greater than 0
        {
            for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
            {
                aiMaterial* material = scene->mMaterials[m];//Get the current material
                aiString materialName;//The name of the material found in mesh file
                aiReturn ret;//Code which says whether loading something has been successful of not

                ret = material->Get(AI_MATKEY_NAME, materialName);//Get the material name (pass by reference)
                if (ret != AI_SUCCESS) materialName = "";//Failed to find material name so makes var empty

                //Diffuse maps
                int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);//Amount of diffuse textures
                aiString textureName;//Filename of the texture using the aiString assimp structure

                if (numTextures > 0)
                {
                    //Get the file name of the texture by passing the variable by reference again
                    //Second param is 0, which is the first diffuse texture
                    //There can be more diffuse textures but for now we are only interested in the first one
                    ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);

                    std::string textureType = "diff_";
                    std::string textureFileName = textureType + textureName.data;//The actual name of the texture file
                }
            }
Jonathan
  • 91
  • 1
  • 8
0

This shall work normally. Could you please provide the obj-model with its corresponding material file and generate a new issue-report here: https://github.com/assimp/assimp/issues

Then we can try to investigate what is going wrong in your example.

KimKulling
  • 2,654
  • 1
  • 15
  • 26
  • Thanks for replying. I'm pretty new to Assimp and I made a few mistakes in the code above. Like the line that says material->Get(...) returns an aiReturn but I've written it as if it was a void method. The variable name get's overwritten as well. I thought I could explicitly get the textures. – Jonathan Dec 30 '19 at 14:26