[SOLVED] Assimp.aiImportFileFromMemory returns null

Started by TwisterGE, January 11, 2017, 16:55:22

Previous topic - Next topic

TwisterGE

Hello,

First of all, I'm trying to load a 3D model using Assimp, but the import function always returns null.
I'm trying to load the model from memory by converting a FileInputStream into a ByteBuffer (using commons-io).

AIScene scene = Assimp.aiImportFileFromMemory(
	ByteBuffer.wrap(IOUtils.toByteArray(stream)),
	Assimp.aiProcess_Triangulate |
	Assimp.aiProcess_ValidateDataStructure,
	""
);


I'm assuming the code isn't right. So I want to know... what should I do?

Secondly: How should I retrieve an AIMesh? (I'm assuming I have to iterate through all the meshes in the AIScene, but everything is "PointerBuffer" ... I'm confused!)
for (int i = 0; i < scene.mNumMeshes(); i++) {
    AIMesh mesh = new AIMesh(scene.mMeshes().getByteBuffer(i, 1024));
}

Is that the correct way of doing it? (I saw a thread here where they did that... I don't understand)

Thanks in advance!

TwisterGE

I solved my own problems....

Lessons learned:
1st:
Never use Java heap ByteBuffers. Use MemoryUtil to create a NativeOrder one.

2nd:
Always. Flip. the Buffer...

This is the working code:
byte[] _data = IOUtils.toByteArray(stream);
ByteBuffer data = MemoryUtil.memCalloc(_data.length);
data.put(_data);
data.flip();
 
AIScene scene = Assimp.aiImportFileFromMemory(
        data,
        Assimp.aiProcess_Triangulate |
        Assimp.aiProcess_ValidateDataStructure,
        ""
);
MemoryUtil.memFree(data);

Oops ;D

spasi

For your second question, the correct code is this:

PointerBuffer meshes = scene.mMeshes(); // the buffer will automatically have a capacity equal to scene.mNumMeshes()
for ( int i = 0; i < meshes.remaining(); i++ ) {
	AIMesh mesh = AIMesh.create(meshes.get(i)); // each element in meshes is a pointer to an aiMesh struct
}