Skip to content

Commit

Permalink
Merge pull request #2066 from zenustech/userdata_to_json
Browse files Browse the repository at this point in the history
Userdata to json
  • Loading branch information
zhxx1987 authored Dec 6, 2024
2 parents 3316a44 + 019f921 commit 3e71236
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 0 deletions.
26 changes: 26 additions & 0 deletions projects/Alembic/ReadAlembic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,11 @@ struct ReadAlembic : INode {
}
set_output("namelist", namelist);
}
if (get_input2<bool>("CopyFacesetToMatid")) {
abctree->visitPrims([](auto &prim){
prim_copy_faceset_to_matid(prim.get());
});
}
set_output("abctree", std::move(abctree));
}
};
Expand All @@ -1225,6 +1230,7 @@ ZENDEFNODE(ReadAlembic, {
{"bool", "read_face_set", "1"},
{"bool", "outOfRangeAsEmpty", "0"},
{"bool", "skipInvisibleObject", "1"},
{"bool", "CopyFacesetToMatid", "1"},
{"frameid"},
},
{
Expand Down Expand Up @@ -1646,5 +1652,25 @@ ZENDEFNODE(SetABCPath, {
{"alembic"},
});

struct PrimCopyFacesetToMatid: INode {
void apply() override {
auto prim = get_input<PrimitiveObject>("prim");
prim_copy_faceset_to_matid(prim.get());

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

ZENDEFNODE(PrimCopyFacesetToMatid, {
{
"prim",
},
{
{"out"},
},
{},
{"alembic"},
});


} // namespace zeno
163 changes: 163 additions & 0 deletions projects/FBX/FBXSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "DualQuaternion.h"
#include "zeno/extra/TempNode.h"
#include "magic_enum.hpp"
#include <tinygltf/json.hpp>
using Json = nlohmann::ordered_json;

#ifdef ZENO_FBXSDK
#include <fbxsdk.h>
Expand Down Expand Up @@ -613,6 +615,163 @@ static std::shared_ptr<PrimitiveObject> 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<FbxDouble3>();
json["emissive_value"] = {value[0], value[1], value[2]};
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["emissive_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sAmbient);
if (property.IsValid()) {
FbxDouble3 value = property.Get<FbxDouble3>();
json["ambient_value"] = {value[0], value[1], value[2]};
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["ambient_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sDiffuse);
if (property.IsValid()) {
FbxDouble3 value = property.Get<FbxDouble3>();
json["diffuse_value"] = {value[0], value[1], value[2]};
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["diffuse_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sSpecular);
if (property.IsValid()) {
FbxDouble3 value = property.Get<FbxDouble3>();
json["specular_value"] = {value[0], value[1], value[2]};
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["specular_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sShininess);
if (property.IsValid()) {
double value = property.Get<double>();
json["shininess_value"] = value;
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["shininess_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sBump);
if (property.IsValid()) {
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["bump_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sNormalMap);
if (property.IsValid()) {
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["normal_map_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sTransparentColor);
if (property.IsValid()) {
FbxDouble3 value = property.Get<FbxDouble3>();
json["transparent_color_value"] = {value[0], value[1], value[2]};
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["transparent_color_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sReflection);
if (property.IsValid()) {
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["reflection_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sDisplacementColor);
if (property.IsValid()) {
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["displacement_color_tex"] = texture->GetFileName();
}
}
}
}
{
FbxProperty property = material->FindProperty(FbxSurfaceMaterial::sVectorDisplacementColor);
if (property.IsValid()) {
int textureCount = property.GetSrcObjectCount<FbxTexture>();
for (int i = 0; i < textureCount; ++i) {
FbxFileTexture* texture = FbxCast<FbxFileTexture>(property.GetSrcObject<FbxTexture>(i));
if (texture) {
json["vector_displacement_color_tex"] = texture->GetFileName();
}
}
}
}
}
mat_json[mat_name] = json;
}
ud.set2("material", mat_json.dump());
}
return prim;
}

Expand Down Expand Up @@ -839,6 +998,9 @@ struct NewFBXImportSkin : INode {
ud.set2(format("AvailableRootName_{}", i), availableRootNames[i]);
}
}
if (get_input2<bool>("CopyFacesetToMatid")) {
prim_copy_faceset_to_matid(prim.get());
}
set_output("prim", prim);
}
};
Expand All @@ -850,6 +1012,7 @@ ZENDEFNODE(NewFBXImportSkin, {
{"bool", "ConvertUnits", "0"},
{"string", "vectors", "nrm,"},
{"bool", "CopyVectorsFromLoopsToVert", "1"},
{"bool", "CopyFacesetToMatid", "1"},
},
{
"prim",
Expand Down
1 change: 1 addition & 0 deletions zeno/include/zeno/funcs/PrimitiveUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
71 changes: 71 additions & 0 deletions zeno/src/nodes/JsonProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -132,6 +133,76 @@ ZENDEFNODE(FormJson, {
},
});

struct PrimUserDataToJson : zeno::INode {
void iobject_to_json(Json &json, std::string key, std::shared_ptr<IObject> iObject) {
if (objectIsRawLiterial<int>(iObject)) {
json[key] = objectToLiterial<int>(iObject);
}
else if (objectIsRawLiterial<vec2i>(iObject)) {
auto value = objectToLiterial<vec2i>(iObject);
json[key] = { value[0], value[1]};
}
else if (objectIsRawLiterial<vec3i>(iObject)) {
auto value = objectToLiterial<vec3i>(iObject);
json[key] = { value[0], value[1], value[2]};
}
else if (objectIsRawLiterial<vec4i>(iObject)) {
auto value = objectToLiterial<vec4i>(iObject);
json[key] = { value[0], value[1], value[2], value[3]};
}
else if (objectIsRawLiterial<float>(iObject)) {
json[key] = objectToLiterial<float>(iObject);
}
else if (objectIsRawLiterial<vec2f>(iObject)) {
auto value = objectToLiterial<vec2f>(iObject);
json[key] = { value[0], value[1]};
}
else if (objectIsRawLiterial<vec3f>(iObject)) {
auto value = objectToLiterial<vec3f>(iObject);
json[key] = { value[0], value[1], value[2]};
}
else if (objectIsRawLiterial<vec4f>(iObject)) {
auto value = objectToLiterial<vec4f>(iObject);
json[key] = { value[0], value[1], value[2], value[3]};
}
else if (objectIsRawLiterial<std::string>(iObject)) {
json[key] = objectToLiterial<std::string>(iObject);
}
}
void apply() override {
auto keys_string = get_input2<std::string>("keys");
auto output_all = get_input2<bool>("output_all");
auto _json = std::make_shared<JsonObject>();
auto iObject = get_input("iObject");
auto &ud = iObject->userData();

std::vector<std::string> keys = zeno::split_str(keys_string, {' ', '\n'});
std::set<std::string> 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<JsonObject>("json");
Expand Down
25 changes: 25 additions & 0 deletions zeno/src/nodes/neo/PrimMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>("faceset_count");
ud.set2("matNum", faceset_count);
for (auto i = 0; i < faceset_count; i++) {
auto value = ud.get2<std::string>(format("faceset_{}", i));
ud.set2(format("Material_{}", i), value);
}

if (prim->tris.attr_is<int>("faceset")) {
prim->tris.attr_visit<AttrAcceptAll>("faceset", [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->tris.template add_attr<T>("matid");
std::copy(attarr.begin(), attarr.end(), targetAttr.begin());
});
}
if (prim->polys.attr_is<int>("faceset")) {
prim->polys.attr_visit<AttrAcceptAll>("faceset", [&] (auto const &attarr) {
using T = std::decay_t<decltype(attarr[0])>;
auto &targetAttr = prim->polys.template add_attr<T>("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<int>("faceset_count",0);
for (auto j = 0; j < faceset_count; j++) {
Expand Down

0 comments on commit 3e71236

Please sign in to comment.