LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2024, 01:25 PM   #1
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
Simple Assimp import and export problem


I'm just trying to understand some simple things about assimp. As a test program, I've got a simple program, below,(originally from https://stackoverflow.com/questions/...rows-exception) which imports a file then exports the data. For some reason, none of the exported files are readable by, e.g. meshlab. What I am missing?

Code:
#include <assimp/cimport.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/Exporter.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

int main(int argc, char** argv)
{
    std::string filename = "float-color.ply";

 //   auto stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
 //   aiAttachLogStream(&stream);

    Assimp::Importer Importer;
    //Importer.
    std::cout << "\tReading file using ASSIMP" << std::endl;
    const aiScene *aiscene = Importer.ReadFile(filename.c_str(), aiProcess_ValidateDataStructure | aiProcessPreset_TargetRealtime_MaxQuality);

    std::string str = Importer.GetErrorString();


    if (!aiscene) {
        printf("Error parsing '%s': '%s'\n", filename.c_str(), Importer.GetErrorString());
        return 1;
    }


    Assimp::Exporter Exporter;
    const aiExportFormatDesc* format = Exporter.GetExportFormatDescription(0);
    int lIndex = filename.find_last_of('/');
    //const string path = Filename.substr(0,lIndex+1);
    std::string path = "float-color.off";
    std::cout << "\tExport path: " << path << std::endl;
    aiReturn ret = Exporter.Export(aiscene, format->id, path);
    std::cout << Exporter.GetErrorString() << std::endl;


    return 0;
}
The test file comes from the unit tests in assimp, below:
Code:
ply
format ascii 1.0
comment VCGLIB generated
element vertex 3
property float x
property float y
property float z
property float red
property float green
property float blue
property float alpha
element face 1
property list uchar int vertex_indices
end_header
0.0 0.0 0.0 0 0 1 1
100.0 0.0 0.0 0 0 1 1
200.0 200.0 0.0 0 0 1 1
3 0 1 2
The code compiles cleanly, runs without error, generates an "off" file, but the file is unreadable by meshlab. Same with multiple other exported file formats...
 
Old 03-07-2024, 11:44 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> the file is unreadable by meshlab

Is there an error message from `meshlab`? Please quote it.
 
Old 03-08-2024, 08:25 AM   #3
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851

Original Poster
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
Thanks for responding!

Depends on the file type, "off" produces "Invalid file", "obj" says "No vertex found", "ply says "header not found".

I realized that all the output files were actually the same identical file regardless of suffix, and the output is an "xml" file.

So.. clearly there is something wrong with my Exporter.Export(aiscene, format->id, path) call, but I haven't figured out how to make it generate actual off, ply, obj, blah blah files. I changed the last few lines to
Code:
    aiReturn ret = Exporter.Export(aiscene, format->id, path, 0); 

    if (!(aiReturn_SUCCESS == 0)) {
        printf("Error exporting '%s': '%s'\n", filename.c_str(), Exporter.GetErrorString());
        return 1;
    }
but get no reported error. meshlab produces no error if I feed it the xml file with the suffix xml, but it also produces no graphics.
(Added)
I tried to change it to aiReturn ret = Exporter.Export(aiscene, "stl" , path, 0); which also produces no error and no output file at all.

Last edited by mostlyharmless; 03-08-2024 at 09:00 AM. Reason: added info
 
Old 03-08-2024, 08:31 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by mostlyharmless View Post
Code:
    const aiExportFormatDesc* format = Exporter.GetExportFormatDescription(0);
What format does 0 correspond to? Maybe it's the wrong one?
 
Old 03-08-2024, 09:09 AM   #5
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851

Original Poster
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
Hmm, good idea. I tried 1,2,3,4 and got different file types. I suppose I need to dig through assimp and see which types are which, or figure out a better way to specify file types. Just putting in the extension didn't produce any output.

I found this https://github.com/assimp/assimp/issues/2481 which lists these types
0: dae 1: x 2: stp 3: obj 4: obj 5: stl 6: stl 7: ply 8: ply 9: 3ds 10: gltf 11: glb 12: gltf2 13: assbin 14: assxml 15: x3d 16: 3mf
but it seems hit or miss. ASCII ply works, obj works, stl produces nothing..

and.. aiReturn ret = Exporter.Export(aiscene, "obj", path); actually works to make a file, just not stl or off. In addition, I tried a different .ply file and found the obj file created is only grey scale. I suspect the latter is because the ply file has color set for each vertex, and assimp is assuming some sort of texture or uv channel?

Last edited by mostlyharmless; 03-08-2024 at 10:40 AM. Reason: more experimentation
 
Old 03-13-2024, 09:11 AM   #6
mostlyharmless
Senior Member
 
Registered: Jan 2008
Distribution: Arch/Manjaro, might try Slackware again
Posts: 1,851

Original Poster
Blog Entries: 14

Rep: Reputation: 284Reputation: 284Reputation: 284
Update: solved, kind of.. It's quite flaky, and I've only tested a ply file with vertex colors and normals, but I've got dae/collada, ascii ply and stl,3ds,x,fbx and x3d successfully made. I'm posting my code here for anyone with a similar problem.
Code:
#include <assimp/cimport.h>
#include <stdio.h>
#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/Exporter.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

int main(int argc, char** argv)
{
    const char* filenameout = NULL;
    const char* filename = NULL;
    filename = argv[1];
    filenameout = argv[2];

    auto stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
    aiAttachLogStream(&stream);
    Assimp::Importer Importer;
    Assimp::Exporter Exporter;
    const aiImporterDesc *iformat = nullptr;

    // Check and validate the specified model file extension.
    // https://github.com/assimp/assimp/issues/3827  binary ply is broken, use rply instead
    // has to be on both the import and export list.
    // only obj,dae,ascii ply,ascii stl,3ds,x and fbx verified to work from ply. Colors are not always preserved
    const char* extension = strrchr(filenameout, '.');
    if (!extension) {
        std::cout <<"Please provide a file with a valid extension.\n";
        return 1;
    }
    if (AI_FALSE == aiIsExtensionSupported(extension)) {
        std::cout <<"The specified model file extension is currently unsupported in Assimp\n ";
        return 1;
    }

    std::cout << "\tReading file using ASSIMP" << std::endl;
    const aiScene *aiscene = Importer.ReadFile(filename, aiProcess_ValidateDataStructure | aiProcessPreset_TargetRealtime_MaxQuality);
    if (!aiscene) {
        printf("Error parsing '%s': '%s'\n", filename, Importer.GetErrorString());
        return 1;}

    uint countin = Importer.GetImporterCount();
;
    uint ID = Importer.GetImporterIndex(extension);  //unfortunately these are not Exporter formatIDs
    int i = 0 ;
    do {
        iformat = Importer.GetImporterInfo(i);
        std::cout << "id: "<< i << " " << iformat->mFileExtensions << "\n";
        i++;
    } while (i < countin);


    //std::cout << "ID: "<< ID << "\n";
    iformat = Importer.GetImporterInfo(ID);
    uint count = Exporter.GetExportFormatCount();
    // std::cout << "count: "<< count << "\n";
    const aiExportFormatDesc *format;
    i = 0 ;
    do {
        format = Exporter.GetExportFormatDescription(i);
        std::cout << "id: "<< i << " " << format->id << "\n";
        if (iformat->mFileExtensions == format->id){
    //        std::cout << "ID is " << i << "\n";
            Exporter.Export(aiscene, format->id , filenameout, 0);
            std::cout << "Wrote " << filenameout << "\n";
        }
        i++;
    } while (i < count);

    // special cases

    if (ID == 26) {  // dae or collada
        format = Exporter.GetExportFormatDescription(0);
        Exporter.Export(aiscene, format->id , filenameout, 0);
        std::cout << "Wrote " << filenameout << "\n";
    }

    if (ID == 45) {  //x3d
        format = Exporter.GetExportFormatDescription(16);
        Exporter.Export(aiscene, format->id , filenameout, 0);
        std::cout << "Wrote " << filenameout << "\n";
    }

    if (ID == 3) { //3ds
        format = Exporter.GetExportFormatDescription(9);
        Exporter.Export(aiscene, format->id , filenameout, 0);
        std::cout << "Wrote " << filenameout << "\n";
    }

    if (!(aiReturn_SUCCESS == 0)) {
        std::cout << "Error exporting" << filenameout << Exporter.GetErrorString() << "\n" ;
    }

    aiDetachAllLogStreams();

    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
svn assimp install help marcia Linux - Software 10 03-06-2009 12:57 PM
import and export command mrbutabara Linux - Newbie 1 02-17-2009 09:21 AM
Import (and export) Email mesages to and from Outlook / OE 1kyle SUSE / openSUSE 2 03-22-2008 03:26 PM
Accessing import and export symbols ? unclesam Linux - Newbie 2 01-11-2006 08:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration