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

Polys loops attr io #1599

Merged
merged 3 commits into from
Dec 10, 2023
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
91 changes: 89 additions & 2 deletions projects/Alembic/ReadAlembic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,27 @@ static void read_attributes(std::shared_ptr<PrimitiveObject> prim, ICompoundProp
attr[i] = { data[ 3 * i], data[3 * i + 1], data[3 * i + 2]};
}
}
else if (prim->polys.size() == data.size()) {
auto &attr = prim->polys.add_attr<float>(p.getName());
for (auto i = 0; i < prim->polys.size(); i++) {
attr[i] = data[i];
}
}
else if (prim->polys.size() * 3 == data.size()) {
auto &attr = prim->polys.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->verts.size(); i++) {
attr[i] = { data[ 3 * i], data[3 * i + 1], data[3 * i + 2]};
}
}
else if (prim->loops.size() == data.size()) {
auto &attr = prim->loops.add_attr<float>(p.getName());
for (auto i = 0; i < prim->loops.size(); i++) {
attr[i] = data[i];
}
}
else {
if (!read_done) {
log_error("[alembic] can not load attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
log_error("[alembic] can not load float attr {}: {}. Check if link to Points channel when exported from Houdini.", p.getName(), data.size());
}
}
}
Expand All @@ -105,9 +123,21 @@ static void read_attributes(std::shared_ptr<PrimitiveObject> prim, ICompoundProp
attr[i] = data[i];
}
}
else if (prim->loops.size() == data.size()) {
auto &attr = prim->loops.add_attr<int>(p.getName());
for (auto i = 0; i < prim->loops.size(); i++) {
attr[i] = data[i];
}
}
else if (prim->polys.size() == data.size()) {
auto &attr = prim->polys.add_attr<int>(p.getName());
for (auto i = 0; i < prim->polys.size(); i++) {
attr[i] = data[i];
}
}
else {
if (!read_done) {
log_error("[alembic] can not load attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
log_error("[alembic] can not load int attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
}
}
}
Expand All @@ -124,6 +154,25 @@ static void read_attributes(std::shared_ptr<PrimitiveObject> prim, ICompoundProp
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->loops.size() == samp.getVals()->size()) {
auto &attr = prim->loops.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->loops.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->polys.size() == samp.getVals()->size()) {
auto &attr = prim->polys.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->polys.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else {
if (!read_done) {
log_error("[alembic] can not load vec3f attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
}
}
}
else if (IN3fGeomParam::matches(p)) {
if (!read_done) {
Expand All @@ -138,6 +187,25 @@ static void read_attributes(std::shared_ptr<PrimitiveObject> prim, ICompoundProp
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->loops.size() == samp.getVals()->size()) {
auto &attr = prim->loops.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->loops.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->polys.size() == samp.getVals()->size()) {
auto &attr = prim->polys.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->polys.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else {
if (!read_done) {
log_error("[alembic] can not load N3f attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
}
}
}
else if (IC3fGeomParam::matches(p)) {
if (!read_done) {
Expand All @@ -152,6 +220,25 @@ static void read_attributes(std::shared_ptr<PrimitiveObject> prim, ICompoundProp
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->loops.size() == samp.getVals()->size()) {
auto &attr = prim->loops.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->loops.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else if (prim->polys.size() == samp.getVals()->size()) {
auto &attr = prim->polys.add_attr<zeno::vec3f>(p.getName());
for (auto i = 0; i < prim->polys.size(); i++) {
auto v = samp.getVals()->get()[i];
attr[i] = {v[0], v[1], v[2]};
}
}
else {
if (!read_done) {
log_error("[alembic] can not load C3f attr {}. Check if link to Points channel when exported from Houdini.", p.getName());
}
}
}
else {
if (!read_done) {
Expand Down
124 changes: 118 additions & 6 deletions projects/Alembic/WriteAlembic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "zeno/utils/format.h"
#include "zeno/utils/string.h"
#include "zeno/types/ListObject.h"
#include "zeno/utils/log.h"
#include <numeric>
#include <filesystem>

Expand Down Expand Up @@ -203,10 +204,10 @@ ZENDEFNODE(WriteAlembic, {
});

template<typename T1, typename T2>
void write_attrs(std::map<std::string, OFloatGeomParam> &attrs, std::string path, std::shared_ptr<PrimitiveObject> prim, T1& schema, T2& samp) {
void write_attrs(std::map<std::string, std::any> &attrs, std::string path, std::shared_ptr<PrimitiveObject> prim, T1& schema, T2& samp) {
OCompoundProperty arbAttrs = schema.getArbGeomParams();
prim->verts.foreach_attr([&](auto const &key, auto &arr) {
if (key == "v" || key == "nrm" || key == "faceset") {
if (key == "v" || key == "nrm") {
return;
}
std::string full_key = path + '/' + key;
Expand All @@ -223,16 +224,127 @@ void write_attrs(std::map<std::string, OFloatGeomParam> &attrs, std::string path
v[i * 3 + 2] = arr[i][2];
}
samp.setVals(v);
attrs[full_key].set(samp);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, float>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kVaryingScope, 1);
}
auto samp = OFloatGeomParam::Sample();
samp.setVals(arr);
attrs[full_key].set(samp);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, int>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OInt32GeomParam (arbAttrs.getPtr(), key, false, kVaryingScope, 1);
}
auto samp = OInt32GeomParam::Sample();
samp.setVals(arr);
std::any_cast<OInt32GeomParam>(attrs[full_key]).set(samp);
}
});
if (prim->loops.size() > 0) {
prim->loops.foreach_attr([&](auto const &key, auto &arr) {
std::string full_key = path + '/' + key;
using T = std::decay_t<decltype(arr[0])>;
if constexpr (std::is_same_v<T, zeno::vec3f>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kFacevaryingScope, 3);
}
auto samp = OFloatGeomParam::Sample();
std::vector<float> v(arr.size() * 3);
for (auto i = 0; i < arr.size(); i++) {
v[i * 3 + 0] = arr[i][0];
v[i * 3 + 1] = arr[i][1];
v[i * 3 + 2] = arr[i][2];
}
samp.setVals(v);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, float>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kFacevaryingScope, 1);
}
auto samp = OFloatGeomParam::Sample();
samp.setVals(arr);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, int>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OInt32GeomParam (arbAttrs.getPtr(), key, false, kFacevaryingScope, 1);
}
auto samp = OInt32GeomParam::Sample();
samp.setVals(arr);
std::any_cast<OInt32GeomParam>(attrs[full_key]).set(samp);
}
});
}
if (prim->polys.size() > 0) {
prim->polys.foreach_attr([&](auto const &key, auto &arr) {
std::string full_key = path + '/' + key;
using T = std::decay_t<decltype(arr[0])>;
if constexpr (std::is_same_v<T, zeno::vec3f>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kUniformScope, 3);
}
auto samp = OFloatGeomParam::Sample();
std::vector<float> v(arr.size() * 3);
for (auto i = 0; i < arr.size(); i++) {
v[i * 3 + 0] = arr[i][0];
v[i * 3 + 1] = arr[i][1];
v[i * 3 + 2] = arr[i][2];
}
samp.setVals(v);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, float>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kUniformScope, 1);
}
auto samp = OFloatGeomParam::Sample();
samp.setVals(arr);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, int>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OInt32GeomParam (arbAttrs.getPtr(), key, false, kUniformScope, 1);
}
auto samp = OInt32GeomParam::Sample();
samp.setVals(arr);
std::any_cast<OInt32GeomParam>(attrs[full_key]).set(samp);
}
});
}
if (prim->tris.size() > 0) {
prim->tris.foreach_attr([&](auto const &key, auto &arr) {
zeno::log_info("{} {}", key, int(arr.size()));
std::string full_key = path + '/' + key;
using T = std::decay_t<decltype(arr[0])>;
if constexpr (std::is_same_v<T, zeno::vec3f>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kUniformScope, 3);
}
auto samp = OFloatGeomParam::Sample();
std::vector<float> v(arr.size() * 3);
for (auto i = 0; i < arr.size(); i++) {
v[i * 3 + 0] = arr[i][0];
v[i * 3 + 1] = arr[i][1];
v[i * 3 + 2] = arr[i][2];
}
samp.setVals(v);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, float>) {
zeno::log_info("std::is_same_v<T, float>");
if (attrs.count(full_key) == 0) {
attrs[full_key] = OFloatGeomParam(arbAttrs.getPtr(), key, false, kUniformScope, 1);
}
auto samp = OFloatGeomParam::Sample();
samp.setVals(arr);
std::any_cast<OFloatGeomParam>(attrs[full_key]).set(samp);
} else if constexpr (std::is_same_v<T, int>) {
if (attrs.count(full_key) == 0) {
attrs[full_key] = OInt32GeomParam (arbAttrs.getPtr(), key, false, kUniformScope, 1);
}
auto samp = OInt32GeomParam::Sample();
samp.setVals(arr);
std::any_cast<OInt32GeomParam>(attrs[full_key]).set(samp);
}
});
}
}
void write_user_data(std::map<std::string, std::any> &user_attrs, std::string path, std::shared_ptr<PrimitiveObject> prim, OCompoundProperty& user) {
auto &ud = prim->userData();
Expand Down Expand Up @@ -308,7 +420,7 @@ struct WriteAlembic2 : INode {
OPolyMesh meshyObj;
OPoints pointsObj;
std::string usedPath;
std::map<std::string, OFloatGeomParam> attrs;
std::map<std::string, std::any> attrs;
std::map<std::string, std::any> user_attrs;

virtual void apply() override {
Expand Down Expand Up @@ -557,7 +669,7 @@ struct WriteAlembicPrims : INode {
std::string usedPath;
std::map<std::string, OPolyMesh> meshyObjs;
std::map<std::string, OPoints> pointsObjs;
std::map<std::string, OFloatGeomParam> attrs;
std::map<std::string, std::any> attrs;
std::map<std::string, std::any> user_attrs;

virtual void apply() override {
Expand Down
Loading