What it is
Assimp (Open Asset Import Library) is a portable and extensible library that imports and processes 3D models in various formats. It provides a unified API for loading meshes, materials, animations, and scene data from files like OBJ, FBX, COLLADA, and many more.
Assimp simplifies importing 3D models by converting them into a unified in-memory representation. It handles geometry, textures, materials, bones, and animations, making it easy to integrate assets into graphics pipelines or game engines.
Installation
sudo apt install assimp-devGetting started
The smallest useful thing you can do with it, and what each part means.
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <iostream>
int main() {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("model.obj",
aiProcess_Triangulate | aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cerr << "Error: " << importer.GetErrorString() << std::endl;
return -1;
}
std::cout << "Model loaded with " << scene->mNumMeshes << " meshes." << std::endl;
return 0;
}Advanced usage
Where the library earns its place over a simpler alternative.
for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[i];
std::cout << "Mesh has " << mesh->mNumVertices << " vertices." << std::endl;
}for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
aiMaterial* material = scene->mMaterials[i];
aiString name;
if (material->Get(AI_MATKEY_NAME, name) == AI_SUCCESS)
std::cout << "Material name: " << name.C_Str() << std::endl;
}if (scene->mAnimations != nullptr) {
std::cout << "Model contains " << scene->mNumAnimations << " animations." << std::endl;
}void processNode(aiNode* node, const aiScene* scene) {
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
std::cout << "Processing mesh with " << mesh->mNumVertices << " vertices" << std::endl;
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
processNode(node->mChildren[i], scene);
}
}
processNode(scene->mRootNode, scene);Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- AI_SCENE_FLAGS_INCOMPLETE
- Indicates the file was not fully imported. Check the format and ensure proper post-processing flags.
- Importer.GetErrorString() returns message
- Provides detailed error reasons like unsupported format or corrupt file. Use supported formats like OBJ, FBX, or COLLADA.
- Null scene pointer
- Occurs if import fails. Verify the file path, extension, and read permissions.
Best practices
- Always check the return value of `ReadFile` for errors before accessing the scene.
- Use post-processing flags like `aiProcess_Triangulate` and `aiProcess_GenNormals` to simplify mesh data.
- Free resources properly by relying on `Assimp::Importer` object lifetime.
- Handle missing textures and materials gracefully to avoid crashes.
- Cache model data if reusing assets to avoid repeated parsing costs.
Background
Why it exists, and what it was reacting to.
Assimp was created to address the lack of a standardized way to import 3D assets in graphics and game engines. Instead of writing custom loaders for every file format, developers can rely on Assimp’s robust and extensible API. Today, Assimp supports over 40 3D formats and is widely used in engines, visualization tools, and AR/VR applications.
