Copy pointer to aiScene

Started by
3 comments, last by congard 5 years ago

I do skeletal animations using Assimp. Accordingly, I always need the data loaded by it. But for some reason, this data becomes inaccessible and results in a Segmentation fault. Below is the problem code:


    static Mesh processMesh(const aiMesh *aimesh, const aiScene *scene) {
        Mesh mesh;
        mesh.ai_scene = scene;
        std::cout << mesh.ai_scene->mRootNode->mName.data; // OK

        ...
    }

But if you do it inside the Mesh itself, it will lead to an error. For example:


    void boneTransform(float TimeInSeconds) {
        glm::mat4 identity = glm::mat4();
        std::cout << ai_scene->mRootNode->mName.data; // SEG FAULT
        ...
    }

What is this problem? And how to solve it?

ai_scene is in the Mesh structure:


    private:
    const aiScene *ai_scene = nullptr;

The scene itself is created in the Model structure as follows:


    void init(const std::string &path, const GLuint params) {
        // Create an instance of the Importer class
        Assimp::Importer importer;
        const aiScene *scene = importer.ReadFile(path, params);

        // If the import failed, report it
        if (!scene) {
            std::cout << importer.GetErrorString() << "\n";
            return;
        }

        processNode(scene->mRootNode, scene);
    }

processNode code:


    void processNode(const aiNode *node, const aiScene *scene) {
        for (size_t i = 0; i < node->mNumMeshes; i++) {
            aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
            meshes.push_back(Mesh::processMesh(mesh, scene));        
        }

        for(size_t i = 0; i < node->mNumChildren; i++) {
            processNode(node->mChildren[i], scene);
        }
    }

 

Advertisement

I haven't read through the Assimp internals or documentation in a while, but from what you're describing, I'm guessing that your aiScene pointer becomes invalid when the Assimp::Importer goes out of scope. If that's the case, you'll need to store the Assimp::Importer somewhere until you're done using it.

Importer will delete the scene as soon as it gets destroyed itself. If you want to keep the scene, you can use Importer::GetOrphanedScene() method - then deleting the scene is your responsibility.

Problem has been solved by expanding the scope of Assimp::Importer importer:


// Create an instance of the Importer class
Assimp::Importer importer;
void init(const std::string &path, const GLuint params) {
    const aiScene *scene = importer.ReadFile(path, params);
    ...
}

 

14 minutes ago, joe_chip said:

Importer will delete the scene as soon as it gets destroyed itself. If you want to keep the scene, you can use Importer::GetOrphanedScene() method - then deleting the scene is your responsibility.

Thanks I'll know!

This topic is closed to new replies.

Advertisement