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

Add JavaScript bindings for element equivalence #2126

Merged
25 changes: 25 additions & 0 deletions javascript/MaterialXTest/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ describe('Element', () =>
});
});
});

describe('Equivalence', () =>
{
let mx, doc, doc2

before(async () => {
mx = await Module();
doc = mx.createDocument();
doc.addNodeGraph("graph");
doc2 = mx.createDocument();
doc2.addNodeGraph("graph1");
});

it('Compare document equivalency', () =>
{
let options = new mx.ElementEquivalenceOptions();
let differences = {};
options.performValueComparisons = false;
let result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.false;
expect(differences.message).to.not.be.empty;
result = doc.isEquivalent(doc2, options, undefined);
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
expect(result).to.be.false;
});
});
24 changes: 24 additions & 0 deletions source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,35 @@ namespace mx = MaterialX;

EMSCRIPTEN_BINDINGS(element)
{
ems::class_<mx::ElementEquivalenceOptions>("ElementEquivalenceOptions")
.constructor<>()
.property("performValueComparisons", &mx::ElementEquivalenceOptions::performValueComparisons)
.property("floatFormat", &mx::ElementEquivalenceOptions::floatFormat)
.property("floatPrecision", &mx::ElementEquivalenceOptions::floatPrecision)
.function("setAttributeExclusionList", ems::optional_override([](mx::ElementEquivalenceOptions& self, const std::vector<std::string>& exclusionList)
{
self.attributeExclusionList = std::set<std::string>(exclusionList.begin(), exclusionList.end());
}));

ems::class_<mx::Element>("Element")
.smart_ptr<std::shared_ptr<mx::Element>>("Element")
.smart_ptr<std::shared_ptr<const mx::Element>>("Element") // mx::ConstElementPtr
.function("equals", ems::optional_override([](mx::Element& self, const mx::Element& rhs) { return self == rhs; }))
.function("notEquals", ems::optional_override([](mx::Element& self, const mx::Element& rhs) { return self != rhs; }))
.function("isEquivalent", ems::optional_override([](mx::Element &self, const mx::Element& rhs,
const mx::ElementEquivalenceOptions& options,
ems::val message = ems::val::undefined())
{
mx::ConstElementPtr rhsPtr = rhs.getSelf();
std::string nativeMessage;
bool handleMessage = !message.isUndefined() && message.typeOf().as<std::string>() == "object";
bool res = self.isEquivalent(rhsPtr, options, handleMessage ? &nativeMessage : nullptr);
if (!res && handleMessage)
{
message.set("message", nativeMessage);
}
return res;
}))
.function("setCategory", &mx::Element::setCategory)
.function("getCategory", &mx::Element::getCategory)
.function("setName", &mx::Element::setName)
Expand Down