From 674cec41760b262e19cca9b8ee81677b6773c7e6 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Thu, 5 Dec 2024 14:27:26 +0800 Subject: [PATCH 1/2] PrimUserDataToJson and prim_copy_faceset_to_matid --- projects/Alembic/ReadAlembic.cpp | 26 +++++++++ projects/FBX/FBXSDK.cpp | 4 ++ zeno/include/zeno/funcs/PrimitiveUtils.h | 1 + zeno/src/nodes/JsonProcess.cpp | 71 ++++++++++++++++++++++++ zeno/src/nodes/neo/PrimMerge.cpp | 25 +++++++++ 5 files changed, 127 insertions(+) diff --git a/projects/Alembic/ReadAlembic.cpp b/projects/Alembic/ReadAlembic.cpp index 993ce171a6..fa7591dd76 100644 --- a/projects/Alembic/ReadAlembic.cpp +++ b/projects/Alembic/ReadAlembic.cpp @@ -1215,6 +1215,11 @@ struct ReadAlembic : INode { } set_output("namelist", namelist); } + if (get_input2("CopyFacesetToMatid")) { + abctree->visitPrims([](auto &prim){ + prim_copy_faceset_to_matid(prim.get()); + }); + } set_output("abctree", std::move(abctree)); } }; @@ -1225,6 +1230,7 @@ ZENDEFNODE(ReadAlembic, { {"bool", "read_face_set", "1"}, {"bool", "outOfRangeAsEmpty", "0"}, {"bool", "skipInvisibleObject", "1"}, + {"bool", "CopyFacesetToMatid", "1"}, {"frameid"}, }, { @@ -1646,5 +1652,25 @@ ZENDEFNODE(SetABCPath, { {"alembic"}, }); +struct PrimCopyFacesetToMatid: INode { + void apply() override { + auto prim = get_input("prim"); + prim_copy_faceset_to_matid(prim.get()); + + set_output("out", prim); + } +}; + +ZENDEFNODE(PrimCopyFacesetToMatid, { + { + "prim", + }, + { + {"out"}, + }, + {}, + {"alembic"}, +}); + } // namespace zeno diff --git a/projects/FBX/FBXSDK.cpp b/projects/FBX/FBXSDK.cpp index f9208dcfe2..b9146dc007 100644 --- a/projects/FBX/FBXSDK.cpp +++ b/projects/FBX/FBXSDK.cpp @@ -839,6 +839,9 @@ struct NewFBXImportSkin : INode { ud.set2(format("AvailableRootName_{}", i), availableRootNames[i]); } } + if (get_input2("CopyFacesetToMatid")) { + prim_copy_faceset_to_matid(prim.get()); + } set_output("prim", prim); } }; @@ -850,6 +853,7 @@ ZENDEFNODE(NewFBXImportSkin, { {"bool", "ConvertUnits", "0"}, {"string", "vectors", "nrm,"}, {"bool", "CopyVectorsFromLoopsToVert", "1"}, + {"bool", "CopyFacesetToMatid", "1"}, }, { "prim", diff --git a/zeno/include/zeno/funcs/PrimitiveUtils.h b/zeno/include/zeno/funcs/PrimitiveUtils.h index e87b73f28e..79ff876c9d 100644 --- a/zeno/include/zeno/funcs/PrimitiveUtils.h +++ b/zeno/include/zeno/funcs/PrimitiveUtils.h @@ -35,6 +35,7 @@ ZENO_API void primLineSort(PrimitiveObject *prim, bool reversed = false); ZENO_API void primLineDistance(PrimitiveObject *prim, std::string resAttr, int start = 0); ZENO_API void prim_set_abcpath(PrimitiveObject* prim, std::string path_name); ZENO_API void prim_set_faceset(PrimitiveObject* prim, std::string faceset_name); +ZENO_API void prim_copy_faceset_to_matid(PrimitiveObject* prim); ZENO_API void primFilterVerts(PrimitiveObject *prim, std::string tagAttr, int tagValue, bool isInversed = false, std::string revampAttrO = {}, std::string method = "verts", int* aux = nullptr, int aux_size = 0, bool use_aux = false); diff --git a/zeno/src/nodes/JsonProcess.cpp b/zeno/src/nodes/JsonProcess.cpp index ddcbd98379..be8fa253d2 100644 --- a/zeno/src/nodes/JsonProcess.cpp +++ b/zeno/src/nodes/JsonProcess.cpp @@ -4,6 +4,7 @@ #include "zeno/types/DictObject.h" #include "zeno/types/ListObject.h" +#include "zeno/types/UserData.h" #include "zeno/utils/fileio.h" #include "zeno/utils/log.h" #include "zeno/utils/string.h" @@ -132,6 +133,76 @@ ZENDEFNODE(FormJson, { }, }); +struct PrimUserDataToJson : zeno::INode { + void iobject_to_json(Json &json, std::string key, std::shared_ptr iObject) { + if (objectIsRawLiterial(iObject)) { + json[key] = objectToLiterial(iObject); + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1]}; + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1], value[2]}; + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1], value[2], value[3]}; + } + else if (objectIsRawLiterial(iObject)) { + json[key] = objectToLiterial(iObject); + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1]}; + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1], value[2]}; + } + else if (objectIsRawLiterial(iObject)) { + auto value = objectToLiterial(iObject); + json[key] = { value[0], value[1], value[2], value[3]}; + } + else if (objectIsRawLiterial(iObject)) { + json[key] = objectToLiterial(iObject); + } + } + void apply() override { + auto keys_string = get_input2("keys"); + auto output_all = get_input2("output_all"); + auto _json = std::make_shared(); + auto iObject = get_input("iObject"); + auto &ud = iObject->userData(); + + std::vector keys = zeno::split_str(keys_string, {' ', '\n'}); + std::set keys_set(keys.begin(), keys.end()); + + for (auto i = ud.begin(); i != ud.end(); i++) { + if (output_all == false && keys_set.count(i->first) == 0) { + continue; + } + iobject_to_json(_json->json, i->first, i->second); + } + + set_output2("json", _json); + } +}; +ZENDEFNODE(PrimUserDataToJson, { + { + "iObject", + {"bool", "output_all", "0"}, + {"multiline_string", "keys", "abc_path _pivot _rotate _scale _translate _transform_row0 _transform_row1 _transform_row2 _transform_row3"}, + }, + { + "json", + }, + {}, + { + "json" + }, + }); struct JsonToString : zeno::INode { virtual void apply() override { auto json = get_input("json"); diff --git a/zeno/src/nodes/neo/PrimMerge.cpp b/zeno/src/nodes/neo/PrimMerge.cpp index cfb3e1c14a..41d6b8707e 100644 --- a/zeno/src/nodes/neo/PrimMerge.cpp +++ b/zeno/src/nodes/neo/PrimMerge.cpp @@ -28,6 +28,31 @@ ZENO_API void prim_set_abcpath(PrimitiveObject* prim, std::string path_name) { } } +ZENO_API void prim_copy_faceset_to_matid(PrimitiveObject* prim) { + auto &ud = prim->userData(); + auto faceset_count = ud.get2("faceset_count"); + ud.set2("matNum", faceset_count); + for (auto i = 0; i < faceset_count; i++) { + auto value = ud.get2(format("faceset_{}", i)); + ud.set2(format("Material_{}", i), value); + } + + if (prim->tris.attr_is("faceset")) { + prim->tris.attr_visit("faceset", [&] (auto const &attarr) { + using T = std::decay_t; + auto &targetAttr = prim->tris.template add_attr("matid"); + std::copy(attarr.begin(), attarr.end(), targetAttr.begin()); + }); + } + if (prim->polys.attr_is("faceset")) { + prim->polys.attr_visit("faceset", [&] (auto const &attarr) { + using T = std::decay_t; + auto &targetAttr = prim->polys.template add_attr("matid"); + std::copy(attarr.begin(), attarr.end(), targetAttr.begin()); + }); + } +} + ZENO_API void prim_set_faceset(PrimitiveObject* prim, std::string faceset_name) { int faceset_count = prim->userData().get2("faceset_count",0); for (auto j = 0; j < faceset_count; j++) { From 019f921ee24687ae7078b9145914037fdbc8af9c Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Thu, 5 Dec 2024 21:25:26 +0800 Subject: [PATCH 2/2] material --- projects/FBX/FBXSDK.cpp | 159 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/projects/FBX/FBXSDK.cpp b/projects/FBX/FBXSDK.cpp index b9146dc007..407ed40afc 100644 --- a/projects/FBX/FBXSDK.cpp +++ b/projects/FBX/FBXSDK.cpp @@ -28,6 +28,8 @@ #include "DualQuaternion.h" #include "zeno/extra/TempNode.h" #include "magic_enum.hpp" +#include +using Json = nlohmann::ordered_json; #ifdef ZENO_FBXSDK #include @@ -613,6 +615,163 @@ static std::shared_ptr GetMesh(FbxNode* pNode) { } ud.set2("faceset_count", mat_count); prim_set_abcpath(prim.get(), format("/ABC/{}", nodeName)); + if (mat_count > 0) { + Json mat_json; + for (auto i = 0; i < mat_count; i++) { + FbxSurfaceMaterial* material = pNode->GetMaterial(i); + ud.set2(format("faceset_{}", i), material->GetName()); + Json json; + std::string mat_name = material->GetName(); + { + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sEmissive); + if (property.IsValid()) { + FbxDouble3 value = property.Get(); + json["emissive_value"] = {value[0], value[1], value[2]}; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["emissive_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sAmbient); + if (property.IsValid()) { + FbxDouble3 value = property.Get(); + json["ambient_value"] = {value[0], value[1], value[2]}; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["ambient_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sDiffuse); + if (property.IsValid()) { + FbxDouble3 value = property.Get(); + json["diffuse_value"] = {value[0], value[1], value[2]}; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["diffuse_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sSpecular); + if (property.IsValid()) { + FbxDouble3 value = property.Get(); + json["specular_value"] = {value[0], value[1], value[2]}; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["specular_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sShininess); + if (property.IsValid()) { + double value = property.Get(); + json["shininess_value"] = value; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["shininess_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sBump); + if (property.IsValid()) { + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["bump_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sNormalMap); + if (property.IsValid()) { + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["normal_map_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sTransparentColor); + if (property.IsValid()) { + FbxDouble3 value = property.Get(); + json["transparent_color_value"] = {value[0], value[1], value[2]}; + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["transparent_color_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sReflection); + if (property.IsValid()) { + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["reflection_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sDisplacementColor); + if (property.IsValid()) { + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["displacement_color_tex"] = texture->GetFileName(); + } + } + } + } + { + FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sVectorDisplacementColor); + if (property.IsValid()) { + int textureCount = property.GetSrcObjectCount(); + for (int i = 0; i < textureCount; ++i) { + FbxFileTexture* texture = FbxCast(property.GetSrcObject(i)); + if (texture) { + json["vector_displacement_color_tex"] = texture->GetFileName(); + } + } + } + } + } + mat_json[mat_name] = json; + } + ud.set2("material", mat_json.dump()); + } return prim; }