HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
connectableAPIBehavior.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_USD_SHADE_CONNECTABLE_BEHAVIOR_H
8 #define PXR_USD_USD_SHADE_CONNECTABLE_BEHAVIOR_H
9 
10 /// \file usdShade/connectableAPIBehavior.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usdShade/api.h"
14 
15 #include "pxr/base/gf/vec3f.h"
16 #include "pxr/base/vt/array.h"
17 
18 #include "pxr/base/tf/type.h"
19 
21 
22 class UsdAttribute;
23 class UsdShadeInput;
24 class UsdShadeOutput;
25 
26 /// UsdShadeConnectableAPIBehavior defines the compatibilty and behavior
27 /// UsdShadeConnectableAPIof when applied to a particular prim type.
28 ///
29 /// This enables schema libraries to enable UsdShadeConnectableAPI for
30 /// their prim types and define its behavior.
32 {
33 public:
34 
35  /// An enum describing the types of connectable nodes which will govern what
36  /// connectibility rule is invoked for these.
38  {
39  BasicNodes, // Shader, NodeGraph
40  DerivedContainerNodes, // Material, etc
41  };
42 
43  // By default we want a connectableBehavior to not exhibit a container like
44  // behavior. And we want encapsulation behavior enabled by default.
47  : _isContainer(false), _requiresEncapsulation(true) {}
48 
50  UsdShadeConnectableAPIBehavior(const bool isContainer,
51  const bool requiresEncapsulation)
52  : _isContainer(isContainer),
53  _requiresEncapsulation(requiresEncapsulation) {}
54 
57 
58  /// The prim owning the input is guaranteed to be of the type this
59  /// behavior was registered with. The function must be thread-safe.
60  ///
61  /// It should return true if the connection is allowed, false
62  /// otherwise. If the connection is prohibited and \p reason is
63  /// non-NULL, it should be set to a user-facing description of the
64  /// reason the connection is prohibited.
65  ///
66  /// The base implementation checks that the input is defined; that
67  /// the source attribute exists; and that the connectability metadata
68  /// on the input allows a connection from the attribute -- see
69  /// UsdShadeInput::GetConnectability().
70  ///
72  virtual bool
74  const UsdAttribute &,
75  std::string *reason) const;
76 
77  /// The prim owning the output is guaranteed to be of the type this
78  /// behavior was registered with. The function must be thread-safe.
79  ///
80  /// It should return true if the connection is allowed, false
81  /// otherwise. If the connection is prohibited and \p reason is
82  /// non-NULL, it should be set to a user-facing description of the
83  /// reason the connection is prohibited.
84  ///
85  /// The base implementation returns false. Outputs of most prim
86  /// types will be defined by the underlying node definition (see
87  /// UsdShadeNodeDefAPI), not a connection.
88  ///
90  virtual bool
92  const UsdAttribute &,
93  std::string *reason) const;
94 
95  /// The function must be thread-safe.
96  ///
97  /// It should return true if the associated prim type is considered
98  /// a "container" for connected nodes.
99  /// Returns the value set for _isContainer.
101  virtual bool
102  IsContainer() const final;
103 
104  /// The function must be thread-safe.
105  ///
106  /// Determines if the behavior should respect container encapsulation rules
107  /// (\ref UsdShadeConnectability), when evaluating CanConnectInputToSource
108  /// or CanConnectOutputToSource. This should return true if the container
109  /// encapsulation rules need to be respected, false otherwise.
110  //
111  /// Returns the value set for _requiresEncapsulation.
112  ///
113  /// \sa IsContainer()
114  ///
116  virtual bool
117  RequiresEncapsulation() const final;
118 
119 protected:
120  /// Helper function to separate and share special connectivity logic for
121  /// specialized, NodeGraph-derived nodes, like Material (and other in other
122  /// domains) that allow their inputs to be connected to an output of a
123  /// source that they directly contain/encapsulate. The default behavior is
124  /// for Shader Nodes or NodeGraphs which allow their input connections to
125  /// output of a sibling source, both encapsulated by the same container
126  /// node.
129  std::string *reason,
130  ConnectableNodeTypes nodeType =
132 
134  bool _CanConnectOutputToSource(const UsdShadeOutput&, const UsdAttribute&,
135  std::string *reason,
136  ConnectableNodeTypes nodeType =
138 private:
139  bool _isContainer;
140  bool _requiresEncapsulation;
141 };
142 
143 /// Registers \p behavior to define connectability of attributes for \p PrimType.
144 ///
145 /// Plugins should call this function in a TF_REGISTRY_FUNCTION. For example:
146 ///
147 /// \code
148 /// class MyBehavior : public UsdShadeConnectableAPIBehavior { ... }
149 ///
150 /// TF_REGISTRY_FUNCTION(UsdShadeConnectableAPI)
151 /// {
152 /// UsdShadeRegisterConnectableAPIBehavior<MyPrim, MyBehavior>();
153 /// }
154 /// \endcode
155 ///
156 /// Plugins must also note that UsdShadeConnectableAPI behavior is implemented
157 /// for a prim type in that type's schema definnition. For example:
158 ///
159 /// \code
160 /// class "MyPrim" (
161 /// ...
162 /// customData = {
163 /// dictionary extraPlugInfo = {
164 /// bool providesUsdShadeConnectableAPIBehavior = true
165 /// }
166 /// }
167 /// ...
168 /// )
169 /// { ... }
170 /// \endcode
171 ///
172 /// This allows the plugin system to discover this behavior dynamically
173 /// and load the plugin if needed.
174 ///
175 /// In addition to Typed schemas, single apply API schemas can also include
176 /// **providesUsdShadeConnectableAPIBehavior** in their **extraPlugInfo** and
177 /// hence impart connectableAPIBehavior to the prim definition in which they
178 /// are participating. Additionally a schema can include metadata in their
179 /// extraPlugInfo fields to override isContainer and requiresEncapsulation
180 /// properties by specifying bool values for **isUsdShadeContainer** and
181 /// **requiresUsdShadeEncapsulation**. This can be especially useful for
182 /// \ref codeless_schema that cannot provide a C++ derivation of
183 /// UsdShadeConnectableAPIBehavior.
184 ///
185 /// \ref UsdShadeConnectableAPIBehavior_ResolutionOrder defines the
186 /// resolution order when multiple types and apiSchemas provide a
187 /// UsdShadeConnectableAPIBehavior.
188 ///
189 template <class PrimType, class BehaviorType = UsdShadeConnectableAPIBehavior>
190 inline void
192 {
194  TfType::Find<PrimType>(),
195  std::shared_ptr<UsdShadeConnectableAPIBehavior>(new BehaviorType));
196 }
197 
198 /// Registers \p behavior to define connectability of attributes for
199 /// \p PrimType.
201 void
203  const TfType& connectablePrimType,
204  const std::shared_ptr<UsdShadeConnectableAPIBehavior>& behavior);
205 
207 
208 #endif // PXR_USD_USD_SHADE_CONNECTABLE_BEHAVIOR_H
void UsdShadeRegisterConnectableAPIBehavior()
#define USDSHADE_API
Definition: api.h:23
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
USDSHADE_API bool _CanConnectInputToSource(const UsdShadeInput &, const UsdAttribute &, std::string *reason, ConnectableNodeTypes nodeType=ConnectableNodeTypes::BasicNodes) const
virtual USDSHADE_API bool CanConnectInputToSource(const UsdShadeInput &, const UsdAttribute &, std::string *reason) const
virtual USDSHADE_API bool RequiresEncapsulation() const final
virtual USDSHADE_API bool CanConnectOutputToSource(const UsdShadeOutput &, const UsdAttribute &, std::string *reason) const
virtual USDSHADE_API ~UsdShadeConnectableAPIBehavior()
USDSHADE_API bool _CanConnectOutputToSource(const UsdShadeOutput &, const UsdAttribute &, std::string *reason, ConnectableNodeTypes nodeType=ConnectableNodeTypes::BasicNodes) const
virtual USDSHADE_API bool IsContainer() const final
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
USDSHADE_API UsdShadeConnectableAPIBehavior(const bool isContainer, const bool requiresEncapsulation)