Merge pull request #4426 from sacereda/info-material-properties

Add properties information on assimp info command line
pull/4431/head^2
Kim Kulling 2022-03-15 11:57:58 +01:00 committed by GitHub
commit 7296ffc3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 368 additions and 346 deletions

View File

@ -50,7 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <iostream> #include <iostream>
#include <string> #include <string>
const char* AICMD_MSG_INFO_HELP_E = const char *AICMD_MSG_INFO_HELP_E =
"assimp info <file> [-r] [-v]\n" "assimp info <file> [-r] [-v]\n"
"\tPrint basic structure of a 3D model\n" "\tPrint basic structure of a 3D model\n"
"\t-r,--raw: No postprocessing, do a raw import\n" "\t-r,--raw: No postprocessing, do a raw import\n"
@ -73,149 +73,144 @@ const char *TREE_STOP = TREE_STOP_UTF8;
const char *TREE_CONTINUE = TREE_CONTINUE_UTF8; const char *TREE_CONTINUE = TREE_CONTINUE_UTF8;
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int CountNodes(const aiNode* root) unsigned int CountNodes(const aiNode *root) {
{
unsigned int i = 0; unsigned int i = 0;
for (unsigned int a = 0; a < root->mNumChildren; ++a ) { for (unsigned int a = 0; a < root->mNumChildren; ++a) {
i += CountNodes(root->mChildren[a]); i += CountNodes(root->mChildren[a]);
} }
return 1+i; return 1 + i;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int GetMaxDepth(const aiNode* root) unsigned int GetMaxDepth(const aiNode *root) {
{
unsigned int cnt = 0; unsigned int cnt = 0;
for (unsigned int i = 0; i < root->mNumChildren; ++i ) { for (unsigned int i = 0; i < root->mNumChildren; ++i) {
cnt = std::max(cnt,GetMaxDepth(root->mChildren[i])); cnt = std::max(cnt, GetMaxDepth(root->mChildren[i]));
} }
return cnt+1; return cnt + 1;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int CountVertices(const aiScene* scene) unsigned int CountVertices(const aiScene *scene) {
{
unsigned int cnt = 0; unsigned int cnt = 0;
for(unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
cnt += scene->mMeshes[i]->mNumVertices; cnt += scene->mMeshes[i]->mNumVertices;
} }
return cnt; return cnt;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int CountFaces(const aiScene* scene) unsigned int CountFaces(const aiScene *scene) {
{
unsigned int cnt = 0; unsigned int cnt = 0;
for(unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
cnt += scene->mMeshes[i]->mNumFaces; cnt += scene->mMeshes[i]->mNumFaces;
} }
return cnt; return cnt;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int CountBones(const aiScene* scene) unsigned int CountBones(const aiScene *scene) {
{
unsigned int cnt = 0; unsigned int cnt = 0;
for(unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
cnt += scene->mMeshes[i]->mNumBones; cnt += scene->mMeshes[i]->mNumBones;
} }
return cnt; return cnt;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int CountAnimChannels(const aiScene* scene) unsigned int CountAnimChannels(const aiScene *scene) {
{
unsigned int cnt = 0; unsigned int cnt = 0;
for(unsigned int i = 0; i < scene->mNumAnimations; ++i) { for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
cnt += scene->mAnimations[i]->mNumChannels; cnt += scene->mAnimations[i]->mNumChannels;
} }
return cnt; return cnt;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int GetAvgFacePerMesh(const aiScene* scene) { unsigned int GetAvgFacePerMesh(const aiScene *scene) {
return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountFaces(scene)/scene->mNumMeshes) : 0; return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountFaces(scene) / scene->mNumMeshes) : 0;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
unsigned int GetAvgVertsPerMesh(const aiScene* scene) { unsigned int GetAvgVertsPerMesh(const aiScene *scene) {
return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountVertices(scene)/scene->mNumMeshes) : 0; return (scene->mNumMeshes != 0) ? static_cast<unsigned int>(CountVertices(scene) / scene->mNumMeshes) : 0;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
void FindSpecialPoints(const aiScene* scene,const aiNode* root,aiVector3D special_points[3],const aiMatrix4x4& mat=aiMatrix4x4()) void FindSpecialPoints(const aiScene *scene, const aiNode *root, aiVector3D special_points[3], const aiMatrix4x4 &mat = aiMatrix4x4()) {
{
// XXX that could be greatly simplified by using code from code/ProcessHelper.h // XXX that could be greatly simplified by using code from code/ProcessHelper.h
// XXX I just don't want to include it here. // XXX I just don't want to include it here.
const aiMatrix4x4 trafo = root->mTransformation*mat; const aiMatrix4x4 trafo = root->mTransformation * mat;
for(unsigned int i = 0; i < root->mNumMeshes; ++i) { for (unsigned int i = 0; i < root->mNumMeshes; ++i) {
const aiMesh* mesh = scene->mMeshes[root->mMeshes[i]]; const aiMesh *mesh = scene->mMeshes[root->mMeshes[i]];
for(unsigned int a = 0; a < mesh->mNumVertices; ++a) { for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
aiVector3D v = trafo*mesh->mVertices[a]; aiVector3D v = trafo * mesh->mVertices[a];
special_points[0].x = std::min(special_points[0].x,v.x); special_points[0].x = std::min(special_points[0].x, v.x);
special_points[0].y = std::min(special_points[0].y,v.y); special_points[0].y = std::min(special_points[0].y, v.y);
special_points[0].z = std::min(special_points[0].z,v.z); special_points[0].z = std::min(special_points[0].z, v.z);
special_points[1].x = std::max(special_points[1].x,v.x); special_points[1].x = std::max(special_points[1].x, v.x);
special_points[1].y = std::max(special_points[1].y,v.y); special_points[1].y = std::max(special_points[1].y, v.y);
special_points[1].z = std::max(special_points[1].z,v.z); special_points[1].z = std::max(special_points[1].z, v.z);
} }
} }
for(unsigned int i = 0; i < root->mNumChildren; ++i) { for (unsigned int i = 0; i < root->mNumChildren; ++i) {
FindSpecialPoints(scene,root->mChildren[i],special_points,trafo); FindSpecialPoints(scene, root->mChildren[i], special_points, trafo);
} }
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
void FindSpecialPoints(const aiScene* scene,aiVector3D special_points[3]) void FindSpecialPoints(const aiScene *scene, aiVector3D special_points[3]) {
{ special_points[0] = aiVector3D(1e10, 1e10, 1e10);
special_points[0] = aiVector3D(1e10,1e10,1e10); special_points[1] = aiVector3D(-1e10, -1e10, -1e10);
special_points[1] = aiVector3D(-1e10,-1e10,-1e10);
FindSpecialPoints(scene,scene->mRootNode,special_points); FindSpecialPoints(scene, scene->mRootNode, special_points);
special_points[2] = (special_points[0]+special_points[1])*(ai_real)0.5; special_points[2] = (special_points[0] + special_points[1]) * (ai_real)0.5;
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
std::string FindPTypes(const aiScene* scene) std::string FindPTypes(const aiScene *scene) {
{ bool haveit[4] = { 0 };
bool haveit[4] = {0}; for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
for(unsigned int i = 0; i < scene->mNumMeshes; ++i) {
const unsigned int pt = scene->mMeshes[i]->mPrimitiveTypes; const unsigned int pt = scene->mMeshes[i]->mPrimitiveTypes;
if (pt & aiPrimitiveType_POINT) { if (pt & aiPrimitiveType_POINT) {
haveit[0]=true; haveit[0] = true;
} }
if (pt & aiPrimitiveType_LINE) { if (pt & aiPrimitiveType_LINE) {
haveit[1]=true; haveit[1] = true;
} }
if (pt & aiPrimitiveType_TRIANGLE) { if (pt & aiPrimitiveType_TRIANGLE) {
haveit[2]=true; haveit[2] = true;
} }
if (pt & aiPrimitiveType_POLYGON) { if (pt & aiPrimitiveType_POLYGON) {
haveit[3]=true; haveit[3] = true;
} }
} }
return (haveit[0]?std::string("points"):"")+(haveit[1]?"lines":"")+ return (haveit[0] ? std::string("points") : "") + (haveit[1] ? "lines" : "") +
(haveit[2]?"triangles":"")+(haveit[3]?"n-polygons":""); (haveit[2] ? "triangles" : "") + (haveit[3] ? "n-polygons" : "");
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
// Prettily print the node graph to stdout // Prettily print the node graph to stdout
void PrintHierarchy( void PrintHierarchy(
const aiNode* node, const aiNode *node,
const std::string &indent, const std::string &indent,
bool verbose, bool verbose,
bool last = false, bool last = false,
bool first = true bool first = true) {
){
// tree visualization // tree visualization
std::string branchchar; std::string branchchar;
if (first) { branchchar = ""; } if (first) {
else if (last) { branchchar = TREE_STOP; } // "'-" branchchar = "";
else { branchchar = TREE_BRANCH; } // "|-" } else if (last) {
branchchar = TREE_STOP;
} // "'-"
else {
branchchar = TREE_BRANCH;
} // "|-"
// print the indent and the branch character and the name // print the indent and the branch character and the name
std::cout << indent << branchchar << node->mName.C_Str(); std::cout << indent << branchchar << node->mName.C_Str();
@ -224,9 +219,11 @@ void PrintHierarchy(
if (node->mNumMeshes) { if (node->mNumMeshes) {
std::cout << " (mesh "; std::cout << " (mesh ";
bool sep = false; bool sep = false;
for (size_t i=0; i < node->mNumMeshes; ++i) { for (size_t i = 0; i < node->mNumMeshes; ++i) {
unsigned int mesh_index = node->mMeshes[i]; unsigned int mesh_index = node->mMeshes[i];
if (sep) { std::cout << ", "; } if (sep) {
std::cout << ", ";
}
std::cout << mesh_index; std::cout << mesh_index;
sep = true; sep = true;
} }
@ -240,10 +237,16 @@ void PrintHierarchy(
if (verbose) { if (verbose) {
// indent to use // indent to use
std::string indentadd; std::string indentadd;
if (last) { indentadd += " "; } if (last) {
else { indentadd += TREE_CONTINUE; } // "| ".. indentadd += " ";
if (node->mNumChildren == 0) { indentadd += " "; } } else {
else { indentadd += TREE_CONTINUE; } // .."| " indentadd += TREE_CONTINUE;
} // "| "..
if (node->mNumChildren == 0) {
indentadd += " ";
} else {
indentadd += TREE_CONTINUE;
} // .."| "
aiVector3D s, r, t; aiVector3D s, r, t;
node->mTransformation.Decompose(s, r, t); node->mTransformation.Decompose(s, r, t);
if (s.x != 1.0 || s.y != 1.0 || s.z != 1.0) { if (s.x != 1.0 || s.y != 1.0 || s.z != 1.0) {
@ -262,9 +265,13 @@ void PrintHierarchy(
// and recurse // and recurse
std::string nextIndent; std::string nextIndent;
if (first) { nextIndent = indent; } if (first) {
else if (last) { nextIndent = indent + " "; } nextIndent = indent;
else { nextIndent = indent + TREE_CONTINUE; } // "| " } else if (last) {
nextIndent = indent + " ";
} else {
nextIndent = indent + TREE_CONTINUE;
} // "| "
for (size_t i = 0; i < node->mNumChildren; ++i) { for (size_t i = 0; i < node->mNumChildren; ++i) {
bool lastone = (i == node->mNumChildren - 1); bool lastone = (i == node->mNumChildren - 1);
PrintHierarchy( PrintHierarchy(
@ -272,17 +279,16 @@ void PrintHierarchy(
nextIndent, nextIndent,
verbose, verbose,
lastone, lastone,
false false);
);
} }
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
// Implementation of the assimp info utility to print basic file info // Implementation of the assimp info utility to print basic file info
int Assimp_Info (const char* const* params, unsigned int num) { int Assimp_Info(const char *const *params, unsigned int num) {
// --help // --help
if (!strcmp( params[0],"-h")||!strcmp( params[0],"--help")||!strcmp( params[0],"-?") ) { if (!strcmp(params[0], "-h") || !strcmp(params[0], "--help") || !strcmp(params[0], "-?")) {
printf("%s",AICMD_MSG_INFO_HELP_E); printf("%s", AICMD_MSG_INFO_HELP_E);
return AssimpCmdError::Success; return AssimpCmdError::Success;
} }
@ -299,11 +305,11 @@ int Assimp_Info (const char* const* params, unsigned int num) {
bool raw = false; bool raw = false;
bool verbose = false; bool verbose = false;
bool silent = false; bool silent = false;
for(unsigned int i = 1; i < num; ++i) { for (unsigned int i = 1; i < num; ++i) {
if (!strcmp(params[i],"--raw")||!strcmp(params[i],"-r")) { if (!strcmp(params[i], "--raw") || !strcmp(params[i], "-r")) {
raw = true; raw = true;
} }
if (!strcmp(params[i],"--verbose")||!strcmp(params[i],"-v")) { if (!strcmp(params[i], "--verbose") || !strcmp(params[i], "-v")) {
verbose = true; verbose = true;
} }
if (!strcmp(params[i], "--silent") || !strcmp(params[i], "-s")) { if (!strcmp(params[i], "--silent") || !strcmp(params[i], "-s")) {
@ -312,7 +318,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
} }
// Verbose and silent at the same time are not allowed // Verbose and silent at the same time are not allowed
if ( verbose && silent ) { if (verbose && silent) {
printf("assimp info: Invalid arguments, verbose and silent at the same time are forbidden. "); printf("assimp info: Invalid arguments, verbose and silent at the same time are forbidden. ");
return AssimpCmdInfoError::InvalidCombinaisonOfArguments; return AssimpCmdInfoError::InvalidCombinaisonOfArguments;
} }
@ -324,12 +330,12 @@ int Assimp_Info (const char* const* params, unsigned int num) {
ProcessStandardArguments(import, params + 1, num - 1); ProcessStandardArguments(import, params + 1, num - 1);
//No custom post process flags defined, we set all the post process flags active //No custom post process flags defined, we set all the post process flags active
if(import.ppFlags == 0) if (import.ppFlags == 0)
import.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality; import.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality;
} }
// import the main model // import the main model
const aiScene* scene = ImportModel(import,in); const aiScene *scene = ImportModel(import, in);
if (!scene) { if (!scene) {
printf("assimp info: Unable to load input file %s\n", printf("assimp info: Unable to load input file %s\n",
in.c_str()); in.c_str());
@ -339,8 +345,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
aiMemoryInfo mem; aiMemoryInfo mem;
globalImporter->GetMemoryRequirements(mem); globalImporter->GetMemoryRequirements(mem);
static const char *format_string =
static const char* format_string =
"Memory consumption: %i B\n" "Memory consumption: %i B\n"
"Nodes: %i\n" "Nodes: %i\n"
"Maximum depth %i\n" "Maximum depth %i\n"
@ -364,7 +369,7 @@ int Assimp_Info (const char* const* params, unsigned int num) {
; ;
aiVector3D special_points[3]; aiVector3D special_points[3];
FindSpecialPoints(scene,special_points); FindSpecialPoints(scene, special_points);
printf(format_string, printf(format_string,
mem.total, mem.total,
CountNodes(scene->mRootNode), CountNodes(scene->mRootNode),
@ -382,14 +387,11 @@ int Assimp_Info (const char* const* params, unsigned int num) {
FindPTypes(scene).c_str(), FindPTypes(scene).c_str(),
GetAvgFacePerMesh(scene), GetAvgFacePerMesh(scene),
GetAvgVertsPerMesh(scene), GetAvgVertsPerMesh(scene),
special_points[0][0],special_points[0][1],special_points[0][2], special_points[0][0], special_points[0][1], special_points[0][2],
special_points[1][0],special_points[1][1],special_points[1][2], special_points[1][0], special_points[1][1], special_points[1][2],
special_points[2][0],special_points[2][1],special_points[2][2] special_points[2][0], special_points[2][1], special_points[2][2]);
)
;
if (silent) if (silent) {
{
printf("\n"); printf("\n");
return AssimpCmdError::Success; return AssimpCmdError::Success;
} }
@ -399,37 +401,56 @@ int Assimp_Info (const char* const* params, unsigned int num) {
printf("\nMeshes: (name) [vertices / bones / faces | primitive_types]\n"); printf("\nMeshes: (name) [vertices / bones / faces | primitive_types]\n");
} }
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) { for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
const aiMesh* mesh = scene->mMeshes[i]; const aiMesh *mesh = scene->mMeshes[i];
printf(" %d (%s)", i, mesh->mName.C_Str()); printf(" %d (%s)", i, mesh->mName.C_Str());
printf( printf(
": [%d / %d / %d |", ": [%d / %d / %d |",
mesh->mNumVertices, mesh->mNumVertices,
mesh->mNumBones, mesh->mNumBones,
mesh->mNumFaces mesh->mNumFaces);
);
const unsigned int ptypes = mesh->mPrimitiveTypes; const unsigned int ptypes = mesh->mPrimitiveTypes;
if (ptypes & aiPrimitiveType_POINT) { printf(" point"); } if (ptypes & aiPrimitiveType_POINT) {
if (ptypes & aiPrimitiveType_LINE) { printf(" line"); } printf(" point");
if (ptypes & aiPrimitiveType_TRIANGLE) { printf(" triangle"); } }
if (ptypes & aiPrimitiveType_POLYGON) { printf(" polygon"); } if (ptypes & aiPrimitiveType_LINE) {
printf(" line");
}
if (ptypes & aiPrimitiveType_TRIANGLE) {
printf(" triangle");
}
if (ptypes & aiPrimitiveType_POLYGON) {
printf(" polygon");
}
printf("]\n"); printf("]\n");
} }
// materials // materials
unsigned int total=0; if (scene->mNumMaterials)
for(unsigned int i = 0;i < scene->mNumMaterials; ++i) { printf("\nNamed Materials:");
aiString name; for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
if (AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i],AI_MATKEY_NAME,&name)) { const aiMaterial *mat = scene->mMaterials[i];
printf("%s\n \'%s\'",(total++?"":"\nNamed Materials:" ),name.data); aiString name = mat->GetName();
printf("\n \'%s\'", name.data);
if (mat->mNumProperties)
printf(" (prop) [index / bytes | texture semantic]");
for (unsigned p = 0; p < mat->mNumProperties; p++) {
const aiMaterialProperty *prop = mat->mProperties[p];
const aiTextureType textype = static_cast<aiTextureType>(prop->mSemantic);
printf("\n %d (%s): [%d / %d | %s]",
p,
prop->mKey.data,
prop->mIndex,
prop->mDataLength,
TextureTypeToString(textype));
} }
} }
if(total) { if (scene->mNumMaterials) {
printf("\n"); printf("\n");
} }
// textures // textures
total=0; unsigned int total = 0;
for(unsigned int i = 0;i < scene->mNumMaterials; ++i) { for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
aiString name; aiString name;
static const aiTextureType types[] = { static const aiTextureType types[] = {
aiTextureType_NONE, aiTextureType_NONE,
@ -452,31 +473,32 @@ int Assimp_Info (const char* const* params, unsigned int num) {
aiTextureType_AMBIENT_OCCLUSION, aiTextureType_AMBIENT_OCCLUSION,
aiTextureType_UNKNOWN aiTextureType_UNKNOWN
}; };
for(unsigned int type = 0; type < sizeof(types)/sizeof(types[0]); ++type) { for (unsigned int type = 0; type < sizeof(types) / sizeof(types[0]); ++type) {
for(unsigned int idx = 0;AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i], for (unsigned int idx = 0; AI_SUCCESS == aiGetMaterialString(scene->mMaterials[i],
AI_MATKEY_TEXTURE(types[type],idx),&name); ++idx) { AI_MATKEY_TEXTURE(types[type], idx), &name);
printf("%s\n \'%s\'",(total++?"":"\nTexture Refs:" ),name.data); ++idx) {
printf("%s\n \'%s\'", (total++ ? "" : "\nTexture Refs:"), name.data);
} }
} }
} }
if(total) { if (total) {
printf("\n"); printf("\n");
} }
// animations // animations
total=0; total = 0;
for(unsigned int i = 0;i < scene->mNumAnimations; ++i) { for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
if (scene->mAnimations[i]->mName.length) { if (scene->mAnimations[i]->mName.length) {
printf("%s\n \'%s\'",(total++?"":"\nNamed Animations:" ),scene->mAnimations[i]->mName.data); printf("%s\n \'%s\'", (total++ ? "" : "\nNamed Animations:"), scene->mAnimations[i]->mName.data);
} }
} }
if(total) { if (total) {
printf("\n"); printf("\n");
} }
// node hierarchy // node hierarchy
printf("\nNode hierarchy:\n"); printf("\nNode hierarchy:\n");
PrintHierarchy(scene->mRootNode,"",verbose); PrintHierarchy(scene->mRootNode, "", verbose);
printf("\n"); printf("\n");
return AssimpCmdError::Success; return AssimpCmdError::Success;