From 35685600042a634db7d663c131bfa56f63909051 Mon Sep 17 00:00:00 2001 From: Jonathan Feldstein Date: Wed, 10 Jun 2020 10:19:38 -0400 Subject: [PATCH] Changes needed for publishing. (#902) Co-authored-by: Jonathan Feldstein --- source/MaterialXRuntime/Private/PvtApi.cpp | 15 +++++ source/MaterialXRuntime/Private/PvtApi.h | 2 + source/MaterialXRuntime/RtApi.cpp | 10 +++ source/MaterialXRuntime/RtApi.h | 6 ++ source/MaterialXRuntime/RtFileIo.cpp | 74 ++++++++++++---------- source/MaterialXRuntime/RtFileIo.h | 7 +- 6 files changed, 77 insertions(+), 37 deletions(-) diff --git a/source/MaterialXRuntime/Private/PvtApi.cpp b/source/MaterialXRuntime/Private/PvtApi.cpp index e627db04aa..7c2141c609 100644 --- a/source/MaterialXRuntime/Private/PvtApi.cpp +++ b/source/MaterialXRuntime/Private/PvtApi.cpp @@ -29,6 +29,21 @@ void PvtApi::reset() _unitDefinitions = UnitConverterRegistry::create(); } +void PvtApi::createLibrary(const RtToken& name) +{ + // If already loaded unload the old first, + // to support reloading of updated libraries. + if (getLibrary(name)) + { + unloadLibrary(name); + } + + RtStagePtr lib = RtStage::createNew(name); + _libraries[name] = lib; + + _libraryRoot->addReference(lib); +} + void PvtApi::loadLibrary(const RtToken& name) { // If already loaded unload the old first, diff --git a/source/MaterialXRuntime/Private/PvtApi.h b/source/MaterialXRuntime/Private/PvtApi.h index d674239d19..c51f65a916 100644 --- a/source/MaterialXRuntime/Private/PvtApi.h +++ b/source/MaterialXRuntime/Private/PvtApi.h @@ -167,6 +167,8 @@ class PvtApi return _implementationSearchPaths; } + void createLibrary(const RtToken& name); + void loadLibrary(const RtToken& name); void unloadLibrary(const RtToken& name); diff --git a/source/MaterialXRuntime/RtApi.cpp b/source/MaterialXRuntime/RtApi.cpp index 51f1a3f7d7..46d45dbe4d 100644 --- a/source/MaterialXRuntime/RtApi.cpp +++ b/source/MaterialXRuntime/RtApi.cpp @@ -166,6 +166,11 @@ const FileSearchPath& RtApi::getImplementationSearchPath() const return _cast(_ptr)->getImplementationSearchPath(); } +void RtApi::createLibrary(const RtToken& name) +{ + _cast(_ptr)->createLibrary(name); +} + void RtApi::loadLibrary(const RtToken& name) { _cast(_ptr)->loadLibrary(name); @@ -191,6 +196,11 @@ void RtApi::setUserDefinitionPath(const FilePath& path) return _cast(_ptr)->setUserDefinitionPath(path); } +RtStagePtr RtApi::getLibrary(const RtToken& name) +{ + return _cast(_ptr)->getLibrary(name); +} + RtStagePtr RtApi::getLibrary() { return _cast(_ptr)->getLibraryRoot(); diff --git a/source/MaterialXRuntime/RtApi.h b/source/MaterialXRuntime/RtApi.h index ab48a81cd4..ace27bfd8c 100644 --- a/source/MaterialXRuntime/RtApi.h +++ b/source/MaterialXRuntime/RtApi.h @@ -117,6 +117,9 @@ class RtApi /// Get search path for implemntations used by libraries. const FileSearchPath& getImplementationSearchPath() const; + /// Create a library. + void createLibrary(const RtToken& name); + /// Load a library. void loadLibrary(const RtToken& name); @@ -126,6 +129,9 @@ class RtApi /// Return a list of all loaded libraries. RtTokenVec getLibraryNames() const; + /// Return a particular library stage + RtStagePtr getLibrary(const RtToken& name); + /// Return the library stage containing all loaded libraries. RtStagePtr getLibrary(); diff --git a/source/MaterialXRuntime/RtFileIo.cpp b/source/MaterialXRuntime/RtFileIo.cpp index 090b4ba624..f5f0fcb913 100644 --- a/source/MaterialXRuntime/RtFileIo.cpp +++ b/source/MaterialXRuntime/RtFileIo.cpp @@ -918,43 +918,46 @@ namespace doc->removeChild(uniqueName); } - void writeNodeGraph(const PvtPrim* src, DocumentPtr dest) + void writeNodeGraph(const PvtPrim* src, DocumentPtr dest, const RtWriteOptions* writeOptions) { NodeGraphPtr destNodeGraph = dest->addNodeGraph(src->getName()); writeMetadata(src, destNodeGraph, nodegraphMetadata); RtNodeGraph nodegraph(src->hnd()); - // Write inputs/parameters. - RtObjTypePredicate inputsFilter; - for (RtAttribute attr : src->getAttributes(inputsFilter)) + if (!writeOptions || writeOptions->writeNodeGraphInputs) { - RtInput nodegraphInput = nodegraph.getInput(attr.getName()); - ValueElementPtr v = nullptr; - if (nodegraphInput.isUniform()) + // Write inputs/parameters. + RtObjTypePredicate inputsFilter; + for (RtAttribute attr : src->getAttributes(inputsFilter)) { - v = destNodeGraph->addParameter(nodegraphInput.getName(), nodegraphInput.getType()); - } - else - { - InputPtr input = destNodeGraph->addInput(nodegraphInput.getName(), nodegraphInput.getType()); - v = input->asA(); - - if (nodegraphInput.isConnected()) + RtInput nodegraphInput = nodegraph.getInput(attr.getName()); + ValueElementPtr v = nullptr; + if (nodegraphInput.isUniform()) { - // Write connections to upstream nodes. - RtOutput source = nodegraphInput.getConnection(); - RtNode sourceNode = source.getParent(); - input->setNodeName(sourceNode.getName()); - if (sourceNode.numOutputs() > 1) + v = destNodeGraph->addParameter(nodegraphInput.getName(), nodegraphInput.getType()); + } + else + { + InputPtr input = destNodeGraph->addInput(nodegraphInput.getName(), nodegraphInput.getType()); + v = input->asA(); + + if (nodegraphInput.isConnected()) { - input->setOutputString(source.getName()); + // Write connections to upstream nodes. + RtOutput source = nodegraphInput.getConnection(); + RtNode sourceNode = source.getParent(); + input->setNodeName(sourceNode.getName()); + if (sourceNode.numOutputs() > 1) + { + input->setOutputString(source.getName()); + } } } - } - if (v) - { - v->setValueString(nodegraphInput.getValueString()); + if (v) + { + v->setValueString(nodegraphInput.getValueString()); + } } } @@ -1160,7 +1163,7 @@ namespace } else if (typeName == RtNodeGraph::typeName()) { - writeNodeGraph(prim, doc); + writeNodeGraph(prim, doc, writeOptions); } else if (typeName != RtLook::typeName() && typeName != RtLookGroup::typeName() && @@ -1207,7 +1210,7 @@ namespace } } - void writeMasterPrim(DocumentPtr document, PvtStage* stage, PvtPrim* prim) + void writeMasterPrim(DocumentPtr document, PvtStage* stage, PvtPrim* prim, const RtWriteOptions* writeOptions) { if (!prim || prim->isDisposed()) { @@ -1232,13 +1235,13 @@ namespace if (nodeGraph.getDefinition() == nodeDefName) { PvtPrim* graphPrim = PvtObject::ptr(child); - writeNodeGraph(graphPrim, document); + writeNodeGraph(graphPrim, document, writeOptions); break; } } } - void writeNodeDefs(DocumentPtr document, PvtStage* stage, const RtTokenVec& names) + void writeNodeDefs(DocumentPtr document, PvtStage* stage, const RtTokenVec& names, const RtWriteOptions* writeOptions) { // Write all definitions if no names provided RtApi& rtApi = RtApi::get(); @@ -1248,7 +1251,7 @@ namespace for (RtPrim masterPrim : rtApi.getMasterPrims(nodedefFilter)) { PvtPrim* prim = PvtObject::ptr(masterPrim); - writeMasterPrim(document, stage, prim); + writeMasterPrim(document, stage, prim, writeOptions); } } else @@ -1257,7 +1260,7 @@ namespace { RtPrim masterPrim = rtApi.getMasterPrim(name); PvtPrim* prim = PvtObject::ptr(masterPrim); - writeMasterPrim(document, stage, prim); + writeMasterPrim(document, stage, prim, writeOptions); } } } @@ -1274,6 +1277,7 @@ RtReadOptions::RtReadOptions() : RtWriteOptions::RtWriteOptions() : writeIncludes(true), + writeNodeGraphInputs(true), writeFilter(nullptr), materialWriteOp(NONE), desiredMajorVersion(MATERIALX_MAJOR_VERSION), @@ -1473,18 +1477,18 @@ void RtFileIo::write(std::ostream& stream, const RtWriteOptions* options) writeToXmlStream(document, stream, &xmlWriteOptions); } -void RtFileIo::writeDefinitions(std::ostream& stream, const RtTokenVec& names) +void RtFileIo::writeDefinitions(std::ostream& stream, const RtTokenVec& names, const RtWriteOptions* writeOptions) { DocumentPtr document = createDocument(); PvtStage* stage = PvtStage::ptr(_stage); - writeNodeDefs(document, stage, names); + writeNodeDefs(document, stage, names, writeOptions); writeToXmlStream(document, stream); } -void RtFileIo::writeDefinitions(const FilePath& documentPath, const RtTokenVec& names) +void RtFileIo::writeDefinitions(const FilePath& documentPath, const RtTokenVec& names, const RtWriteOptions* writeOptions) { std::ofstream ofs(documentPath.asString()); - writeDefinitions(ofs, names); + writeDefinitions(ofs, names, writeOptions); } } diff --git a/source/MaterialXRuntime/RtFileIo.h b/source/MaterialXRuntime/RtFileIo.h index 979acb8735..83629a7c6c 100644 --- a/source/MaterialXRuntime/RtFileIo.h +++ b/source/MaterialXRuntime/RtFileIo.h @@ -59,6 +59,9 @@ class RtWriteOptions /// includes rather than explicit data. Defaults to true. bool writeIncludes; + // If true, writes out nodegraph inputs + bool writeNodeGraphInputs; + /// Filter function type used for filtering objects during write. /// If the filter returns false the object will not be written. WriteFilter writeFilter; @@ -128,8 +131,8 @@ class RtFileIo /// will be written to the document. void write(const FilePath& documentPath, const RtWriteOptions* writeOptions = nullptr); - void writeDefinitions(std::ostream& stream, const RtTokenVec& names); - void writeDefinitions(const FilePath& documentPath, const RtTokenVec& names); + void writeDefinitions(std::ostream& stream, const RtTokenVec& names, const RtWriteOptions* writeOptions = nullptr); + void writeDefinitions(const FilePath& documentPath, const RtTokenVec& names, const RtWriteOptions* writeOptions = nullptr); protected: /// Read all contents from one or more libraries.