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 all 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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ else()
if(MATERIALX_WARNINGS_AS_ERRORS)
add_compile_options(-Werror)
endif()
if(MATERIALX_BUILD_JS)
add_compile_options(-fexceptions)
endif()
endif()

# Shared functions
Expand Down
14 changes: 4 additions & 10 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,19 +19,12 @@ 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)

# Include the source directory
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 ")
# Linker flags
set(JS_LINK_FLAGS "-I${SOURCE_FOLDER} -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 format files
string(APPEND JS_LINK_FLAGS "--post-js ${OUTPUT_FORMAT}JsXmlIo.js --post-js ${CMAKE_CURRENT_SOURCE_DIR}/initMaterialX.js ")
Expand All @@ -39,12 +33,12 @@ if (EXTERNAL_LINK_FLAGS)
string(APPEND JS_LINK_FLAGS "${EXTERNAL_LINK_FLAGS} ")
endif()

# Compiler flags
set(JS_COMPILE_FLAGS "")
if (EXTERNAL_COMPILE_FLAGS)
string(APPEND JS_COMPILE_FLAGS "${EXTERNAL_COMPILE_FLAGS} ")
endif()


if (CMAKE_BUILD_TYPE MATCHES Debug)
message("JS: Building Debug")
string(APPEND JS_LINK_FLAGS "--bind ")
Expand Down
13 changes: 6 additions & 7 deletions source/JsMaterialX/helpers.h → source/JsMaterialX/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//
// TM & (c) 2021 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//

#ifndef JSMATERIALX_HELPERS_H
#define JSMATERIALX_HELPERS_H

#include <vector>

template <class myClass>
std::vector<myClass> arrayToVec(myClass *arr, int size)
{
std::vector<myClass> dest(arr, arr + size);
return dest;
}

using stRef = const std::string&;

// Binding helpers

Expand Down
67 changes: 62 additions & 5 deletions source/JsMaterialX/JsMaterialXCore/JsDefinition.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
//
// TM & (c) 2021 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//

#include "../VectorHelper.h"
#include "../Helpers.h"

#include "../helpers.h"
#include <MaterialXCore/Definition.h>

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

namespace ems = emscripten;
namespace mx = MaterialX;

#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 +26,23 @@ 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)
.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 +55,7 @@ extern "C"
.function("getFunction", &mx::Implementation::getFunction)
.function("setNodeDef", &mx::Implementation::setNodeDef)
.function("getNodeDef", &mx::Implementation::getNodeDef)
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 +69,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 +102,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 +111,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.

39 changes: 29 additions & 10 deletions source/JsMaterialX/JsMaterialXCore/JsDocument.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include "../helpers.h"
//
// TM & (c) 2021 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//

#include "../VectorHelper.h"
#include "../Helpers.h"

#include <MaterialXCore/Document.h>

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

namespace ems = emscripten;
namespace mx = MaterialX;

using stRef = const std::string&;

extern "C"
{
EMSCRIPTEN_BINDINGS(document)
Expand All @@ -17,14 +21,15 @@ 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 set = self.getReferencedSourceUris();
return ems::val::array(set.begin(), set.end());
}))
BIND_MEMBER_FUNC("addNodeGraph", mx::Document, addNodeGraph, 0, 1, stRef)
.function("getNodeGraph", &mx::Document::getNodeGraph)
.function("getNodeGraphs", &mx::Document::getNodeGraphs)
Expand All @@ -43,6 +48,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 +68,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 +88,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 +97,17 @@ extern "C"
.function("getUnitTypeDef", &mx::Document::getUnitTypeDef)
.function("getUnitTypeDefs", &mx::Document::getUnitTypeDefs)
.function("removeUnitTypeDef", &mx::Document::removeUnitTypeDef)
.function("getVersionIntegers", &mx::Document::getVersionIntegers)
.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)
.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