Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import stl #2026

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/FBX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_include_directories(zeno PRIVATE ../../ui/include)
set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE)
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE)
set(ASSIMP_BUILD_FBX_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
set(ASSIMP_BUILD_STL_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
set(ASSIMP_BUILD_FBX_EXPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
add_subdirectory(assimp)
target_link_libraries(zeno PRIVATE assimp)
Expand Down
60 changes: 60 additions & 0 deletions projects/FBX/ReadFBX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,4 +1340,64 @@ ZENDEFNODE(ReadLightFromFile,
"FBX",
}
});

struct ReadSTL : zeno::INode {
virtual void apply() override {
auto filepath = get_input2<std::string>("path");

Assimp::Importer importer;

// Import the STL file with triangulation and joining identical vertices
const aiScene* scene = importer.ReadFile(filepath, 0);

// Check if the import was successful
if (!scene) {
std::cerr << "Failed to load model: " << importer.GetErrorString() << std::endl;
}

auto prim = std::make_shared<zeno::PrimitiveObject>();

// Iterate through all meshes in the scene
for (unsigned int meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) {
aiMesh* mesh = scene->mMeshes[meshIndex];
prim->verts.resize(mesh->mNumVertices);
std::cout << "Mesh " << meshIndex << ": " << mesh->mNumVertices << " vertices, " << mesh->mNumFaces << " faces." << std::endl;

// Iterate through all vertices in the mesh
for (unsigned int vertexIndex = 0; vertexIndex < mesh->mNumVertices; ++vertexIndex) {
aiVector3D position = mesh->mVertices[vertexIndex];
prim->verts[vertexIndex] = {position.x, position.y, position.z};
}

prim->loops.reserve(mesh->mNumFaces * 3);
prim->polys.resize(mesh->mNumFaces);

int counter = 0;

// Iterate through all faces in the mesh
for (unsigned int faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) {
aiFace face = mesh->mFaces[faceIndex];

for (unsigned int i = 0; i < face.mNumIndices; ++i) {

prim->loops.push_back(face.mIndices[i]);
}

prim->polys[faceIndex] = {counter, int(face.mNumIndices)};
counter += face.mNumIndices;
}
}
set_output("prim", prim);
}
};
ZENDEFNODE(ReadSTL, {
{
{"readpath", "path"},
},
{
{"prim"},
},
{},
{"BaseIO"},
});
}
Loading