Skip to content

Commit

Permalink
Add JavaScript bindings for element equivalence (#2126)
Browse files Browse the repository at this point in the history
Expose the `Element::isEquivalent()` + options structure API in Javascript.
  • Loading branch information
kwokcb authored Dec 12, 2024
1 parent ce1168a commit c46e8a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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);
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

0 comments on commit c46e8a3

Please sign in to comment.