Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhang95 committed Aug 22, 2024
1 parent 2365334 commit 40daa9c
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions projects/FBX/FBXSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zeno/types/ListObject.h>

#include "zeno/utils/log.h"
#include "zeno/utils/bit_operations.h"
#include <zeno/types/UserData.h>
#include "zeno/types/PrimitiveObject.h"
#include "zeno/utils/scope_exit.h"
Expand Down Expand Up @@ -596,6 +597,85 @@ std::shared_ptr<PrimitiveObject> GetMesh(FbxNode* pNode) {
return prim;
}

std::shared_ptr<PrimitiveObject> GetSkeleton(FbxNode* pNode) {
FbxMesh* pMesh = pNode->GetMesh();
if (!pMesh) return nullptr;
std::vector<std::string> bone_names;
std::vector<vec3f> poss;
std::vector<vec3f> transform_r0;
std::vector<vec3f> transform_r1;
std::vector<vec3f> transform_r2;
std::map<std::string, std::string> parent_mapping;
if (pMesh->GetDeformerCount(FbxDeformer::eSkin)) {
FbxSkin* pSkin = (FbxSkin*)pMesh->GetDeformer(0, FbxDeformer::eSkin);
// Iterate over each cluster (bone)
for (int j = 0; j < pSkin->GetClusterCount(); ++j) {
FbxCluster* pCluster = pSkin->GetCluster(j);

FbxNode* pBoneNode = pCluster->GetLink();
if (!pBoneNode) continue;
FbxAMatrix transformLinkMatrix;
pCluster->GetTransformLinkMatrix(transformLinkMatrix);

// The transformation of the mesh at binding time
FbxAMatrix transformMatrix;
pCluster->GetTransformMatrix(transformMatrix);

// Inverse bind matrix.
FbxAMatrix bindMatrix_ = transformMatrix.Inverse() * transformLinkMatrix;
auto bindMatrix = bit_cast<FbxMatrix>(bindMatrix_);
auto t = bindMatrix.GetRow(3);
poss.emplace_back(t[0], t[1], t[2]);

auto r0 = bindMatrix.GetRow(0);
auto r1 = bindMatrix.GetRow(1);
auto r2 = bindMatrix.GetRow(2);
transform_r0.emplace_back(r0[0], r0[1], r0[2]);
transform_r1.emplace_back(r1[0], r1[1], r1[2]);
transform_r2.emplace_back(r2[0], r2[1], r2[2]);
std::string boneName = pBoneNode->GetName();
bone_names.emplace_back(boneName);
auto pParentNode = pBoneNode->GetParent();
if (pParentNode) {
std::string parentName = pParentNode->GetName();
parent_mapping[boneName] = parentName;
}
}
}
std::string nodeName = pNode->GetName();
auto prim = std::make_shared<PrimitiveObject>();
prim->userData().set2("RootName", nodeName);
prim->verts.resize(bone_names.size());
prim->verts.values = poss;
prim->verts.add_attr<vec3f>("transform_r0") = transform_r0;
prim->verts.add_attr<vec3f>("transform_r1") = transform_r1;
prim->verts.add_attr<vec3f>("transform_r2") = transform_r2;
std::vector<int> bone_connects;
for (auto bone_name: bone_names) {
if (parent_mapping.count(bone_name)) {
auto parent_name = parent_mapping[bone_name];
if (std::count(bone_names.begin(), bone_names.end(), parent_name)) {
auto self_index = std::find(bone_names.begin(), bone_names.end(), bone_name) - bone_names.begin();
auto parent_index = std::find(bone_names.begin(), bone_names.end(), parent_name) - bone_names.begin();
bone_connects.push_back(parent_index);
bone_connects.push_back(self_index);
}
}
}
prim->loops.values = bone_connects;
prim->polys.resize(bone_connects.size() / 2);
for (auto j = 0; j < bone_connects.size() / 2; j++) {
prim->polys[j] = {j * 2, 2};
}
auto &boneNames = prim->verts.add_attr<int>("boneName");
std::iota(boneNames.begin(), boneNames.end(), 0);
prim->userData().set2("boneName_count", int(bone_names.size()));
for (auto i = 0; i < bone_names.size(); i++) {
prim->userData().set2(zeno::format("boneName_{}", i), bone_names[i]);
}
return prim;
}

void TraverseNodesToGetNames(FbxNode* pNode, std::vector<std::string> &names) {
if (!pNode) return;

Expand Down Expand Up @@ -629,6 +709,25 @@ void TraverseNodesToGetPrim(FbxNode* pNode, std::string target_name, std::shared
TraverseNodesToGetPrim(pNode->GetChild(i), target_name, prim);
}
}
void TraverseNodesToGetSkeleton(FbxNode* pNode, std::string target_name, std::shared_ptr<PrimitiveObject> &prim) {
if (!pNode) return;

FbxMesh* mesh = pNode->GetMesh();
if (mesh) {
auto name = pNode->GetName();
if (target_name == name) {
auto sub_prim = GetSkeleton(pNode);
if (sub_prim) {
prim = sub_prim;
}
return;
}
}

for (int i = 0; i < pNode->GetChildCount(); i++) {
TraverseNodesToGetSkeleton(pNode->GetChild(i), target_name, prim);
}
}
void TraverseNodesToGetPrims(FbxNode* pNode, std::vector<std::shared_ptr<PrimitiveObject>> &prims) {
if (!pNode) return;

Expand Down Expand Up @@ -885,6 +984,39 @@ ZENDEFNODE(NewFBXImportSkeleton, {
{"primitive"},
});

struct NewFBXImportSkeleton2 : INode {
virtual void apply() override {
auto fbx_object = get_input2<FBXObject>("fbx_object");
auto lSdkManager = fbx_object->lSdkManager;
auto lScene = fbx_object->lScene;

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

FbxNode* lRootNode = lScene->GetRootNode();
std::vector<std::string> availableRootNames;
TraverseNodesToGetNames(lRootNode, availableRootNames);
for (auto name: availableRootNames) {
zeno::log_info("fuck: {}", name);
}
TraverseNodesToGetSkeleton(lRootNode, get_input2<std::string>("name"), prim);

set_output("prim", prim);
}
};

ZENDEFNODE(NewFBXImportSkeleton2, {
{
"fbx_object",
{"bool", "ConvertUnits", "0"},
{"string", "name", "0"},
},
{
"prim",
},
{},
{"deprecated"},
});

struct NewFBXImportAnimation : INode {
virtual void apply() override {
int frameid;
Expand Down

0 comments on commit 40daa9c

Please sign in to comment.