How to import 3D model file from memory as a file stream to assimp importer?


if you have seen before, this is the how most of tutorials use the assimp lib to import a 3D model into the opengl context .

const aiScene* scene = importer.ReadFile(fileName, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices);

The ReadFile method accept a reference to the physical memory of the file .

const std::string& fileName

my question is how can i use the other method provided by assimp called :
ReadFilefrommemory .

is it possible to use this method to accept a filearray as input ?


Assimp::Importer::ReadFileFromMemory requires a pointer to the data. The data can be a char[], std::string, std::vector<uint8_t> or similar. Anything that holds raw data in contiguous memory and can provide a pointer to it (I have no idea what a “filearray” is). The pHint parameter should be the file extension; without this the library will have to deduce the format from the data, which typically requires that the format starts with some kind of signature.

thanks for your reply
by any chance is there any example that i can follow ?
any kind of code snippet

How can i define the size of the data coming to this method ?

   const aiScene* ReadFileFromMemory(
        const void* pBuffer,
        size_t pLength,
        unsigned int pFlags,
        const char* pHint = "");

as an example , what can be the correct input for pbuffer and pLength ?

it gives me the following error :

Model (my_file) failed get loaded: File is too small


Reading from memory is for when you have already loaded some file directly into memory and you want the library to parse and understand it. The “correct input” would be a pointer to where that data starts and the number of bytes in it.

you are write .

i’m trying to write a simple c++ block of code , to import the file into a buffer and then pass the buffer to this method .
but i have difficulty to understand how to manage the c++ code .
can you provide me with some example or at least where i should look for an answer ?

for example i don’t understand the meaning of this error coming from assimp loader

Model (my_file) failed get loaded: File is too small


I don’t understand it either, because you didn’t provide the code/data that generated it. Without knowing what you did, the error message is without meaning.

Try posting the code you’re using.

	std::ifstream file("Models/test.stl", std::ios::binary | std::ios::ate);

	if (!file.is_open()) 
		std::cout << "file did not get open" << std::endl;
	size_t fileSize = file.tellg();
	char* pBuffer = new char[fileSize];, fileSize);


	// After everything is done.
	delete[] pBuffer;

and then i pass pBuffer and fileSize as the first 2 arguments of the ReadFileFromMemory

I have found the answer

put this function in your project’s main file:

string readFile(const string& fileName)
     	ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);

    	ifstream::pos_type fileSize = ifs.tellg();
    	ifs.seekg(0, ios::beg);

    	vector<char> bytes(fileSize);, fileSize);

    	return string(, fileSize);

in the same file , inside the main function make an object of the readFile function and give the model file as input to it
put the data and length of the buffer in 2 variables

 std::string my_file = readFile2("models/model_file.EXTENTION");
   const char* pbuffer =;
    size_t  plenght =  my_file.length();

send these 2 variable as input into the LoadModel function as following

 void Model::LoadModel(const void* pbuffer, size_t plenght )
    	Assimp::Importer importer;
    	const aiScene* scene = importer.ReadFileFromMemory(pbuffer, plenght , aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices,"EXTENTION");

    	// use the read error mechanism 

    	if (!scene)
    		printf("Model (%s) failed get loaded: %s", "my_file" , importer.GetErrorString());

    	LoadNode(scene->mRootNode, scene);