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

Improve fbx #1975

Merged
merged 4 commits into from
Aug 5, 2024
Merged
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
225 changes: 82 additions & 143 deletions projects/FBX/FBXSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,69 @@ ZENDEFNODE(FBXSDKVisibility,
});

namespace zeno {
struct FBXObject : PrimitiveObject {
FbxManager* lSdkManager = nullptr;
FbxScene* lScene = nullptr;
};

struct ReadFBXFile: INode {
std::shared_ptr<FBXObject> _inner_fbx_object;
std::string usedPath;
virtual void apply() override {
// Change the following filename to a suitable filename value.
auto lFilename = get_input2<std::string>("path");
if (lFilename == usedPath && _inner_fbx_object != nullptr) {
set_output("fbx_object", _inner_fbx_object);
return;
}

// Initialize the SDK manager. This object handles all our memory management.
FbxManager* lSdkManager = FbxManager::Create();

// Create the IO settings object.
FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);

// Create an importer using the SDK manager.
FbxImporter* lImporter = FbxImporter::Create(lSdkManager,"");

// Use the first argument as the filename for the importer.
if(!lImporter->Initialize(lFilename.c_str(), -1, lSdkManager->GetIOSettings())) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
int major, minor, revision;
lImporter->GetFileVersion(major, minor, revision);
auto fbx_object = std::make_shared<FBXObject>();
fbx_object->lSdkManager = lSdkManager;
// Create a new scene so that it can be populated by the imported file.
fbx_object->lScene = FbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(fbx_object->lScene);

// The file is imported; so get rid of the importer.
lImporter->Destroy();
fbx_object->userData().set2("version", vec3i(major, minor, revision));
usedPath = lFilename;
_inner_fbx_object = fbx_object;

set_output("fbx_object", std::move(fbx_object));
}
};

ZENDEFNODE(ReadFBXFile, {
{
{"readpath", "path"},
},
{
"fbx_object",
},
{},
{"FBX"},
});

/**
* Return a string-based representation based on the attribute type.
*/
Expand Down Expand Up @@ -546,7 +609,7 @@ void TraverseNodesToGetPrim(FbxNode* pNode, std::string target_name, std::shared

FbxMesh* mesh = pNode->GetMesh();
if (mesh) {
auto name = mesh->GetName();
auto name = pNode->GetName();
if (target_name == name) {
auto sub_prim = GetMesh(pNode);
if (sub_prim) {
Expand All @@ -565,7 +628,6 @@ void TraverseNodesToGetPrims(FbxNode* pNode, std::vector<std::shared_ptr<Primiti

FbxMesh* mesh = pNode->GetMesh();
if (mesh) {
auto name = mesh->GetName();
auto sub_prim = GetMesh(pNode);
if (sub_prim) {
prims.push_back(sub_prim);
Expand All @@ -579,36 +641,8 @@ void TraverseNodesToGetPrims(FbxNode* pNode, std::vector<std::shared_ptr<Primiti

struct NewFBXImportSkin : INode {
virtual void apply() override {
// Change the following filename to a suitable filename value.
auto lFilename = get_input2<std::string>("path");

// Initialize the SDK manager. This object handles all our memory management.
FbxManager* lSdkManager = FbxManager::Create();

// Create the IO settings object.
FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);

// Create an importer using the SDK manager.
FbxImporter* lImporter = FbxImporter::Create(lSdkManager,"");

// Use the first argument as the filename for the importer.
if(!lImporter->Initialize(lFilename.c_str(), -1, lSdkManager->GetIOSettings())) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
int major, minor, revision;
lImporter->GetFileVersion(major, minor, revision);

// Create a new scene so that it can be populated by the imported file.
FbxScene* lScene = FbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(lScene);

// The file is imported; so get rid of the importer.
lImporter->Destroy();
auto fbx_object = get_input2<FBXObject>("fbx_object");
auto lScene = fbx_object->lScene;

// Print the nodes of the scene and their attributes recursively.
// Note that we are not printing the root node because it should
Expand Down Expand Up @@ -695,21 +729,18 @@ struct NewFBXImportSkin : INode {
}
{
auto &ud = prim->userData();
ud.set2("version", vec3i(major, minor, revision));
ud.set2("AvailableRootName_count", int(availableRootNames.size()));
for (int i = 0; i < availableRootNames.size(); i++) {
ud.set2(format("AvailableRootName_{}", i), availableRootNames[i]);
}
}
set_output("prim", prim);
// Destroy the SDK manager and all the other objects it was handling.
lSdkManager->Destroy();
}
};

ZENDEFNODE(NewFBXImportSkin, {
{
{"readpath", "path"},
"fbx_object",
{"string", "rootName", ""},
{"bool", "ConvertUnits", "0"},
{"string", "vectors", "nrm,"},
Expand All @@ -724,36 +755,9 @@ ZENDEFNODE(NewFBXImportSkin, {

struct NewFBXImportSkeleton : INode {
virtual void apply() override {
// Change the following filename to a suitable filename value.
auto lFilename = get_input2<std::string>("path");

// Initialize the SDK manager. This object handles all our memory management.
FbxManager* lSdkManager = FbxManager::Create();

// Create the IO settings object.
FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);

// Create an importer using the SDK manager.
FbxImporter* lImporter = FbxImporter::Create(lSdkManager,"");

// Use the first argument as the filename for the importer.
if(!lImporter->Initialize(lFilename.c_str(), -1, lSdkManager->GetIOSettings())) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
int major, minor, revision;
lImporter->GetFileVersion(major, minor, revision);

// Create a new scene so that it can be populated by the imported file.
FbxScene* lScene = FbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(lScene);

// The file is imported; so get rid of the importer.
lImporter->Destroy();
auto fbx_object = get_input2<FBXObject>("fbx_object");
auto lSdkManager = fbx_object->lSdkManager;
auto lScene = fbx_object->lScene;

// Print the nodes of the scene and their attributes recursively.
// Note that we are not printing the root node because it should
Expand Down Expand Up @@ -859,19 +863,13 @@ struct NewFBXImportSkeleton : INode {
transform_r2[i] *= 0.01;
}
}
{
auto &ud = prim->userData();
ud.set2("version", vec3i(major, minor, revision));
}
set_output("prim", prim);
// Destroy the SDK manager and all the other objects it was handling.
lSdkManager->Destroy();
}
};

ZENDEFNODE(NewFBXImportSkeleton, {
{
{"readpath", "path"},
"fbx_object",
{"bool", "ConvertUnits", "0"},
},
{
Expand All @@ -894,45 +892,15 @@ struct NewFBXImportAnimation : INode {
FbxTime curTime; // The time for each key in the animation curve(s)
curTime.SetSecondDouble(t); // Starting time

// Change the following filename to a suitable filename value.
auto lFilename = get_input2<std::string>("path");

// Initialize the SDK manager. This object handles all our memory management.
FbxManager* lSdkManager = FbxManager::Create();

// Create the IO settings object.
FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);
// Destroy the SDK manager and all the other objects it was handling.
zeno::scope_exit sp([=]() { lSdkManager->Destroy(); });

// Create an importer using the SDK manager.
FbxImporter* lImporter = FbxImporter::Create(lSdkManager,"");

// Use the first argument as the filename for the importer.
if(!lImporter->Initialize(lFilename.c_str(), -1, lSdkManager->GetIOSettings())) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
int major, minor, revision;
lImporter->GetFileVersion(major, minor, revision);

// Create a new scene so that it can be populated by the imported file.
FbxScene* lScene = FbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(lScene);

// The file is imported; so get rid of the importer.
lImporter->Destroy();
auto fbx_object = get_input2<FBXObject>("fbx_object");
auto lSdkManager = fbx_object->lSdkManager;
auto lScene = fbx_object->lScene;

// Print the nodes of the scene and their attributes recursively.
// Note that we are not printing the root node because it should
// not contain any attributes.
auto prim = std::make_shared<PrimitiveObject>();
auto &ud = prim->userData();
ud.set2("version", vec3i(major, minor, revision));

FbxArray<FbxString*> animationStackNames;
std::vector<std::string> clip_names;
Expand Down Expand Up @@ -1059,7 +1027,7 @@ struct NewFBXImportAnimation : INode {

ZENDEFNODE(NewFBXImportAnimation, {
{
{"readpath", "path"},
"fbx_object",
{"string", "clipName", ""},
{"frameid"},
{"float", "fps", "25"},
Expand All @@ -1084,46 +1052,15 @@ struct NewFBXImportCamera : INode {
float t = float(frameid) / fps;
FbxTime curTime; // The time for each key in the animation curve(s)
curTime.SetSecondDouble(t); // Starting time

// Change the following filename to a suitable filename value.
auto lFilename = get_input2<std::string>("path");

// Initialize the SDK manager. This object handles all our memory management.
FbxManager* lSdkManager = FbxManager::Create();

// Create the IO settings object.
FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
lSdkManager->SetIOSettings(ios);
// Destroy the SDK manager and all the other objects it was handling.
zeno::scope_exit sp([=]() { lSdkManager->Destroy(); });

// Create an importer using the SDK manager.
FbxImporter* lImporter = FbxImporter::Create(lSdkManager,"");

// Use the first argument as the filename for the importer.
if(!lImporter->Initialize(lFilename.c_str(), -1, lSdkManager->GetIOSettings())) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
int major, minor, revision;
lImporter->GetFileVersion(major, minor, revision);

// Create a new scene so that it can be populated by the imported file.
FbxScene* lScene = FbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(lScene);

// The file is imported; so get rid of the importer.
lImporter->Destroy();
auto fbx_object = get_input2<FBXObject>("fbx_object");
auto lSdkManager = fbx_object->lSdkManager;
auto lScene = fbx_object->lScene;

// Print the nodes of the scene and their attributes recursively.
// Note that we are not printing the root node because it should
// not contain any attributes.
auto prim = std::make_shared<PrimitiveObject>();
auto &ud = prim->userData();
ud.set2("version", vec3i(major, minor, revision));

FbxArray<FbxString*> animationStackNames;
std::vector<std::string> clip_names;
Expand Down Expand Up @@ -1224,7 +1161,7 @@ struct NewFBXImportCamera : INode {

ZENDEFNODE(NewFBXImportCamera, {
{
{"readpath", "path"},
"fbx_object",
{"string", "clipName", ""},
{"frameid"},
{"float", "fps", "25"},
Expand Down Expand Up @@ -1343,7 +1280,7 @@ struct NewFBXBoneDeform : INode {
bw.push_back(&prim->verts.add_attr<float>(format("boneWeight_{}", i)));
}
size_t vert_count = prim->verts.size();
#pragma omp parallel for
#pragma omp parallel for
for (auto i = 0; i < vert_count; i++) {
auto opos = prim->verts[i];
vec3f pos = {};
Expand All @@ -1365,6 +1302,7 @@ struct NewFBXBoneDeform : INode {
if (vector.size()) {
if (prim->verts.attr_is<vec3f>(vector)) {
auto &nrms = prim->verts.attr<vec3f>(vector);
#pragma omp parallel for
for (auto i = 0; i < vert_count; i++) {
glm::mat4 matrix(0);
float w = 0;
Expand All @@ -1382,6 +1320,7 @@ struct NewFBXBoneDeform : INode {
}
if (prim->loops.attr_is<vec3f>(vector)) {
auto &nrms = prim->loops.attr<vec3f>(vector);
#pragma omp parallel for
for (auto i = 0; i < prim->loops.size(); i++) {
auto vi = prim->loops[i];
glm::mat4 matrix(0);
Expand Down
Loading