Skip to content

Commit

Permalink
Changes needed for publishing. (#902)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Feldstein <[email protected]>
  • Loading branch information
feldstj and feldstj authored Jun 10, 2020
1 parent 3a942aa commit 3568560
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 37 deletions.
15 changes: 15 additions & 0 deletions source/MaterialXRuntime/Private/PvtApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions source/MaterialXRuntime/Private/PvtApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class PvtApi
return _implementationSearchPaths;
}

void createLibrary(const RtToken& name);

void loadLibrary(const RtToken& name);

void unloadLibrary(const RtToken& name);
Expand Down
10 changes: 10 additions & 0 deletions source/MaterialXRuntime/RtApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions source/MaterialXRuntime/RtApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();

Expand Down
74 changes: 39 additions & 35 deletions source/MaterialXRuntime/RtFileIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RtInput> 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<RtInput> 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<ValueElement>();

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<ValueElement>();

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());
}
}
}

Expand Down Expand Up @@ -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() &&
Expand Down Expand Up @@ -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())
{
Expand All @@ -1232,13 +1235,13 @@ namespace
if (nodeGraph.getDefinition() == nodeDefName)
{
PvtPrim* graphPrim = PvtObject::ptr<PvtPrim>(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();
Expand All @@ -1248,7 +1251,7 @@ namespace
for (RtPrim masterPrim : rtApi.getMasterPrims(nodedefFilter))
{
PvtPrim* prim = PvtObject::ptr<PvtPrim>(masterPrim);
writeMasterPrim(document, stage, prim);
writeMasterPrim(document, stage, prim, writeOptions);
}
}
else
Expand All @@ -1257,7 +1260,7 @@ namespace
{
RtPrim masterPrim = rtApi.getMasterPrim(name);
PvtPrim* prim = PvtObject::ptr<PvtPrim>(masterPrim);
writeMasterPrim(document, stage, prim);
writeMasterPrim(document, stage, prim, writeOptions);
}
}
}
Expand All @@ -1274,6 +1277,7 @@ RtReadOptions::RtReadOptions() :

RtWriteOptions::RtWriteOptions() :
writeIncludes(true),
writeNodeGraphInputs(true),
writeFilter(nullptr),
materialWriteOp(NONE),
desiredMajorVersion(MATERIALX_MAJOR_VERSION),
Expand Down Expand Up @@ -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);
}

}
7 changes: 5 additions & 2 deletions source/MaterialXRuntime/RtFileIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 3568560

Please sign in to comment.