add usd-21-05-win64_py37

This commit is contained in:
Sébastien Albert committed 2021-12-07 17:16:25 +01:00
1 parent 41d23e71ba
commit 66bc054336
15675 files changed
+3061008

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_API_H
#define PXR_USD_NDR_API_H
#include "pxr/base/arch/export.h"
#if defined(PXR_STATIC)
# define NDR_API
# define NDR_API_TEMPLATE_CLASS(...)
# define NDR_API_TEMPLATE_STRUCT(...)
# define NDR_LOCAL
#else
# if defined(NDR_EXPORTS)
# define NDR_API ARCH_EXPORT
# define NDR_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
# define NDR_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
# else
# define NDR_API ARCH_IMPORT
# define NDR_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
# define NDR_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
# endif
# define NDR_LOCAL ARCH_HIDDEN
#endif
#endif
+45
View File
@@ -0,0 +1,45 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_DEBUG_CODES_H
#define PXR_USD_NDR_DEBUG_CODES_H
#include "pxr/pxr.h"
#include "pxr/base/tf/debug.h"
PXR_NAMESPACE_OPEN_SCOPE
TF_DEBUG_CODES(
NDR_DISCOVERY,
NDR_PARSING,
NDR_INFO,
NDR_STATS,
NDR_DEBUG
);
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_DEBUG_CODES_H
+211
View File
@@ -0,0 +1,211 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_DECLARE_H
#define PXR_USD_NDR_DECLARE_H
/// \file ndr/declare.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/token.h"
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
PXR_NAMESPACE_OPEN_SCOPE
class NdrNode;
class NdrProperty;
class SdfValueTypeName;
/// \file declare.h
///
/// Common typedefs that are used throughout the NDR library.
typedef TfToken NdrIdentifier;
typedef TfToken::HashFunctor NdrIdentifierHashFunctor;
inline const std::string&
NdrGetIdentifierString(const NdrIdentifier& id) { return id.GetString(); }
typedef std::vector<NdrIdentifier> NdrIdentifierVec;
typedef std::unordered_set<NdrIdentifier,
NdrIdentifierHashFunctor> NdrIdentifierSet;
// Token
typedef std::vector<TfToken> NdrTokenVec;
typedef std::unordered_map<TfToken, std::string,
TfToken::HashFunctor> NdrTokenMap;
// Property
typedef NdrProperty* NdrPropertyPtr;
typedef NdrProperty const* NdrPropertyConstPtr;
typedef std::unique_ptr<NdrProperty> NdrPropertyUniquePtr;
typedef std::vector<NdrPropertyUniquePtr> NdrPropertyUniquePtrVec;
typedef std::unordered_map<TfToken, NdrPropertyConstPtr,
TfToken::HashFunctor> NdrPropertyPtrMap;
// Node
typedef NdrNode* NdrNodePtr;
typedef NdrNode const* NdrNodeConstPtr;
typedef std::unique_ptr<NdrNode> NdrNodeUniquePtr;
typedef std::vector<NdrNodeConstPtr> NdrNodeConstPtrVec;
typedef std::vector<NdrNodeUniquePtr> NdrNodeUniquePtrVec;
// Misc
typedef std::vector<std::string> NdrStringVec;
typedef std::pair<TfToken, TfToken> NdrOption;
typedef std::vector<NdrOption> NdrOptionVec;
typedef std::unordered_set<std::string> NdrStringSet;
typedef std::pair<SdfValueTypeName, TfToken> SdfTypeIndicator;
// Version
class NdrVersion {
public:
/// Create an invalid version.
NDR_API
NdrVersion() = default;
/// Create a version with the given major and minor numbers.
/// Numbers must be non-negative, and at least one must be non-zero.
/// On failure generates an error and yields an invalid version.
NDR_API
NdrVersion(int major, int minor = 0);
/// Create a version from a string. On failure generates an error and
/// yields an invalid version.
NDR_API
NdrVersion(const std::string& x);
/// Return an equal version marked as default. It's permitted to mark
/// an invalid version as the default.
NDR_API
NdrVersion GetAsDefault() const
{
return NdrVersion(*this, true);
}
/// Return the major version number or zero for an invalid version.
NDR_API
int GetMajor() const { return _major; }
/// Return the minor version number or zero for an invalid version.
NDR_API
int GetMinor() const { return _minor; }
/// Return true iff this version is marked as default.
NDR_API
bool IsDefault() const { return _isDefault; }
/// Return the version as a string.
NDR_API
std::string GetString() const;
/// Return the version as a identifier suffix.
NDR_API
std::string GetStringSuffix() const;
/// Return a hash for the version.
NDR_API
std::size_t GetHash() const
{
return (static_cast<std::size_t>(_major) << 32) +
static_cast<std::size_t>(_minor);
}
/// Return true iff the version is valid.
NDR_API
explicit operator bool() const
{
return !!*this;
}
/// Return true iff the version is invalid.
NDR_API
bool operator!() const
{
return _major == 0 && _minor == 0;
}
/// Return true iff versions are equal.
NDR_API
friend bool operator==(const NdrVersion& lhs, const NdrVersion& rhs)
{
return lhs._major == rhs._major && lhs._minor == rhs._minor;
}
/// Return true iff versions are not equal.
NDR_API
friend bool operator!=(const NdrVersion& lhs, const NdrVersion& rhs)
{
return !(lhs == rhs);
}
/// Return true iff the left side is less than the right side.
NDR_API
friend bool operator<(const NdrVersion& lhs, const NdrVersion& rhs)
{
return lhs._major < rhs._major ||
(lhs._major == rhs._major && lhs._minor < rhs._minor);
}
/// Return true iff the left side is less than or equal to the right side.
NDR_API
friend bool operator<=(const NdrVersion& lhs, const NdrVersion& rhs)
{
return lhs._major < rhs._major ||
(lhs._major == rhs._major && lhs._minor <= rhs._minor);
}
/// Return true iff the left side is greater than the right side.
NDR_API
friend bool operator>(const NdrVersion& lhs, const NdrVersion& rhs)
{
return !(lhs <= rhs);
}
/// Return true iff the left side is greater than or equal to the right side.
NDR_API
friend bool operator>=(const NdrVersion& lhs, const NdrVersion& rhs)
{
return !(lhs < rhs);
}
private:
NdrVersion(const NdrVersion& x, bool)
: _major(x._major), _minor(x._minor), _isDefault(true) { }
private:
int _major = 0, _minor = 0;
bool _isDefault = false;
};
/// Enumeration used to select nodes by version.
enum NdrVersionFilter {
NdrVersionFilterDefaultOnly,
NdrVersionFilterAllVersions,
NdrNumVersionFilters
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_DECLARE_H
+188
View File
@@ -0,0 +1,188 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_DISCOVERY_PLUGIN_H
#define PXR_USD_NDR_DISCOVERY_PLUGIN_H
/// \file ndr/registry.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/declarePtrs.h"
#include "pxr/base/tf/type.h"
#include "pxr/base/tf/weakBase.h"
#include "pxr/usd/ndr/declare.h"
#include "pxr/usd/ndr/nodeDiscoveryResult.h"
PXR_NAMESPACE_OPEN_SCOPE
/// Register a discovery plugin (`DiscoveryPluginClass`) with the plugin system.
/// If registered, the discovery plugin will execute its discovery process when
/// the registry is instantiated.
#define NDR_REGISTER_DISCOVERY_PLUGIN(DiscoveryPluginClass) \
TF_REGISTRY_FUNCTION(TfType) \
{ \
TfType::Define<DiscoveryPluginClass, TfType::Bases<NdrDiscoveryPlugin>>() \
.SetFactory<NdrDiscoveryPluginFactory<DiscoveryPluginClass>>(); \
}
TF_DECLARE_WEAK_AND_REF_PTRS(NdrDiscoveryPluginContext);
/// A context for discovery. Discovery plugins can use this to get
/// a limited set of non-local information without direct coupling
/// between plugins.
class NdrDiscoveryPluginContext : public TfRefBase, public TfWeakBase
{
public:
NDR_API
virtual ~NdrDiscoveryPluginContext() = default;
/// Returns the source type associated with the discovery type.
/// This may return an empty token if there is no such association.
NDR_API
virtual TfToken GetSourceType(const TfToken& discoveryType) const = 0;
};
TF_DECLARE_WEAK_AND_REF_PTRS(NdrDiscoveryPlugin);
/// \class NdrDiscoveryPlugin
///
/// Interface for discovery plugins.
///
/// Discovery plugins, like the name implies, find nodes. Where the plugin
/// searches is up to the plugin that implements this interface. Examples
/// of discovery plugins could include plugins that look for nodes on the
/// filesystem, another that finds nodes in a cloud service, and another that
/// searches a local database. Multiple discovery plugins that search the
/// filesystem in specific locations/ways could also be created. All discovery
/// plugins are executed as soon as the registry is instantiated.
///
/// These plugins simply report back to the registry what nodes they found in
/// a generic way. The registry doesn't know much about the innards of the
/// nodes yet, just that the nodes exist. Understanding the nodes is the
/// responsibility of another set of plugins defined by the `NdrParserPlugin`
/// interface.
///
/// Discovery plugins report back to the registry via `NdrNodeDiscoveryResult`s.
/// These are small, lightweight classes that contain the information for a
/// single node that was found during discovery. The discovery result only
/// includes node information that can be gleaned pre-parse, so the data is
/// fairly limited; to see exactly what's included, and what is expected to
/// be populated, see the documentation for `NdrNodeDiscoveryResult`.
///
/// \section create How to Create a Discovery Plugin
/// There are three steps to creating a discovery plugin:
/// <ul>
/// <li>
/// Implement the discovery plugin interface, `NdrDiscoveryPlugin`
/// </li>
/// <li>
/// Register your new plugin with the registry. The registration macro
/// must be called in your plugin's implementation file:
/// \code{.cpp}
/// NDR_REGISTER_DISCOVERY_PLUGIN(YOUR_DISCOVERY_PLUGIN_CLASS_NAME)
/// \endcode
/// This macro is available in discoveryPlugin.h.
/// </li>
/// <li>
/// In the same folder as your plugin, create a `plugInfo.json` file.
/// This file must be formatted like so, substituting
/// `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`, and `YOUR_DISPLAY_NAME`:
/// \code{.json}
/// {
/// "Plugins": [{
/// "Type": "library",
/// "Name": "YOUR_LIBRARY_NAME",
/// "Root": "@PLUG_INFO_ROOT@",
/// "LibraryPath": "@PLUG_INFO_LIBRARY_PATH@",
/// "ResourcePath": "@PLUG_INFO_RESOURCE_PATH@",
/// "Info": {
/// "Types": {
/// "YOUR_CLASS_NAME" : {
/// "bases": ["NdrDiscoveryPlugin"],
/// "displayName": "YOUR_DISPLAY_NAME"
/// }
/// }
/// }
/// }]
/// }
/// \endcode
///
/// The NDR ships with one discovery plugin, the
/// `_NdrFilesystemDiscoveryPlugin`. Take a look at NDR's plugInfo.json
/// file for example values for `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`,
/// and `YOUR_DISPLAY_NAME`. If multiple discovery plugins exist in the
/// same folder, you can continue adding additional plugins under the
/// `Types` key in the JSON. More detailed information about the
/// plugInfo.json format can be found in the documentation for the
/// `plug` library (in pxr/base).
/// </li>
/// </ul>
///
class NdrDiscoveryPlugin : public TfRefBase, public TfWeakBase
{
public:
using Context = NdrDiscoveryPluginContext;
NDR_API
NdrDiscoveryPlugin();
NDR_API
virtual ~NdrDiscoveryPlugin();
/// Finds and returns all nodes that the implementing plugin should be
/// aware of.
NDR_API
virtual NdrNodeDiscoveryResultVec DiscoverNodes(const Context&) = 0;
/// Gets the URIs that this plugin is searching for nodes in.
NDR_API
virtual const NdrStringVec& GetSearchURIs() const = 0;
};
/// \cond
/// Factory classes should be hidden from the documentation.
class NdrDiscoveryPluginFactoryBase : public TfType::FactoryBase
{
public:
NDR_API
virtual NdrDiscoveryPluginRefPtr New() const = 0;
};
template <class T>
class NdrDiscoveryPluginFactory : public NdrDiscoveryPluginFactoryBase
{
public:
NdrDiscoveryPluginRefPtr New() const override
{
return TfCreateRefPtr(new T);
}
};
/// \endcond
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_DISCOVERY_PLUGIN_H
+110
View File
@@ -0,0 +1,110 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_FILESYSTEM_DISCOVERY_H
#define PXR_USD_NDR_FILESYSTEM_DISCOVERY_H
/// \file ndrDiscovery/filesystemDiscovery.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/usd/ndr/discoveryPlugin.h"
#include <functional>
PXR_NAMESPACE_OPEN_SCOPE
TF_DECLARE_WEAK_AND_REF_PTRS(_NdrFilesystemDiscoveryPlugin);
/// \class _NdrFilesystemDiscoveryPlugin
///
/// Discovers nodes on the filesystem. The provided search paths are walked to
/// find files that have certain extensions. If a file with a matching extension
/// is found, it is turned into a `NdrNodeDiscoveryResult` and will be parsed
/// into a node when its information is accessed.
///
/// Parameters for this plugin are specified via environment variables (which
/// must be set before the library is loaded):
///
/// PXR_NDR_FS_PLUGIN_SEARCH_PATHS - The paths that should be searched,
/// recursively, for files that represent nodes. Paths should be separated by
/// either a ':' or a ';' depending on your platform (it should mimic the PATH
/// env var on your platform). See ARCH_PATH_LIST_SEP.
///
/// PXR_NDR_FS_PLUGIN_ALLOWED_EXTS - The extensions on files that define nodes.
/// Do not include the leading ".". Extensions should be separated by a colon.
///
/// PXR_NDR_FS_PLUGIN_FOLLOW_SYMLINKS - Whether symlinks should be followed
/// while walking the search paths. Set to "true" (case sensitive) if they
/// should be followed.
///
class _NdrFilesystemDiscoveryPlugin final : public NdrDiscoveryPlugin
{
public:
/// A filter for discovered nodes. If the function returns false
/// then the discovered node is discarded. Otherwise the function
/// can modify the discovery result.
using Filter = std::function<bool(NdrNodeDiscoveryResult&)>;
/// Constructor.
NDR_API
_NdrFilesystemDiscoveryPlugin();
/// DiscoverNodes() will pass each result to the given function for
/// modification. If the function returns false then the result is
/// discarded.
NDR_API
_NdrFilesystemDiscoveryPlugin(Filter filter);
/// Destructor
NDR_API
~_NdrFilesystemDiscoveryPlugin() {}
/// Discover all of the nodes that appear within the the search paths
/// provided and match the extensions provided.
NDR_API
NdrNodeDiscoveryResultVec DiscoverNodes(const Context&) override;
/// Gets the paths that this plugin is searching for nodes in.
NDR_API
const NdrStringVec& GetSearchURIs() const override { return _searchPaths; }
private:
/// The paths (abs) indicating where the plugin should search for nodes.
NdrStringVec _searchPaths;
/// The extensions (excluding leading '.') that signify a valid node file.
/// The extension will be used as the `type` member in the resulting
/// `NdrNodeDiscoveryResult` instance.
NdrStringVec _allowedExtensions;
/// Whether or not to follow symlinks while scanning directories for files.
bool _followSymlinks;
// The filter to run on the results.
Filter _filter;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_FILESYSTEM_DISCOVERY_H
@@ -0,0 +1,66 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_FILESYSTEM_DISCOVERY_HELPERS_H
#define PXR_USD_NDR_FILESYSTEM_DISCOVERY_HELPERS_H
/// \file ndr/filesystemDiscoveryHelpers.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/usd/ndr/declare.h"
#include "pxr/usd/ndr/nodeDiscoveryResult.h"
PXR_NAMESPACE_OPEN_SCOPE
class NdrDiscoveryPluginContext;
/// \file filesystemDiscoveryHelpers.h
///
/// Provides utilities that the default filesystem discovery plugin uses. If
/// a custom filesystem discovery plugin is needed, these can be used to fill
/// in a large chunk of the functionality.
///
/// Walks the specified search paths, optionally following symlinks. Paths
/// are walked recursively, and each directory has `FsHelpersExamineFiles()`
/// called on it. Only files that match one of the provided extensions (case
/// insensitive) are candidates for being turned into `NdrDiscoveryResult`s.
/// Returns a vector of discovery results that have been found while walking
/// the search paths. In each result the name and identifier will be the
/// same, the version will be invalid and default, and the family will
/// be empty. The caller is expected to adjust these as appropriate. A
/// naive client with no versions and no family will work correctly.
NDR_API
NdrNodeDiscoveryResultVec
NdrFsHelpersDiscoverNodes(
const NdrStringVec& searchPaths,
const NdrStringVec& allowedExtensions,
bool followSymlinks = true,
const NdrDiscoveryPluginContext* context = nullptr
);
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_FILESYSTEM_DISCOVERY_HELPERS_H
+223
View File
@@ -0,0 +1,223 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_NODE_H
#define PXR_USD_NDR_NODE_H
/// \file ndr/node.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/token.h"
#include "pxr/usd/ndr/declare.h"
#include "pxr/usd/ndr/nodeDiscoveryResult.h"
#include "pxr/usd/ndr/property.h"
PXR_NAMESPACE_OPEN_SCOPE
/// \class NdrNode
///
/// Represents an abstract node. Describes information like the name of the
/// node, what its inputs and outputs are, and any associated metadata.
///
/// In almost all cases, this class will not be used directly. More specialized
/// nodes can be created that derive from `NdrNode`; those specialized nodes can
/// add their own domain-specific data and methods.
///
class NdrNode
{
public:
/// Constructor.
NDR_API
NdrNode(const NdrIdentifier& identifier,
const NdrVersion& version,
const std::string& name,
const TfToken& family,
const TfToken& context,
const TfToken& sourceType,
const std::string& definitionURI,
const std::string& implementationURI,
NdrPropertyUniquePtrVec&& properties,
const NdrTokenMap& metadata = NdrTokenMap(),
const std::string &sourceCode = std::string());
/// Destructor.
NDR_API
virtual ~NdrNode();
/// \name The Basics
/// @{
/// Return the identifier of the node.
const NdrIdentifier& GetIdentifier() const { return _identifier; }
/// Return the version of the node
NdrVersion GetVersion() const { return _version; }
/// Gets the name of the node.
const std::string& GetName() const { return _name; }
/// Gets the name of the family that the node belongs to. An empty token
/// will be returned if the node does not belong to a family.
const TfToken& GetFamily() const { return _family; }
/// Gets the context of the node.
///
/// The context is the context that the node declares itself as having (or,
/// if a particular node does not declare a context, it will be assigned a
/// default context by the parser).
///
/// As a concrete example from the `Sdr` library, a shader with a specific
/// source type may perform different duties vs. another shader with the
/// same source type. For example, one shader with a source type of
/// `SdrArgsParser::SourceType` may declare itself as having a context of
/// 'pattern', while another shader of the same source type may say it is
/// used for lighting, and thus has a context of 'light'.
const TfToken& GetContext() const { return _context; }
/// Gets the type of source that this node originated from.
///
/// Note that this is distinct from `GetContext()`, which is the type that
/// the node declares itself as having.
///
/// As a concrete example from the `Sdr` library, several shader parsers
/// exist and operate on different types of shaders. In this scenario, each
/// distinct type of shader (OSL, Args, etc) is considered a different
/// _source_, even though they are all shaders. In addition, the shaders
/// under each source type may declare themselves as having a specific
/// context (shaders can serve different roles). See `GetContext()` for
/// more information on this.
const TfToken& GetSourceType() const { return _sourceType; }
/// Gets the URI to the resource that provided this node's
/// definition. Could be a path to a file, or some other resource
/// identifier. This URI should be fully resolved.
///
/// \sa NdrNode::GetResolvedImplementationURI()
const std::string& GetResolvedDefinitionURI() const { return _definitionURI; }
/// Gets the URI to the resource that provides this node's
/// implementation. Could be a path to a file, or some other resource
/// identifier. This URI should be fully resolved.
///
/// \sa NdrNode::GetResolvedDefinitionURI()
const std::string& GetResolvedImplementationURI() const { return _implementationURI; }
/// Returns the source code for this node. This will be empty for most
/// nodes. It will be non-empty only for the nodes that are constructed
/// using \ref NdrRegistry::GetNodeFromSourceCode(), in which case, the
/// source code has not been parsed (or even compiled) yet.
///
/// An unparsed node with non-empty source-code but no properties is
/// considered to be invalid. Once the node is parsed and the relevant
/// properties and metadata are extracted from the source code, the node
/// becomes valid.
///
/// \sa NdrNode::IsValid
const std::string &GetSourceCode() const { return _sourceCode; }
/// Whether or not this node is valid. A node that is valid indicates that
/// the parser plugin was able to successfully parse the contents of this
/// node.
///
/// Note that if a node is not valid, some data like its name, URI, source
/// code etc. could still be available (data that was obtained during the
/// discovery process). However, other data that must be gathered from the
/// parsing process will NOT be available (eg, inputs and outputs).
NDR_API
virtual bool IsValid() const { return _isValid; }
/// Gets a string with basic information about this node. Helpful for
/// things like adding this node to a log.
NDR_API
virtual std::string GetInfoString() const;
/// @}
/// \name Inputs and Outputs
/// An input or output is also generically referred to as a "property".
/// @{
/// Get an ordered list of all the input names on this node.
NDR_API
const NdrTokenVec& GetInputNames() const;
/// Get an ordered list of all the output names on this node.
NDR_API
const NdrTokenVec& GetOutputNames() const;
/// Get an input property by name. `nullptr` is returned if an input with
/// the given name does not exist.
NDR_API
NdrPropertyConstPtr GetInput(const TfToken& inputName) const;
/// Get an output property by name. `nullptr` is returned if an output with
/// the given name does not exist.
NDR_API
NdrPropertyConstPtr GetOutput(const TfToken& outputName) const;
/// @}
/// \name Metadata
/// The metadata returned here is a direct result of what the parser plugin
/// is able to determine about the node. See the documentation for a
/// specific parser plugin to get help on what the parser is looking for to
/// populate these values.
/// @{
/// All metadata that came from the parse process. Specialized nodes may
/// isolate values in the metadata (with possible manipulations and/or
/// additional parsing) and expose those values in their API.
NDR_API
const NdrTokenMap& GetMetadata() const;
/// @}
protected:
NdrNode& operator=(const NdrNode&) = delete;
bool _isValid;
NdrIdentifier _identifier;
NdrVersion _version;
std::string _name;
TfToken _family;
TfToken _context;
TfToken _sourceType;
std::string _definitionURI;
std::string _implementationURI;
NdrPropertyUniquePtrVec _properties;
NdrTokenMap _metadata;
std::string _sourceCode;
NdrPropertyPtrMap _inputs;
NdrTokenVec _inputNames;
NdrPropertyPtrMap _outputs;
NdrTokenVec _outputNames;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_NODE_H
+162
View File
@@ -0,0 +1,162 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_NODE_DISCOVERY_RESULT_H
#define PXR_USD_NDR_NODE_DISCOVERY_RESULT_H
#include "pxr/usd/ndr/declare.h"
PXR_NAMESPACE_OPEN_SCOPE
/// Represents the raw data of a node, and some other bits of metadata, that
/// were determined via a `NdrDiscoveryPlugin`.
struct NdrNodeDiscoveryResult {
/// Constructor.
NdrNodeDiscoveryResult(
const NdrIdentifier& identifier,
const NdrVersion& version,
const std::string& name,
const TfToken& family,
const TfToken& discoveryType,
const TfToken& sourceType,
const std::string& uri,
const std::string& resolvedUri,
const std::string &sourceCode=std::string(),
const NdrTokenMap &metadata=NdrTokenMap(),
const std::string& blindData=std::string(),
const TfToken& subIdentifier=TfToken(),
const NdrTokenVec& aliases=NdrTokenVec()
) : identifier(identifier),
version(version),
name(name),
family(family),
discoveryType(discoveryType),
sourceType(sourceType),
uri(uri),
resolvedUri(resolvedUri),
sourceCode(sourceCode),
metadata(metadata),
blindData(blindData),
subIdentifier(subIdentifier),
aliases(aliases)
{ }
/// The node's identifier.
///
/// How the node is identified. In many cases this will be the
/// name of the file or resource that this node originated from.
/// E.g. "mix_float_2_1". The identifier must be unique for a
/// given sourceType.
NdrIdentifier identifier;
/// The node's version. This may or may not be embedded in the
/// identifier, it's up to implementations. E.g a node with
/// identifier "mix_float_2_1" might have version 2.1.
NdrVersion version;
/// The node's name.
///
/// A version independent identifier for the node type. This will
/// often embed type parameterization but should not embed the
/// version. E.g a node with identifier "mix_float_2_1" might have
/// name "mix_float".
std::string name;
/// The node's family.
///
/// A node's family is an optional piece of metadata that specifies a
/// generic grouping of nodes. E.g a node with identifier
/// "mix_float_2_1" might have family "mix".
TfToken family;
/// The node's discovery type.
///
/// The type could be the file extension, or some other type of metadata
/// that can signify a type prior to parsing. See the documentation for
/// `NdrParserPlugin` and `NdrParserPlugin::DiscoveryTypes` for more
/// information on how this value is used.
TfToken discoveryType;
/// The node's source type.
///
/// This type is unique to the parsing plugin
/// (`NdrParserPlugin::SourceType`), and determines the source of the node.
/// See `NdrNode::GetSourceType()` for more information.
TfToken sourceType;
/// The node's origin.
///
/// This may be a filesystem path, a URL pointing to a resource in the
/// cloud, or some other type of resource identifier.
std::string uri;
/// The node's fully-resolved URI.
///
/// For example, this might be an absolute path when the original URI was
/// a relative path. In most cases, this is the path that `Ar`'s
/// `Resolve()` returns. In any case, this path should be locally
/// accessible.
std::string resolvedUri;
/// The node's entire source code.
///
/// The source code is parsed (if non-empty) by parser plugins when the
/// resolvedUri value is empty.
std::string sourceCode;
/// The node's metadata collected during the discovery process.
///
/// Additional metadata may be present in the node's source, in the asset
/// pointed to by resolvedUri or in sourceCode (if resolvedUri is empty).
/// In general, parsers should override this data with metadata from the
/// shader source.
NdrTokenMap metadata;
/// An optional detail for the parser plugin. The parser plugin
/// defines the meaning of this data so the discovery plugin must
/// be written to match.
std::string blindData;
/// The subIdentifier is associated with a particular asset and refers to a
/// specific definition within the asset. The asset is the one referred to
/// by `NdrRegistry::GetNodeFromAsset()`. The subIdentifier is not needed
/// for all cases where the node definition is not associated with an asset.
/// Even if the node definition is associated with an asset, the
/// subIdentifier is only needed if the asset specifies multiple definitions
/// rather than a single definition.
TfToken subIdentifier;
/// A list of aliases that can be used when looking up the node by
/// identifier in the NdrRegistry.
///
/// See NdrRegistry::GetNodesByIdentifier, NdrRegistry::GetNodeByIdentifier,
/// and NdrRegistry::GetNodeByIdentifierAndType.
NdrTokenVec aliases;
};
typedef std::vector<NdrNodeDiscoveryResult> NdrNodeDiscoveryResultVec;
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_NODE_DISCOVERY_RESULT_H
+190
View File
@@ -0,0 +1,190 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_PARSER_PLUGIN_H
#define PXR_USD_NDR_PARSER_PLUGIN_H
/// \file ndr/parserPlugin.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/type.h"
#include "pxr/base/tf/weakBase.h"
#include "pxr/base/tf/weakPtr.h"
#include "pxr/usd/ndr/declare.h"
PXR_NAMESPACE_OPEN_SCOPE
// Forward declarations
struct NdrNodeDiscoveryResult;
/// Register a parser plugin with the plugin system.
#define NDR_REGISTER_PARSER_PLUGIN(ParserPluginClass) \
TF_REGISTRY_FUNCTION(TfType) \
{ \
TfType::Define<ParserPluginClass, TfType::Bases<NdrParserPlugin>>() \
.SetFactory<NdrParserPluginFactory<ParserPluginClass>>(); \
}
/// \class NdrParserPlugin
///
/// Interface for parser plugins.
///
/// Parser plugins take a `NdrNodeDiscoveryResult` from the discovery process
/// and creates a full `NdrNode` instance (or, in the case of a real-world
/// scenario, a specialized node that derives from `NdrNode`). The parser that
/// is selected to run is ultimately decided by the registry, and depends on the
/// `NdrNodeDiscoveryResult`'s `discoveryType` member. A parser plugin's
/// `GetDiscoveryTypes()` method is how this link is made. If a discovery result
/// has a `discoveryType` of 'foo', and `SomeParserPlugin` has 'foo' included
/// in its `GetDiscoveryTypes()` return value, `SomeParserPlugin` will parse
/// that discovery result.
///
/// Another kind of 'type' within the parser plugin is the 'source type'. The
/// discovery type simply acts as a way to link a discovery result to a parser
/// plugin. On the other hand, a 'source type' acts as an umbrella type that
/// groups all of the discovery types together. For example, if a plugin handled
/// discovery types 'foo', 'bar', and 'baz' (which are all related because they
/// are all handled by the same parser), they may all be grouped under one
/// unifying source type. This type is available on the node via
/// `NdrNode::GetSourceType()`.
///
/// \section create How to Create a Parser Plugin
/// There are three steps to creating a parser plugin:
/// <ul>
/// <li>
/// Implement the parser plugin interface. An example parser plugin is
/// available in the plugin folder under `sdrOsl`. The `Parse()` method
/// should return the specialized node that derives from `NdrNode` (and
/// this node should also be constructed with its specialized
/// properties). Examples of a specialized node and property class are
/// `SdrShaderNode` and `SdrShaderProperty`.
/// </li>
/// <li>
/// Register your new plugin with the registry. The registration macro
/// must be called in your plugin's implementation file:
/// \code{.cpp}
/// NDR_REGISTER_PARSER_PLUGIN(<YOUR_PARSER_PLUGIN_CLASS_NAME>)
/// \endcode
/// This macro is available in parserPlugin.h.
/// </li>
/// <li>
/// In the same folder as your plugin, create a `plugInfo.json` file.
/// This file must be formatted like so, substituting
/// `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`, and `YOUR_DISPLAY_NAME`:
/// \code{.json}
/// {
/// "Plugins": [{
/// "Type": "library",
/// "Name": "YOUR_LIBRARY_NAME",
/// "Root": "@PLUG_INFO_ROOT@",
/// "LibraryPath": "@PLUG_INFO_LIBRARY_PATH@",
/// "ResourcePath": "@PLUG_INFO_RESOURCE_PATH@",
/// "Info": {
/// "Types": {
/// "YOUR_CLASS_NAME" : {
/// "bases": ["NdrParserPlugin"],
/// "displayName": "YOUR_DISPLAY_NAME"
/// }
/// }
/// }
/// }]
/// }
/// \endcode
///
/// The SDR ships with one parser plugin, the `SdrOslParserPlugin`. Take
/// a look at its plugInfo.json file for example values for
/// `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`, and `YOUR_DISPLAY_NAME`. If
/// multiple parser plugins exist in the same folder, you can continue
/// adding additional plugins under the `Types` key in the JSON. More
/// detailed information about the plugInfo.json format can be found in
/// the documentation for the `plug` library (in pxr/base).
/// </li>
/// </ul>
class NdrParserPlugin : public TfWeakBase
{
public:
NDR_API
NdrParserPlugin();
NDR_API
virtual ~NdrParserPlugin();
/// Takes the specified `NdrNodeDiscoveryResult` instance, which was a
/// result of the discovery process, and generates a new `NdrNode`.
/// The node's name, source type, and family must match.
NDR_API
virtual NdrNodeUniquePtr Parse(
const NdrNodeDiscoveryResult& discoveryResult) = 0;
/// Returns the types of nodes that this plugin can parse.
///
/// "Type" here is the discovery type (in the case of files, this will
/// probably be the file extension, but in other systems will be data that
/// can be determined during discovery). This type should only be used to
/// match up a `NdrNodeDiscoveryResult` to its parser plugin; this value is
/// not exposed in the node's API.
NDR_API
virtual const NdrTokenVec& GetDiscoveryTypes() const = 0;
/// Returns the source type that this parser operates on.
///
/// A source type is the most general type for a node. The parser plugin is
/// responsible for parsing all discovery results that have the types
/// declared under `GetDiscoveryTypes()`, and those types are collectively
/// identified as one "source type".
NDR_API
virtual const TfToken& GetSourceType() const = 0;
/// Gets an invalid node based on the discovery result provided. An invalid
/// node is a node that has no properties, but may have basic data found
/// during discovery.
NDR_API
static NdrNodeUniquePtr GetInvalidNode(const NdrNodeDiscoveryResult& dr);
};
/// \cond
/// Factory classes should be hidden from the documentation.
class NdrParserPluginFactoryBase : public TfType::FactoryBase
{
public:
virtual NdrParserPlugin* New() const = 0;
};
template <class T>
class NdrParserPluginFactory : public NdrParserPluginFactoryBase
{
public:
virtual NdrParserPlugin* New() const
{
return new T;
}
};
/// \endcond
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_PARSER_PLUGIN_H
+178
View File
@@ -0,0 +1,178 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_PROPERTY_H
#define PXR_USD_NDR_PROPERTY_H
/// \file ndr/property.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/token.h"
#include "pxr/base/vt/value.h"
#include "pxr/usd/ndr/declare.h"
PXR_NAMESPACE_OPEN_SCOPE
/// \class NdrProperty
///
/// Represents a property (input or output) that is part of a `NdrNode`
/// instance.
///
/// A property must have a name and type, but may also specify a host of
/// additional metadata. Instances can also be queried to determine if another
/// `NdrProperty` instance can be connected to it.
///
/// In almost all cases, this class will not be used directly. More specialized
/// properties can be created that derive from `NdrProperty`; those specialized
/// properties can add their own domain-specific data and methods.
class NdrProperty
{
public:
/// Constructor.
NDR_API
NdrProperty(
const TfToken& name,
const TfToken& type,
const VtValue& defaultValue,
bool isOutput,
size_t arraySize,
bool isDynamicArray,
const NdrTokenMap& metadata
);
/// Destructor.
NDR_API
virtual ~NdrProperty();
/// \name The Basics
/// @{
/// Gets the name of the property.
NDR_API
const TfToken& GetName() const { return _name; }
/// Gets the type of the property.
NDR_API
const TfToken& GetType() const { return _type; }
/// Gets this property's default value.
NDR_API
const VtValue& GetDefaultValue() const { return _defaultValue; }
/// Whether this property is an output.
NDR_API
bool IsOutput() const { return _isOutput; }
/// Whether this property's type is an array type.
NDR_API
bool IsArray() const { return (_arraySize > 0) || _isDynamicArray; }
/// Whether this property's array type is dynamically-sized.
NDR_API
bool IsDynamicArray() const { return _isDynamicArray; };
/// Gets this property's array size.
///
/// If this property is a fixed-size array type, the array size is returned.
/// In the case of a dynamically-sized array, this method returns the array
/// size that the parser reports, and should not be relied upon to be
/// accurate. A parser may report -1 for the array size, for example, to
/// indicate a dynamically-sized array. For types that are not a fixed-size
/// array or dynamic array, this returns 0.
NDR_API
int GetArraySize() const { return _arraySize; }
/// Gets a string with basic information about this property. Helpful for
/// things like adding this property to a log.
NDR_API
virtual std::string GetInfoString() const;
/// @}
/// \name Metadata
/// The metadata returned here is a direct result of what the parser plugin
/// is able to determine about the node. See the documentation for a
/// specific parser plugin to get help on what the parser is looking for to
/// populate these values.
/// @{
/// All of the metadata that came from the parse process.
NDR_API
virtual const NdrTokenMap& GetMetadata() const { return _metadata; }
/// @}
/// \name Connection Information
/// @{
/// Whether this property can be connected to other properties.
NDR_API
virtual bool IsConnectable() const;
/// Determines if this property can be connected to the specified property.
NDR_API
virtual bool CanConnectTo(const NdrProperty& other) const;
/// @}
/// \name Utilities
/// @{
/// Converts the property's type from `GetType()` into a `SdfValueTypeName`.
///
/// Two scenarios can result: an exact mapping from property type to Sdf
/// type, and an inexact mapping. In the first scenario, the first element
/// in the pair will be the cleanly-mapped Sdf type, and the second element,
/// a TfToken, will be empty. In the second scenario, the Sdf type will be
/// set to `Token` to indicate an unclean mapping, and the second element
/// will be set to the original type returned by `GetType()`.
///
/// This base property class is generic and cannot know ahead of time how to
/// perform this mapping reliably, thus it will always fall into the second
/// scenario. It is up to specialized properties to perform the mapping.
NDR_API
virtual const SdfTypeIndicator GetTypeAsSdfType() const;
/// @}
protected:
NdrProperty& operator=(const NdrProperty&) = delete;
TfToken _name;
TfToken _type;
VtValue _defaultValue;
bool _isOutput;
size_t _arraySize;
bool _isDynamicArray;
bool _isConnectable;
NdrTokenMap _metadata;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_PROPERTY_H
+403
View File
@@ -0,0 +1,403 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_NDR_REGISTRY_H
#define PXR_USD_NDR_REGISTRY_H
/// \file ndr/registry.h
#include "pxr/pxr.h"
#include "pxr/usd/ndr/api.h"
#include "pxr/base/tf/weakBase.h"
#include "pxr/usd/ndr/declare.h"
#include "pxr/usd/ndr/discoveryPlugin.h"
#include "pxr/usd/ndr/node.h"
#include "pxr/usd/ndr/nodeDiscoveryResult.h"
#include "pxr/usd/ndr/parserPlugin.h"
#include "pxr/usd/sdf/assetPath.h"
#include <mutex>
PXR_NAMESPACE_OPEN_SCOPE
/// \class NdrRegistry
///
/// The registry provides access to node information. "Discovery Plugins" are
/// responsible for finding the nodes that should be included in the registry.
///
/// Discovery plugins are found through the plugin system. If additional
/// discovery plugins need to be specified, a client can pass them to
/// `SetExtraDiscoveryPlugins()`.
///
/// When the registry is first told about the discovery plugins, the plugins
/// will be asked to discover nodes. These plugins will generate
/// `NdrNodeDiscoveryResult` instances, which only contain basic metadata. Once
/// the client asks for information that would require the node's contents to
/// be parsed (eg, what its inputs and outputs are), the registry will begin the
/// parsing process on an as-needed basis. See `NdrNodeDiscoveryResult` for the
/// information that can be retrieved without triggering a parse.
///
/// Some methods in this library may allow for a "family" to be provided. A
/// family is simply a generic grouping which is optional.
///
class NdrRegistry : public TfWeakBase
{
public:
using DiscoveryPluginRefPtrVec = NdrDiscoveryPluginRefPtrVector;
/// Allows the client to set any additional discovery plugins that would
/// otherwise NOT be found through the plugin system. Runs the discovery
/// process for the specified plugins immediately.
///
/// Note that this method cannot be called after any nodes in the registry
/// have been parsed (eg, through GetNode*()), otherwise an error will
/// result.
NDR_API
void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
/// Allows the client to set any additional discovery plugins that would
/// otherwise NOT be found through the plugin system. Runs the discovery
/// process for the specified plugins immediately.
///
/// Note that this method cannot be called after any nodes in the registry
/// have been parsed (eg, through GetNode*()), otherwise an error will
/// result.
NDR_API
void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
/// Allows the client to set any additional parser plugins that would
/// otherwise NOT be found through the plugin system.
///
/// Note that this method cannot be called after any nodes in the registry
/// have been parsed (eg, through GetNode*()), otherwise an error will
/// result.
NDR_API
void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
/// Parses the given \p asset, constructs a NdrNode from it and adds it to
/// the registry.
///
/// Nodes created from an asset using this API can be looked up by the
/// unique identifier and sourceType of the returned node, or by URI,
/// which will be set to the unresolved asset path value.
///
/// \p metadata contains additional metadata needed for parsing and
/// compiling the source code in the file pointed to by \p asset correctly.
/// This metadata supplements the metadata available in the asset and
/// overrides it in cases where there are key collisions.
///
/// \p subidentifier is optional, and it would be used to indicate a
/// particular definition in the asset file if the asset contains multiple
/// node definitions.
///
/// \p sourceType is optional, and it is only needed to indicate a
/// particular type if the asset file is capable of representing a node
/// definition of multiple source types.
///
/// Returns a valid node if the asset is parsed successfully using one
/// of the registered parser plugins.
NDR_API
NdrNodeConstPtr GetNodeFromAsset(const SdfAssetPath &asset,
const NdrTokenMap &metadata,
const TfToken &subIdentifier=TfToken(),
const TfToken &sourceType=TfToken());
/// Parses the given \p sourceCode string, constructs a NdrNode from it and
/// adds it to the registry. The parser to be used is determined by the
/// specified \p sourceType.
///
/// Nodes created from source code using this API can be looked up by the
/// unique identifier and sourceType of the returned node.
///
/// \p metadata contains additional metadata needed for parsing and
/// compiling the source code correctly. This metadata supplements the
/// metadata available in \p sourceCode and overrides it cases where there
/// are key collisions.
///
/// Returns a valid node if the given source code is parsed successfully
/// using the parser plugins that is registered for the specified
/// \p sourceType.
NDR_API
NdrNodeConstPtr GetNodeFromSourceCode(const std::string &sourceCode,
const TfToken &sourceType,
const NdrTokenMap &metadata);
/// Get the locations where the registry is searching for nodes.
///
/// Depending on which discovery plugins were used, this may include
/// non-filesystem paths.
NDR_API
NdrStringVec GetSearchURIs() const;
/// Get the identifiers of all the nodes that the registry is aware of.
///
/// This will not run the parsing plugins on the nodes that have been
/// discovered, so this method is relatively quick. Optionally, a "family"
/// name can be specified to only get the identifiers of nodes that belong
/// to that family and a filter can be specified to get just the default
/// version (the default) or all versions of the node.
NDR_API
NdrIdentifierVec
GetNodeIdentifiers(const TfToken& family = TfToken(),
NdrVersionFilter filter =
NdrVersionFilterDefaultOnly) const;
/// Get the names of all the nodes that the registry is aware of.
///
/// This will not run the parsing plugins on the nodes that have been
/// discovered, so this method is relatively quick. Optionally, a "family"
/// name can be specified to only get the names of nodes that belong to
/// that family.
NDR_API
NdrStringVec GetNodeNames(const TfToken& family = TfToken()) const;
/// Get the node with the specified \p identifier, and an optional
/// \p sourceTypePriority list specifying the set of node SOURCE types (see
/// `NdrNode::GetSourceType()`) that should be searched.
///
/// If no sourceTypePriority is specified, the first encountered node with
/// the specified identifier will be returned (first is arbitrary) if found.
/// If no matching node is found then the first node found with an alias
/// matching the identifier will be returned if one exists.
///
/// If a sourceTypePriority list is specified, then this will iterate
/// through each source type and try to find a node matching by identifier
/// or alias. This is equivalent to calling
/// NdrRegistry::GetNodeByIdentifierAndType for each source type until a
/// node is found.
///
/// Nodes of the same identifier but different source type can exist
/// in the registry. If a node 'Foo' with source types 'abc' and 'xyz'
/// exist in the registry, and you want to make sure the 'abc' version
/// is fetched before the 'xyz' version, the priority list would be
/// specified as ['abc', 'xyz']. If the 'abc' version did not exist in
/// the registry, then the 'xyz' version would be returned.
///
/// Returns `nullptr` if a node matching the arguments can't be found.
///
/// \sa NdrNodeDiscoveryResult::aliases
NDR_API
NdrNodeConstPtr GetNodeByIdentifier(const NdrIdentifier& identifier,
const NdrTokenVec& sourceTypePriority = NdrTokenVec());
/// Get the node with the specified \p identifier and \p sourceType. If,
/// for the given sourceType, there is no node with the given identifier,
/// this will instead search for a node which has an alias that matches the
/// identifier and return it if it exists. Otherwise there is no matching
/// node for the sourceType and nullptr is returned.
///
/// \sa NdrNodeDiscoveryResult::aliases
NDR_API
NdrNodeConstPtr GetNodeByIdentifierAndType(const NdrIdentifier& identifier,
const TfToken& sourceType);
/// Get the node with the specified name. An optional priority list
/// specifies the set of node SOURCE types (\sa NdrNode::GetSourceType())
/// that should be searched and in what order.
///
/// Optionally, a filter can be specified to consider just the default
/// versions of nodes matching \p name (the default) or all versions
/// of the nodes.
///
/// \sa GetNodeByIdentifier().
NDR_API
NdrNodeConstPtr GetNodeByName(const std::string& name,
const NdrTokenVec& sourceTypePriority = NdrTokenVec(),
NdrVersionFilter filter = NdrVersionFilterDefaultOnly);
/// A convenience wrapper around \c GetNodeByName(). Instead of
/// providing a priority list, an exact type is specified, and
/// `nullptr` is returned if a node with the exact identifier and
/// type does not exist.
///
/// Optionally, a filter can be specified to consider just the default
/// versions of nodes matching \p name (the default) or all versions
/// of the nodes.
NDR_API
NdrNodeConstPtr GetNodeByNameAndType(const std::string& name,
const TfToken& sourceType,
NdrVersionFilter filter =
NdrVersionFilterDefaultOnly);
/// Get all nodes matching the specified identifier (multiple nodes of
/// the same identifier, but different source types, may exist) as well as
/// any nodes which have an alias that matches the identifier. If no nodes
/// match the identifier, an empty vector is returned.
///
/// \sa NdrNodeDiscoveryResult::aliases
NDR_API
NdrNodeConstPtrVec GetNodesByIdentifier(const NdrIdentifier& identifier);
/// Get all nodes matching the specified name. Only nodes matching the
/// specified name will be parsed. Optionally, a filter can be specified
/// to get just the default version (the default) or all versions of the
/// node. If no nodes match an empty vector is returned.
NDR_API
NdrNodeConstPtrVec GetNodesByName(const std::string& name,
NdrVersionFilter filter =
NdrVersionFilterDefaultOnly);
/// Get all nodes from the registry, optionally restricted to the nodes
/// that fall under a specified family and/or the default version.
///
/// Note that this will parse \em all nodes that the registry is aware of
/// (unless a family is specified), so this may take some time to run
/// the first time it is called.
NDR_API
NdrNodeConstPtrVec GetNodesByFamily(const TfToken& family = TfToken(),
NdrVersionFilter filter =
NdrVersionFilterDefaultOnly);
/// Get a sorted list of all node source types that may be present on the
/// nodes in the registry.
///
/// Source types originate from the discovery process, but there is no
/// guarantee that the discovered source types will also have a registered
/// parser plugin. The actual supported source types here depend on the
/// parsers that are available. Also note that some parser plugins may not
/// advertise a source type.
///
/// See the documentation for `NdrParserPlugin` and
/// `NdrNode::GetSourceType()` for more information.
NDR_API
NdrTokenVec GetAllNodeSourceTypes() const;
protected:
NdrRegistry(const NdrRegistry&) = delete;
NdrRegistry& operator=(const NdrRegistry&) = delete;
NDR_API
NdrRegistry();
NDR_API
~NdrRegistry();
private:
class _DiscoveryContext;
friend class _DiscoveryContext;
typedef std::unordered_map<TfToken, NdrParserPlugin*,
TfToken::HashFunctor> TypeToParserPluginMap;
typedef std::pair<NdrIdentifier, TfToken> NodeMapKey;
struct NodeMapKeyHashFunctor {
size_t operator()(const NodeMapKey& x) const {
return NdrIdentifierHashFunctor()(x.first) ^
TfToken::HashFunctor()(x.second);
}
};
typedef std::unordered_multimap<NodeMapKey, NdrNodeUniquePtr,
NodeMapKeyHashFunctor> NodeMap;
// The discovery result vec is not a concurrent data structure, thus it
// needs some locking infrastructure.
mutable std::mutex _discoveryResultMutex;
// The node map is not a concurrent data structure, thus it needs some
// locking infrastructure.
mutable std::mutex _nodeMapMutex;
// Runs each discovery plugin provided and appends the results to the
// internal discovery results vector
void _RunDiscoveryPlugins(const DiscoveryPluginRefPtrVec& discoveryPlugins);
// Finds and instantiates the discovery plugins
void _FindAndInstantiateDiscoveryPlugins();
// Finds and instantiates the parser plugins
void _FindAndInstantiateParserPlugins();
// Instantiates the specified parser plugins and adds them to
// the registry.
void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
// Returns the cached or newly parsed node for the discovery result if its
// identifier matches the given identifier.
NdrNodeConstPtr _ParseNodeMatchingIdentifier(
const NdrNodeDiscoveryResult& dr, const NdrIdentifier& identifier);
// Returns the cached or newly parsed node for the discovery result if it
// has an alias that matches the given identifier.
NdrNodeConstPtr _ParseNodeMatchingAlias(
const NdrNodeDiscoveryResult& dr, const NdrIdentifier& identifier);
// Returns the cached or newly parsed node for the discovery result if its
// name and version match the given name and version filter.
NdrNodeConstPtr _ParseNodeMatchingNameAndFilter(
const NdrNodeDiscoveryResult& dr, const std::string& name,
NdrVersionFilter filter);
// Implementation helper for getting the first node of the given sourceType
// that matches the given indentifier. This includes node that match the
// identifier through an alias.
NdrNodeConstPtr _GetNodeByIdentifierAndTypeImpl(
const NdrIdentifier& identifier, const TfToken& sourceType);
// Implementation helper for getting the first node of the given sourceType
// that matches the given name and version filter.
NdrNodeConstPtr _GetNodeByNameAndTypeImpl(
const std::string& name, const TfToken& sourceType,
NdrVersionFilter filter);
// Inserts a new node into the node cache. If a node with the
// same name and type already exists in the cache, the pointer to the
// existing node will be returned. If there was an error inserting the node,
// `nullptr` will be returned.
NdrNodeConstPtr _InsertNodeIntoCache(const NdrNodeDiscoveryResult& dr);
// Get a vector of all of the node unique_ptrs in the node map as raw ptrs
NdrNodeConstPtrVec _GetNodeMapAsNodePtrVec(const TfToken& family,
NdrVersionFilter filter) const;
// Return the parser plugin for a discovery type. Returns null if no parser
// plugin has that discovery type.
NdrParserPlugin*
_GetParserForDiscoveryType(const TfToken& discoveryType) const;
// The discovery plugins that were found through libplug and/or provided by
// the client
DiscoveryPluginRefPtrVec _discoveryPlugins;
// The parser plugins that have been discovered via the plugin system. Maps
// a discovery result's "discovery type" to a specific parser.
TypeToParserPluginMap _parserPluginMap;
// The parser plugins. This has ownership of the plugin objects.
std::vector<std::unique_ptr<NdrParserPlugin>> _parserPlugins;
// The preliminary discovery results prior to parsing. If accessing or
// mutating, _discoveryResultMutex should be used.
NdrNodeDiscoveryResultVec _discoveryResults;
// Additional mapping of discovery results by grouped source type to aid in
// getting nodes by type priority. Stored as indices into the
// _disoveryResults vector.
std::map<TfToken, std::vector<size_t>> _discoveryResultIndicesBySourceType;
// Maps a node's name to a node instance. If accessing or mutating,
// _nodeMapMutex should be used.
NodeMap _nodeMap;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_NDR_REGISTRY_H