Skip to content

Commit

Permalink
Move Datalibrary into seperate h/cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinbhat committed Oct 3, 2024
1 parent 62434e7 commit 8d78fbc
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 82 deletions.
70 changes: 70 additions & 0 deletions source/MaterialXCore/Datalibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Datalibrary.h>

MATERIALX_NAMESPACE_BEGIN

const DataLibraryPtr standardDataLibrary = DataLibrary::create();

DataLibraryPtr DataLibrary::create()
{
return std::make_shared<DataLibrary>();
}

// use loadDocuments to build this vector
void DataLibrary::loadDataLibrary(vector<DocumentPtr>& librarydocuments)
{
_datalibrary = createDocument();

for (auto library : librarydocuments)
{
for (auto child : library->getChildren())
{
if (child->getCategory().empty())
{
throw Exception("Trying to import child without a category: " + child->getName());
}

const string childName = child->getQualifiedName(child->getName());

// Check for duplicate elements.
ConstElementPtr previous = _datalibrary->getChild(childName);
if (previous)
{
continue;
}

// Create the imported element.
ElementPtr childCopy = _datalibrary->addChildOfCategory(child->getCategory(), childName);
childCopy->copyContentFrom(child);
if (!childCopy->hasFilePrefix() && library->hasFilePrefix())
{
childCopy->setFilePrefix(library->getFilePrefix());
}
if (!childCopy->hasGeomPrefix() && library->hasGeomPrefix())
{
childCopy->setGeomPrefix(library->getGeomPrefix());
}
if (!childCopy->hasColorSpace() && library->hasColorSpace())
{
childCopy->setColorSpace(library->getColorSpace());
}
if (!childCopy->hasNamespace() && library->hasNamespace())
{
childCopy->setNamespace(library->getNamespace());
}
if (!childCopy->hasSourceUri() && library->hasSourceUri())
{
childCopy->setSourceUri(library->getSourceUri());
}
}
}

}


MATERIALX_NAMESPACE_END
41 changes: 41 additions & 0 deletions source/MaterialXCore/Datalibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//

#ifndef MATERIALX_DATALIBRARY
#define MATERIALX_DATALIBRARY

/// @file
/// The top-level DataLibrary class

#include <MaterialXCore/Document.h>

MATERIALX_NAMESPACE_BEGIN

class DataLibrary;
using DataLibraryPtr = shared_ptr<DataLibrary>;
using ConstDataLibraryPtr = shared_ptr<const DataLibrary>;

class MX_CORE_API DataLibrary
{
public:
ConstDocumentPtr dataLibrary()
{
return _datalibrary;
}

void loadDataLibrary(vector<DocumentPtr>& documents);

static DataLibraryPtr create();

private:
// Shared node library used across documents.
DocumentPtr _datalibrary;
};

extern MX_CORE_API const DataLibraryPtr standardDataLibrary;

MATERIALX_NAMESPACE_END

#endif
55 changes: 0 additions & 55 deletions source/MaterialXCore/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,6 @@ MATERIALX_NAMESPACE_BEGIN
const string Document::CMS_ATTRIBUTE = "cms";
const string Document::CMS_CONFIG_ATTRIBUTE = "cmsconfig";

const DataLibraryPtr standardDataLibrary = DataLibrary::create();

// use loadDocuments to build this vector
void DataLibrary::loadDataLibrary(vector<DocumentPtr>& librarydocuments)
{
_datalibrary = createDocument();

for (auto library : librarydocuments)
{
for (auto child : library->getChildren())
{
if (child->getCategory().empty())
{
throw Exception("Trying to import child without a category: " + child->getName());
}

const string childName = child->getQualifiedName(child->getName());

// Check for duplicate elements.
ConstElementPtr previous = _datalibrary->getChild(childName);
if (previous)
{
continue;
}

// Create the imported element.
ElementPtr childCopy = _datalibrary->addChildOfCategory(child->getCategory(), childName);
childCopy->copyContentFrom(child);
if (!childCopy->hasFilePrefix() && library->hasFilePrefix())
{
childCopy->setFilePrefix(library->getFilePrefix());
}
if (!childCopy->hasGeomPrefix() && library->hasGeomPrefix())
{
childCopy->setGeomPrefix(library->getGeomPrefix());
}
if (!childCopy->hasColorSpace() && library->hasColorSpace())
{
childCopy->setColorSpace(library->getColorSpace());
}
if (!childCopy->hasNamespace() && library->hasNamespace())
{
childCopy->setNamespace(library->getNamespace());
}
if (!childCopy->hasSourceUri() && library->hasSourceUri())
{
childCopy->setSourceUri(library->getSourceUri());
}
}
}

}



//
// Document factory function
//
Expand Down
27 changes: 0 additions & 27 deletions source/MaterialXCore/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ using DocumentPtr = shared_ptr<Document>;
/// A shared pointer to a const Document
using ConstDocumentPtr = shared_ptr<const Document>;

class DataLibrary;
using DataLibraryPtr = shared_ptr<DataLibrary>;
using ConstDataLibraryPtr = shared_ptr<const DataLibrary>;

/// @class Document
/// A MaterialX document, which represents the top-level element in the
/// MaterialX ownership hierarchy.
Expand Down Expand Up @@ -693,33 +689,10 @@ class MX_CORE_API Document : public GraphElement

};


class MX_CORE_API DataLibrary
{
public:
ConstDocumentPtr dataLibrary()
{
return _datalibrary;
}

void loadDataLibrary(vector<DocumentPtr>& documents);

static DataLibraryPtr create()
{
return std::make_shared<DataLibrary>();
}

private:
// Shared node library used across documents.
DocumentPtr _datalibrary;
};

/// Create a new Document.
/// @relates Document
MX_CORE_API DocumentPtr createDocument();

extern MX_CORE_API const DataLibraryPtr standardDataLibrary;

MATERIALX_NAMESPACE_END

#endif
1 change: 1 addition & 0 deletions source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Material.h>
#include <MaterialXCore/Datalibrary.h>

#include <deque>

Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenShader/ColorManagementSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <MaterialXGenShader/TypeDesc.h>

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Datalibrary.h>

MATERIALX_NAMESPACE_BEGIN

Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenShader/UnitSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <MaterialXCore/Unit.h>

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Datalibrary.h>

MATERIALX_NAMESPACE_BEGIN

Expand Down

0 comments on commit 8d78fbc

Please sign in to comment.