Skip to content

Commit 4057ff9

Browse files
committed
Add Javascript bindings + unit test.
1 parent 8caf533 commit 4057ff9

3 files changed

Lines changed: 105 additions & 4 deletions

File tree

javascript/MaterialXTest/document.spec.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,100 @@ describe('Document', () =>
185185
nodeGraph.delete();
186186
doc.delete();
187187
});
188+
189+
// Sample node graph for defintion tests
190+
function create_sample_graph()
191+
{
192+
const doc = mx.createDocument();
193+
194+
// Create a node graph with a single image node and output.
195+
const nodeGraph = doc.addNodeGraph('NG_wrapper');
196+
expect(doc.getNodeGraphs().length).to.equal(1);
197+
const image = nodeGraph.addNode('image');
198+
const nodes = nodeGraph.getNodes();
199+
expect(nodes.length).to.equal(1);
200+
expect(nodes[0]).to.eql(image);
201+
202+
image.setInputValueString('file', 'image1.png', 'filename');
203+
const input = image.getInput('file');
204+
expect(input).to.not.be.null;
205+
206+
const output = nodeGraph.addOutput();
207+
const outputs = nodeGraph.getOutputs();
208+
expect(outputs.length).to.equal(1);
209+
expect(outputs[0]).to.eql(output);
210+
211+
// Verify the graph is valid
212+
expect(doc.validate()).to.be.true;
213+
return { doc, nodeGraph };
214+
}
215+
216+
it('Create NodeDef from NodeGraph with child implementation', () =>
217+
{
218+
const { doc, nodeGraph } = create_sample_graph();
219+
220+
// Create DefinitionOptions with addImplementationAsChild set to true
221+
const options = new mx.DefinitionOptions();
222+
options.addImplementationAsChild = true;
223+
224+
// Create NodeDef from the NodeGraph with embedded implementation
225+
const nodeDef = doc.addNodeDefFromGraph(
226+
nodeGraph,
227+
'ND_wrapper',
228+
'texture',
229+
'NG_wrapper',
230+
options
231+
);
232+
233+
expect(nodeDef).to.exist;
234+
235+
// Verify the implementation was created as a child of the nodedef
236+
const impl = nodeDef.getImplementation();
237+
expect(impl).to.exist;
238+
expect(impl.isANodeGraph())
239+
expect(impl.getName()).to.equal('NG_wrapper');
240+
241+
// Verify the implementation matches the original node graph
242+
let diff_options = new mx.ElementEquivalenceOptions();
243+
let differences = {};
244+
options.performValueComparisons = false;
245+
let result = impl.isEquivalent(nodeGraph, diff_options, differences);
246+
expect(result).to.be.true;
247+
248+
// Cleanup
249+
diff_options.delete();
250+
options.delete();
251+
doc.delete();
252+
});
253+
254+
it('Create NodeDef from NodeGraph with referencing implementation', () =>
255+
{
256+
const { doc, nodeGraph } = create_sample_graph();
257+
258+
// Create DefinitionOptions with addImplementationAsChild set to true
259+
const options = new mx.DefinitionOptions();
260+
options.addImplementationAsChild = false
261+
262+
const nodeDef2 = doc.addNodeDefFromGraph(
263+
nodeGraph,
264+
'ND_wrapper_2',
265+
'texture',
266+
'NG_wrapper_2',
267+
options
268+
);
269+
270+
expect(nodeDef2).to.exist;
271+
272+
// Verify the implementation was created as a referenced implementation
273+
const impl2 = nodeDef2.getImplementation();
274+
expect(impl2).to.exist;
275+
expect(impl2.isANodeGraph())
276+
expect(impl2.getName()).to.equal('NG_wrapper_2');
277+
const nodedef_string = impl2.getNodeDefString();
278+
expect(nodedef_string).to.equal('ND_wrapper_2');
279+
280+
// Cleanup
281+
options.delete();
282+
doc.delete();
283+
});
188284
});

source/JsMaterialX/JsMaterialXCore/JsDocument.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace mx = MaterialX;
1616
EMSCRIPTEN_BINDINGS(document)
1717
{
1818
ems::function("createDocument", &mx::createDocument);
19+
20+
ems::class_<mx::DefinitionOptions>("DefinitionOptions")
21+
.constructor<>()
22+
.property("addImplementationAsChild", &mx::DefinitionOptions::addImplementationAsChild);
23+
1924
ems::class_<mx::Document, ems::base<mx::GraphElement>>("Document")
2025
.smart_ptr_constructor("Document", &std::make_shared<mx::Document, mx::ElementPtr, const std::string &>)
2126
.smart_ptr<std::shared_ptr<const mx::Document>>("Document")
@@ -62,8 +67,8 @@ EMSCRIPTEN_BINDINGS(document)
6267
.function("getTypeDefs", &mx::Document::getTypeDefs)
6368
.function("removeTypeDef", &mx::Document::removeTypeDef)
6469
BIND_MEMBER_FUNC("addNodeDef", mx::Document, addNodeDef, 0, 3, stRef, stRef, stRef)
65-
BIND_MEMBER_FUNC("addNodeDefFromGraph", mx::Document, addNodeDefFromGraph, 4, 4, mx::NodeGraphPtr,
66-
const std::string&, const std::string&, const std::string&)
70+
BIND_MEMBER_FUNC_RAW_PTR("addNodeDefFromGraph", mx::Document, addNodeDefFromGraph, 4, 5, mx::NodeGraphPtr,
71+
const std::string&, const std::string&, const std::string&, mx::DefinitionOptions*)
6772
.function("getNodeDef", &mx::Document::getNodeDef)
6873
.function("getNodeDefs", &mx::Document::getNodeDefs)
6974
.function("removeNodeDef", &mx::Document::removeNodeDef)

source/MaterialXCore/Document.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,11 @@ class MX_CORE_API Document : public GraphElement
713713
};
714714

715715
/// @class DefinitionOptions
716-
/// Options for defining a NodeDef from an immplementation
716+
/// Options for defining a NodeDef from an implementation
717717
class MX_CORE_API DefinitionOptions
718718
{
719719
public:
720-
/// Add implementation as child of NodeDef as opposed a sibliing.
720+
/// Add implementation as child of NodeDef as opposed a sibling.
721721
bool addImplementationAsChild = false;
722722
};
723723

0 commit comments

Comments
 (0)