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

Full JS bindings for MaterialX Core #1203

Merged
merged 33 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
deaf7e0
Updated JsNode to JsValue
sdunkel May 12, 2021
5930f7b
Iterator protocol implemented
sdunkel May 12, 2021
cf229af
Added default args and validate bindings
sdunkel May 14, 2021
f69b231
Updated default arg binding funcs to new name
sdunkel May 14, 2021
1505644
Update JsDefinition to JsLook
muenstc May 14, 2021
fbe1884
Default Args for JsDefinition to JsLook
muenstc May 14, 2021
308e34a
chore: Style improvements
kohakukun May 11, 2021
bab0455
Consolidate test with python main.py
kohakukun May 17, 2021
aeea4b5
Fix tests
muenstc May 17, 2021
f1dd085
Add JsMaterial bindings
muenstc May 17, 2021
15a98ca
Add some of the missing bindings + fixes
muenstc May 17, 2021
4dda06e
Add more missing bindings, adapt getVersionIntegers bindings, remove …
muenstc May 17, 2021
994e0ad
addressed pr comments
sdunkel May 18, 2021
77217c7
addressed more review comments
sdunkel May 18, 2021
3fa6eb6
temporary workaround for issues with wrapping code
sdunkel May 18, 2021
c628f38
add bindings for JsElement: equal, notEqual, isA, asA
muenstc May 18, 2021
e7635cd
Automatic conversion of array <-> vector
frericp May 18, 2021
678bde8
removed unnecessary binding
sdunkel May 18, 2021
552c840
Fix vector <-> array conversion for vectors of smart pointers
frericp May 20, 2021
ccdcf3f
Remove JS wrapper code
frericp May 21, 2021
e8bd224
addressed pr comments
sdunkel May 21, 2021
cdf839c
Update array <-> vector test
frericp May 21, 2021
611300a
moved stRef to helper.h
sdunkel May 21, 2021
8d2dee8
added bind.h include again
sdunkel May 21, 2021
cfecad4
Fix getVersionIntegers
muenstc May 21, 2021
01682d1
Fix C++ internal exception handling.
frericp May 21, 2021
9aa97f5
Removed second validate overload
sdunkel May 25, 2021
9c70c32
Removed validate bindings from children
sdunkel May 25, 2021
9d8a770
Remove arrayToVec helper
frericp May 26, 2021
e90e180
Add inheritanceIterator test
kohakukun May 26, 2021
07f2643
addressed review comments
sdunkel May 26, 2021
a1b378a
add while loop test
kohakukun May 26, 2021
8a89e1b
added missing copyright headers
sdunkel May 26, 2021
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
5 changes: 1 addition & 4 deletions source/JsMaterialX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(OUTPUT_LIBS ${PROJECT_BINARY_DIR}/source/)
set(SOURCE_FOLDER ${CMAKE_SOURCE_DIR}/source)

set(CORE_DEPS ${OUTPUT_CORE}JsTypes.cpp
${OUTPUT_CORE}JsMaterial.cpp
${OUTPUT_CORE}JsUnit.cpp
${OUTPUT_CORE}JsExceptions.cpp
${OUTPUT_CORE}JsUtil.cpp
Expand All @@ -18,7 +19,6 @@ set(CORE_DEPS ${OUTPUT_CORE}JsTypes.cpp
${OUTPUT_CORE}JsGeom.cpp
${OUTPUT_CORE}JsVariant.cpp
${OUTPUT_CORE}JsValue.cpp
${OUTPUT_CORE}JsRegisteredVectors.cpp
${OUTPUT_CORE}JsTraversal.cpp)

set(FORMAT_DEPS ${OUTPUT_FORMAT}JsXmlIo.cpp)
Expand All @@ -29,9 +29,6 @@ set(JS_LINK_FLAGS "-I${SOURCE_FOLDER} ")
# Add all of the required flags for emcc
string(APPEND JS_LINK_FLAGS "-s WASM=1 -s DISABLE_EXCEPTION_CATCHING=0 -s MODULARIZE=1 -s EXPORT_ES6=1 -s USE_ES6_IMPORT_META=0 -s MEMORY_GROWTH_LINEAR_STEP=32MB -s ALLOW_MEMORY_GROWTH=1 ")

# Add the post javascript core files
string(APPEND JS_LINK_FLAGS "--post-js ${OUTPUT_CORE}JsTypes.js --post-js ${OUTPUT_CORE}JsUnit.js --post-js ${OUTPUT_CORE}JsGeom.js --post-js ${OUTPUT_CORE}JsDefinition.js --post-js ${OUTPUT_CORE}JsDocument.js --post-js ${OUTPUT_CORE}JsElement.js --post-js ${OUTPUT_CORE}JsInterface.js --post-js ${OUTPUT_CORE}JsLook.js --post-js ${OUTPUT_CORE}JsNode.js --post-js ${OUTPUT_CORE}JsProperty.js --post-js ${OUTPUT_CORE}JsTraversal.js --post-js ${OUTPUT_CORE}JsUtil.js --post-js ${OUTPUT_CORE}JsValue.js --post-js ${OUTPUT_CORE}JsVariant.js ")

# Add the post javascript format files
string(APPEND JS_LINK_FLAGS "--post-js ${OUTPUT_FORMAT}JsXmlIo.js --post-js ${CMAKE_CURRENT_SOURCE_DIR}/initMaterialX.js ")

Expand Down
66 changes: 61 additions & 5 deletions source/JsMaterialX/JsMaterialXCore/JsDefinition.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@

#include "../vectorHelper.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: To prep for merging we should stick with an CamelCase.h naming convention for file names for both of these.

#include "../helpers.h"

#include <MaterialXCore/Definition.h>

#include <emscripten.h>
#include <emscripten/bind.h>

namespace ems = emscripten;
namespace mx = MaterialX;

using stRef = const std::string&;

#define BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(NAME, T) \
BIND_MEMBER_FUNC("setValue" #NAME, mx::AttributeDef, setValue<T>, 1, 2, const T&, stRef)

extern "C"
{
EMSCRIPTEN_BINDINGS(definition)
Expand All @@ -18,19 +23,24 @@ extern "C"
.function("setNodeString", &mx::NodeDef::setNodeString)
.function("hasNodeString", &mx::NodeDef::hasNodeString)
.function("getNodeString", &mx::NodeDef::getNodeString)
.function("getType", &mx::NodeDef::getType)
.function("setNodeGroup", &mx::NodeDef::setNodeGroup)
muenstc marked this conversation as resolved.
Show resolved Hide resolved
.function("hasNodeGroup", &mx::NodeDef::hasNodeGroup)
.function("getNodeGroup", &mx::NodeDef::getNodeGroup)
.function("getImplementation", &mx::NodeDef::getImplementation)
BIND_MEMBER_FUNC("getImplementation", mx::NodeDef, getImplementation, 0, 1, stRef)
BIND_VALIDATE(mx::NodeDef)
.function("isVersionCompatible", &mx::NodeDef::isVersionCompatible)
BIND_MEMBER_FUNC("getDeclaration", mx::NodeDef, getDeclaration, 0, 1, stRef)
.class_property("CATEGORY", &mx::NodeDef::CATEGORY)
.class_property("NODE_ATTRIBUTE", &mx::NodeDef::NODE_ATTRIBUTE)
.class_property("NODE_GROUP_ATTRIBUTE", &mx::NodeDef::NODE_GROUP_ATTRIBUTE)
.class_property("TEXTURE_NODE_GROUP", &mx::NodeDef::TEXTURE_NODE_GROUP)
.class_property("PROCEDURAL_NODE_GROUP", &mx::NodeDef::PROCEDURAL_NODE_GROUP)
.class_property("GEOMETRIC_NODE_GROUP", &mx::NodeDef::GEOMETRIC_NODE_GROUP)
.class_property("ADJUSTMENT_NODE_GROUP", &mx::NodeDef::ADJUSTMENT_NODE_GROUP)
.class_property("CONDITIONAL_NODE_GROUP", &mx::NodeDef::CONDITIONAL_NODE_GROUP)
.class_property("ORGANIZATION_NODE_GROUP", &mx::NodeDef::ORGANIZATION_NODE_GROUP);
.class_property("ORGANIZATION_NODE_GROUP", &mx::NodeDef::ORGANIZATION_NODE_GROUP)
.class_property("TRANSLATION_NODE_GROUP", &mx::NodeDef::TRANSLATION_NODE_GROUP);

ems::class_<mx::Implementation, ems::base<mx::InterfaceElement>>("Implementation")
.smart_ptr_constructor("Implementation", &std::make_shared<mx::Implementation, mx::ElementPtr, const std::string &>)
Expand All @@ -43,6 +53,8 @@ extern "C"
.function("getFunction", &mx::Implementation::getFunction)
.function("setNodeDef", &mx::Implementation::setNodeDef)
.function("getNodeDef", &mx::Implementation::getNodeDef)
BIND_VALIDATE(mx::Implementation)
BIND_MEMBER_FUNC("getDeclaration", mx::Implementation, getDeclaration, 0, 1, stRef)
.class_property("CATEGORY", &mx::Implementation::CATEGORY)
.class_property("FILE_ATTRIBUTE", &mx::Implementation::FILE_ATTRIBUTE)
.class_property("FUNCTION_ATTRIBUTE", &mx::Implementation::FUNCTION_ATTRIBUTE);
Expand All @@ -56,14 +68,20 @@ extern "C"
.function("setContext", &mx::TypeDef::setContext)
.function("hasContext", &mx::TypeDef::hasContext)
.function("getContext", &mx::TypeDef::getContext)
.function("addMember", &mx::TypeDef::addMember)
BIND_MEMBER_FUNC("addMember", mx::TypeDef, addMember, 0, 1, stRef)
.function("getMember", &mx::TypeDef::getMember)
.function("getMembers", &mx::TypeDef::getMembers)
.function("removeMember", &mx::TypeDef::removeMember)
.class_property("CATEGORY", &mx::TypeDef::CATEGORY)
.class_property("SEMANTIC_ATTRIBUTE", &mx::TypeDef::SEMANTIC_ATTRIBUTE)
.class_property("CONTEXT_ATTRIBUTE", &mx::TypeDef::CONTEXT_ATTRIBUTE);

ems::class_<mx::TargetDef, ems::base<mx::TypedElement>>("TargetDef")
.smart_ptr_constructor("TargetDef", &std::make_shared<mx::TargetDef, mx::ElementPtr, const std::string &>)
.smart_ptr<std::shared_ptr<const mx::TargetDef>>("TargetDef")
.function("getMatchingTargets", &mx::TargetDef::getMatchingTargets)
.class_property("CATEGORY", &mx::TargetDef::CATEGORY);

ems::class_<mx::Member, ems::base<mx::TypedElement>>("Member")
.smart_ptr_constructor("Member", &std::make_shared<mx::Member, mx::ElementPtr, const std::string &>)
.smart_ptr<std::shared_ptr<const mx::Member>>("Member")
Expand All @@ -83,6 +101,7 @@ extern "C"
.function("addUnit", &mx::UnitDef::addUnit)
.function("getUnit", &mx::UnitDef::getUnit)
.function("getUnits", &mx::UnitDef::getUnits)
.function("removeUnit", &mx::UnitDef::removeUnit)
.class_property("CATEGORY", &mx::UnitDef::CATEGORY)
.class_property("UNITTYPE_ATTRIBUTE", &mx::UnitDef::UNITTYPE_ATTRIBUTE);

Expand All @@ -91,5 +110,42 @@ extern "C"
.smart_ptr<std::shared_ptr<const mx::UnitTypeDef>>("UnitTypeDef")
.function("getUnitDefs", &mx::UnitTypeDef::getUnitDefs)
.class_property("CATEGORY", &mx::UnitTypeDef::CATEGORY);

ems::class_<mx::AttributeDef, ems::base<mx::TypedElement>>("AttributeDef")
.smart_ptr_constructor("AttributeDef", &std::make_shared<mx::AttributeDef, mx::ElementPtr, const std::string &>)
.smart_ptr<std::shared_ptr<const mx::AttributeDef>>("AttributeDef")
.function("setAttrName", &mx::AttributeDef::setAttrName)
.function("hasAttrName", &mx::AttributeDef::hasAttrName)
.function("getAttrName", &mx::AttributeDef::getAttrName)
.function("setValueString", &mx::AttributeDef::setValueString)
.function("hasValueString", &mx::AttributeDef::hasValueString)
.function("getValueString", &mx::AttributeDef::getValueString)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Integer, int)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Boolean, bool)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Float, float)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Color3, mx::Color3)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Color4, mx::Color4)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Vector2, mx::Vector2)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Vector3, mx::Vector3)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Vector4, mx::Vector4)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Matrix33, mx::Matrix33)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(Matrix44, mx::Matrix44)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(String, std::string)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(IntegerArray, mx::IntVec)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(BooleanArray, mx::BoolVec)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(FloatArray, mx::FloatVec)
BIND_ATTRIBUTE_DEF_FUNC_INSTANCE(StringArray, mx::StringVec)
.function("hasValue", &mx::AttributeDef::hasValue)
.function("getValue", &mx::AttributeDef::getValue)
.function("setElements", &mx::AttributeDef::setElements)
.function("hasElements", &mx::AttributeDef::hasElements)
.function("getElements", &mx::AttributeDef::getElements)
.function("setExportable", &mx::AttributeDef::setExportable)
.function("getExportable", &mx::AttributeDef::getExportable)
.class_property("CATEGORY", &mx::AttributeDef::CATEGORY)
.class_property("ATTRNAME_ATTRIBUTE", &mx::AttributeDef::ATTRNAME_ATTRIBUTE)
.class_property("VALUE_ATTRIBUTE", &mx::AttributeDef::VALUE_ATTRIBUTE)
.class_property("ELEMENTS_ATTRIBUTE", &mx::AttributeDef::ELEMENTS_ATTRIBUTE)
.class_property("EXPORTABLE_ATTRIBUTE", &mx::AttributeDef::EXPORTABLE_ATTRIBUTE);
}
}
27 changes: 0 additions & 27 deletions source/JsMaterialX/JsMaterialXCore/JsDefinition.js

This file was deleted.

35 changes: 28 additions & 7 deletions source/JsMaterialX/JsMaterialXCore/JsDocument.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "../vectorHelper.h"
#include "../helpers.h"

#include <MaterialXCore/Document.h>

#include <emscripten.h>
#include <emscripten/bind.h>

namespace ems = emscripten;
Expand All @@ -17,14 +18,16 @@ extern "C"
ems::class_<mx::Document, ems::base<mx::GraphElement>>("Document")
.smart_ptr_constructor("Document", &std::make_shared<mx::Document, mx::ElementPtr, const std::string &>)
.smart_ptr<std::shared_ptr<const mx::Document>>("Document")
// At the moment only the Document type is used. Once more types are added this binding needs to be adapted accordingly.
frericp marked this conversation as resolved.
Show resolved Hide resolved
.class_function("createDocument", &mx::Document::createDocument<mx::Document>)
.function("initialize", &mx::Document::initialize)
.function("copy", &mx::Document::copy)
.function("importLibrary", &mx::Document::importLibrary)
.function("getReferencedSourceUris", ems::optional_override([](mx::Document &self) {
mx::StringSet referenced = self.mx::Document::getReferencedSourceUris();
int size = referenced.size();
return arrayToVec((std::string *)&referenced, size);
}))
mx::StringSet referenced = self.mx::Document::getReferencedSourceUris();
int size = referenced.size();
return arrayToVec((std::string *)&referenced, size);
frericp marked this conversation as resolved.
Show resolved Hide resolved
}))
BIND_MEMBER_FUNC("addNodeGraph", mx::Document, addNodeGraph, 0, 1, stRef)
.function("getNodeGraph", &mx::Document::getNodeGraph)
.function("getNodeGraphs", &mx::Document::getNodeGraphs)
Expand All @@ -43,6 +46,7 @@ extern "C"
.function("getLook", &mx::Document::getLook)
.function("getLooks", &mx::Document::getLooks)
.function("removeLook", &mx::Document::removeLook)
.function("mergeLooks", &mx::Document::mergeLooks)
muenstc marked this conversation as resolved.
Show resolved Hide resolved
BIND_MEMBER_FUNC("addLookGroup", mx::Document, addLookGroup, 0, 1, stRef)
.function("getLookGroup", &mx::Document::getLookGroup)
.function("getLookGroups", &mx::Document::getLookGroups)
Expand All @@ -62,7 +66,14 @@ extern "C"
.function("getNodeDefs", &mx::Document::getNodeDefs)
.function("removeNodeDef", &mx::Document::removeNodeDef)
.function("getMatchingNodeDefs", &mx::Document::getMatchingNodeDefs)
.function("getMatchingImplementations", &mx::Document::getMatchingImplementations)
frericp marked this conversation as resolved.
Show resolved Hide resolved
BIND_MEMBER_FUNC("addAttributeDef", mx::Document, addAttributeDef, 0, 1, stRef)
.function("getAttributeDef", &mx::Document::getAttributeDef)
.function("getAttributeDefs", &mx::Document::getAttributeDefs)
.function("removeAttributeDef", &mx::Document::removeAttributeDef)
BIND_MEMBER_FUNC("addTargetDef", mx::Document, addTargetDef, 0, 1, stRef)
.function("getTargetDef", &mx::Document::getTargetDef)
.function("getTargetDefs", &mx::Document::getTargetDefs)
.function("removeTargetDef", &mx::Document::removeTargetDef)
BIND_MEMBER_FUNC("addPropertySet", mx::Document, addPropertySet, 0, 1, stRef)
.function("getPropertySet", &mx::Document::getPropertySet)
.function("getPropertySets", &mx::Document::getPropertySets)
Expand All @@ -75,6 +86,7 @@ extern "C"
.function("getImplementation", &mx::Document::getImplementation)
.function("getImplementations", &mx::Document::getImplementations)
.function("removeImplementation", &mx::Document::removeImplementation)
.function("getMatchingImplementations", &mx::Document::getMatchingImplementations)
.function("addUnitDef", &mx::Document::addUnitDef)
.function("getUnitDef", &mx::Document::getUnitDef)
.function("getUnitDefs", &mx::Document::getUnitDefs)
Expand All @@ -83,12 +95,21 @@ extern "C"
.function("getUnitTypeDef", &mx::Document::getUnitTypeDef)
.function("getUnitTypeDefs", &mx::Document::getUnitTypeDefs)
.function("removeUnitTypeDef", &mx::Document::removeUnitTypeDef)
.function("getVersionIntegers", ems::optional_override([](mx::Document &self) {
auto version = mx::getVersionIntegers();
frericp marked this conversation as resolved.
Show resolved Hide resolved
return ems::val::array((int *)&version, (int *)&version + 2);
}))
.function("upgradeVersion", &mx::Document::upgradeVersion)
.function("setColorManagementSystem", &mx::Document::setColorManagementSystem)
.function("hasColorManagementSystem", &mx::Document::hasColorManagementSystem)
.function("getColorManagementSystem", &mx::Document::getColorManagementSystem)
.function("setColorManagementConfig", &mx::Document::setColorManagementConfig)
.function("hasColorManagementConfig", &mx::Document::hasColorManagementConfig)
.function("getColorManagementConfig", &mx::Document::getColorManagementConfig);
.function("getColorManagementConfig", &mx::Document::getColorManagementConfig)
BIND_VALIDATE(mx::Document)
.function("invalidateCache", &mx::Document::invalidateCache)
.class_property("CATEGORY", &mx::Document::CATEGORY)
.class_property("CMS_ATTRIBUTE", &mx::Document::CMS_ATTRIBUTE)
.class_property("CMS_CONFIG_ATTRIBUTE", &mx::Document::CMS_CONFIG_ATTRIBUTE);
}
}
8 changes: 0 additions & 8 deletions source/JsMaterialX/JsMaterialXCore/JsDocument.js

This file was deleted.

Loading