HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
input.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_INPUT_H
8 #define PXR_USD_USD_SHADE_INPUT_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usdShade/api.h"
12 #include "pxr/usd/usdShade/types.h"
13 #include "pxr/usd/usdShade/utils.h"
14 #include "pxr/usd/usd/attribute.h"
15 
16 #include "pxr/usd/sdr/declare.h"
17 
18 #include <vector>
19 
21 
24 class UsdShadeOutput;
25 
26 /// \class UsdShadeInput
27 ///
28 /// This class encapsulates a shader or node-graph input, which is a
29 /// connectable attribute representing a typed value.
30 ///
32 {
33 public:
34  /// Default constructor returns an invalid Input. Exists for the sake of
35  /// container classes
37  {
38  // nothing
39  }
40 
41  /// Get the name of the attribute associated with the Input.
42  ///
43  TfToken const &GetFullName() const {
44  return _attr.GetName();
45  }
46 
47  /// Returns the name of the input.
48  ///
49  /// We call this the base name since it strips off the "inputs:" namespace
50  /// prefix from the attribute name, and returns it.
51  ///
53  TfToken GetBaseName() const;
54 
55  /// Get the "scene description" value type name of the attribute associated
56  /// with the Input.
57  ///
60 
61  /// Get the prim that the input belongs to.
62  UsdPrim GetPrim() const {
63  return _attr.GetPrim();
64  }
65 
66  /// Convenience wrapper for the templated UsdAttribute::Get().
67  template <typename T>
69  return GetAttr().Get(value, time);
70  }
71 
72  /// Convenience wrapper for VtValue version of UsdAttribute::Get().
74  bool Get(VtValue* value, UsdTimeCode time = UsdTimeCode::Default()) const;
75 
76  /// Set a value for the Input at \p time.
77  ///
79  bool Set(const VtValue& value,
81 
82  /// \overload
83  /// Set a value of the Input at \p time.
84  ///
85  template <typename T>
86  bool Set(const T& value, UsdTimeCode time = UsdTimeCode::Default()) const {
87  return _attr.Set(value, time);
88  }
89 
90  /// Hash functor.
91  struct Hash {
92  inline size_t operator()(const UsdShadeInput &input) const {
93  return hash_value(input._attr);
94  }
95  };
96 
97  /// \name Configuring the Input's Type
98  /// @{
99 
100  /// Specify an alternative, renderer-specific type to use when
101  /// emitting/translating this Input, rather than translating based
102  /// on its GetTypeName()
103  ///
104  /// For example, we set the renderType to "struct" for Inputs that
105  /// are of renderman custom struct types.
106  ///
107  /// \return true on success.
108  ///
110  bool SetRenderType(TfToken const& renderType) const;
111 
112  /// Return this Input's specialized renderType, or an empty
113  /// token if none was authored.
114  ///
115  /// \sa SetRenderType()
117  TfToken GetRenderType() const;
118 
119  /// Return true if a renderType has been specified for this Input.
120  ///
121  /// \sa SetRenderType()
123  bool HasRenderType() const;
124 
125  /// @}
126 
127  /// \name API to author and query an Input's sdrMetadata
128  ///
129  /// This section provides API for authoring and querying shader registry
130  /// metadata on an Input. When the owning shader prim is providing a shader
131  /// definition, the authored "sdrMetadata" dictionary value provides
132  /// metadata needed to populate the Input correctly in the shader registry.
133  ///
134  /// We expect the keys in sdrMetadata to correspond to the keys
135  /// in \ref SdrPropertyMetadata. However, this is not strictly enforced by
136  /// the API. The only allowed value type in the "sdrMetadata" dictionary is
137  /// a std::string since it needs to be converted into a SdrTokenMap, which
138  /// Sdr will parse using the utilities available in \ref SdrMetadataHelpers.
139  ///
140  /// @{
141 
142  /// Returns this Input's composed "sdrMetadata" dictionary as a
143  /// SdrTokenMap.
145  SdrTokenMap GetSdrMetadata() const;
146 
147  /// Returns the value corresponding to \p key in the composed
148  /// <b>sdrMetadata</b> dictionary.
150  std::string GetSdrMetadataByKey(const TfToken &key) const;
151 
152  /// Authors the given \p sdrMetadata value on this Input at the current
153  /// EditTarget.
155  void SetSdrMetadata(const SdrTokenMap &sdrMetadata) const;
156 
157  /// Sets the value corresponding to \p key to the given string \p value, in
158  /// the Input's "sdrMetadata" dictionary at the current EditTarget.
160  void SetSdrMetadataByKey(
161  const TfToken &key,
162  const std::string &value) const;
163 
164  /// Returns true if the Input has a non-empty composed "sdrMetadata"
165  /// dictionary value.
167  bool HasSdrMetadata() const;
168 
169  /// Returns true if there is a value corresponding to the given \p key in
170  /// the composed "sdrMetadata" dictionary.
172  bool HasSdrMetadataByKey(const TfToken &key) const;
173 
174  /// Clears any "sdrMetadata" value authored on the Input in the current
175  /// EditTarget.
177  void ClearSdrMetadata() const;
178 
179  /// Clears the entry corresponding to the given \p key in the
180  /// "sdrMetadata" dictionary authored in the current EditTarget.
182  void ClearSdrMetadataByKey(const TfToken &key) const;
183 
184  /// @}
185 
186  // ---------------------------------------------------------------
187  /// \name UsdAttribute API
188  // ---------------------------------------------------------------
189 
190  /// @{
191 
192  /// Speculative constructor that will produce a valid UsdShadeInput when
193  /// \p attr already represents a shade Input, and produces an \em invalid
194  /// UsdShadeInput otherwise (i.e. the explicit bool conversion operator will
195  /// return false).
197  explicit UsdShadeInput(const UsdAttribute &attr);
198 
199  /// Test whether a given UsdAttribute represents a valid Input, which
200  /// implies that creating a UsdShadeInput from the attribute will succeed.
201  ///
202  /// Success implies that \c attr.IsDefined() is true.
204  static bool IsInput(const UsdAttribute &attr);
205 
206  /// Test if this name has a namespace that indicates it could be an
207  /// input.
209  static bool IsInterfaceInputName(const std::string & name);
210 
211  /// Explicit UsdAttribute extractor.
212  const UsdAttribute &GetAttr() const { return _attr; }
213 
214  /// Allow UsdShadeInput to auto-convert to UsdAttribute, so you can
215  /// pass a UsdShadeInput to any function that accepts a UsdAttribute or
216  /// const-ref thereto.
217  operator const UsdAttribute & () const { return GetAttr(); }
218 
219  /// Return true if the wrapped UsdAttribute is defined, and in addition the
220  /// attribute is identified as an input.
221  bool IsDefined() const {
222  return _attr && IsInput(_attr);
223  }
224 
225  /// Set documentation string for this Input.
226  /// \sa UsdObject::SetDocumentation()
228  bool SetDocumentation(const std::string& docs) const;
229 
230  /// Get documentation string for this Input.
231  /// \sa UsdObject::GetDocumentation()
233  std::string GetDocumentation() const;
234 
235  /// Set the displayGroup metadata for this Input, i.e. hinting for the
236  /// location and nesting of the attribute.
237  ///
238  /// Note for an input representing a nested SdrShaderProperty, its expected
239  /// to have the scope delimited by a ":".
240  /// \sa UsdProperty::SetDisplayGroup(), UsdProperty::SetNestedDisplayGroup()
241  /// \sa SdrShaderProperty::GetPage()
243  bool SetDisplayGroup(const std::string& displayGroup) const;
244 
245  /// Get the displayGroup metadata for this Input, i.e. hint for the location
246  /// and nesting of the attribute.
247  /// \sa UsdProperty::GetDisplayGroup(), UsdProperty::GetNestedDisplayGroup()
249  std::string GetDisplayGroup() const;
250 
251  /// @}
252 
253  /// Return true if this Input is valid for querying and authoring
254  /// values and metadata, which is identically equivalent to IsDefined().
255  explicit operator bool() const {
256  return IsDefined();
257  }
258 
259  /// Equality comparison. Returns true if \a lhs and \a rhs represent the
260  /// same UsdShadeInput, false otherwise.
261  friend bool operator==(const UsdShadeInput &lhs, const UsdShadeInput &rhs) {
262  return lhs.GetAttr() == rhs.GetAttr();
263  }
264 
265  /// Inequality comparison. Return false if \a lhs and \a rhs represent the
266  /// same UsdShadeInput, true otherwise.
267  friend bool operator!=(const UsdShadeInput &lhs, const UsdShadeInput &rhs) {
268  return !(lhs == rhs);
269  }
270 
271  // -------------------------------------------------------------------------
272  /// \name Connections API
273  // -------------------------------------------------------------------------
274  /// @{
275 
276  /// Determines whether this Input can be connected to the given
277  /// source attribute, which can be an input or an output.
278  ///
279  /// \sa UsdShadeConnectableAPI::CanConnect
281  bool CanConnect(const UsdAttribute &source) const;
282 
283  /// \overload
285  bool CanConnect(const UsdShadeInput &sourceInput) const;
286 
287  /// \overload
289  bool CanConnect(const UsdShadeOutput &sourceOutput) const;
290 
292 
293  /// Authors a connection for this Input
294  ///
295  /// \p source is a struct that describes the upstream source attribute
296  /// with all the information necessary to make a connection. See the
297  /// documentation for UsdShadeConnectionSourceInfo.
298  /// \p mod describes the operation that should be applied to the list of
299  /// connections. By default the new connection will replace any existing
300  /// connections, but it can add to the list of connections to represent
301  /// multiple input connections.
302  ///
303  /// \return
304  /// \c true if a connection was created successfully.
305  /// \c false if this input or \p source is invalid.
306  ///
307  /// \note This method does not verify the connectability of the shading
308  /// attribute to the source. Clients must invoke CanConnect() themselves
309  /// to ensure compatibility.
310  /// \note The source shading attribute is created if it doesn't exist
311  /// already.
312  ///
313  /// \sa UsdShadeConnectableAPI::ConnectToSource
314  ///
316  bool ConnectToSource(
320 
321  /// \deprecated
322  /// \overload
324  bool ConnectToSource(
326  TfToken const &sourceName,
328  SdfValueTypeName typeName=SdfValueTypeName()) const;
329 
330  /// Authors a connection for this Input to the source at the given path.
331  ///
332  /// \sa UsdShadeConnectableAPI::ConnectToSource
333  ///
335  bool ConnectToSource(SdfPath const &sourcePath) const;
336 
337  /// Connects this Input to the given input, \p sourceInput.
338  ///
339  /// \sa UsdShadeConnectableAPI::ConnectToSource
340  ///
342  bool ConnectToSource(UsdShadeInput const &sourceInput) const;
343 
344  /// Connects this Input to the given output, \p sourceOutput.
345  ///
346  /// \sa UsdShadeConnectableAPI::ConnectToSource
347  ///
349  bool ConnectToSource(UsdShadeOutput const &sourceOutput) const;
350 
351  /// Connects this Input to the given sources, \p sourceInfos
352  ///
353  /// \sa UsdShadeConnectableAPI::SetConnectedSources
354  ///
356  bool SetConnectedSources(
357  std::vector<UsdShadeConnectionSourceInfo> const &sourceInfos) const;
358 
360 
361  /// Finds the valid sources of connections for the Input.
362  ///
363  /// \p invalidSourcePaths is an optional output parameter to collect the
364  /// invalid source paths that have not been reported in the returned vector.
365  ///
366  /// Returns a vector of \p UsdShadeConnectionSourceInfo structs with
367  /// information about each upsteam attribute. If the vector is empty, there
368  /// have been no valid connections.
369  ///
370  /// \note A valid connection requires the existence of the source attribute
371  /// and also requires that the source prim is UsdShadeConnectableAPI
372  /// compatible.
373  /// \note The python wrapping returns a tuple with the valid connections
374  /// first, followed by the invalid source paths.
375  ///
376  /// \sa UsdShadeConnectableAPI::GetConnectedSources
377  ///
380  SdfPathVector *invalidSourcePaths = nullptr) const;
381 
382  /// \deprecated
385  TfToken *sourceName,
386  UsdShadeAttributeType *sourceType) const;
387 
388  /// \deprecated
389  /// Returns the "raw" (authored) connected source paths for this Input.
390  ///
391  /// \sa UsdShadeConnectableAPI::GetRawConnectedSourcePaths
392  ///
394  bool GetRawConnectedSourcePaths(SdfPathVector *sourcePaths) const;
395 
396  /// Returns true if and only if this Input is currently connected to a
397  /// valid (defined) source.
398  ///
399  /// \sa UsdShadeConnectableAPI::HasConnectedSource
400  ///
402  bool HasConnectedSource() const;
403 
404  /// Returns true if the connection to this Input's source, as returned by
405  /// GetConnectedSource(), is authored across a specializes arc, which is
406  /// used to denote a base material.
407  ///
408  /// \sa UsdShadeConnectableAPI::IsSourceConnectionFromBaseMaterial
409  ///
412 
413  /// Disconnect source for this Input. If \p sourceAttr is valid, only a
414  /// connection to the specified attribute is disconnected, otherwise all
415  /// connections are removed.
416  ///
417  /// \sa UsdShadeConnectableAPI::DisconnectSource
418  ///
420  bool DisconnectSource(UsdAttribute const &sourceAttr = UsdAttribute()) const;
421 
422  /// Clears sources for this Input in the current UsdEditTarget.
423  ///
424  /// Most of the time, what you probably want is DisconnectSource()
425  /// rather than this function.
426  ///
427  /// \sa UsdShadeConnectableAPI::ClearSources
428  ///
430  bool ClearSources() const;
431 
432  /// \deprecated
434  bool ClearSource() const;
435 
436  /// @}
437 
438  // -------------------------------------------------------------------------
439  /// \name Connectability API
440  // -------------------------------------------------------------------------
441  /// @{
442 
443  /// \brief Set the connectability of the Input.
444  ///
445  /// In certain shading data models, there is a need to distinguish which
446  /// inputs <b>can</b> vary over a surface from those that must be
447  /// <b>uniform</b>. This is accomplished in UsdShade by limiting the
448  /// connectability of the input. This is done by setting the
449  /// "connectability" metadata on the associated attribute.
450  ///
451  /// Connectability of an Input can be set to UsdShadeTokens->full or
452  /// UsdShadeTokens->interfaceOnly.
453  ///
454  /// \li <b>full</b> implies that the Input can be connected to any other
455  /// Input or Output.
456  /// \li <b>interfaceOnly</b> implies that the Input can only be connected to
457  /// a NodeGraph Input (which represents an interface override, not a
458  /// render-time dataflow connection), or another Input whose connectability
459  /// is also "interfaceOnly".
460  ///
461  /// The default connectability of an input is UsdShadeTokens->full.
462  ///
463  /// \sa SetConnectability()
465  bool SetConnectability(const TfToken &connectability) const;
466 
467  /// \brief Returns the connectability of the Input.
468  ///
469  /// \sa SetConnectability()
471  TfToken GetConnectability() const;
472 
473  /// \brief Clears any authored connectability on the Input.
474  ///
476  bool ClearConnectability() const;
477 
478  /// @}
479 
480  // -------------------------------------------------------------------------
481  /// \name Connected Value API
482  // -------------------------------------------------------------------------
483  /// @{
484 
485  /// \brief Find what is connected to this Input recursively
486  ///
487  /// \sa UsdShadeUtils::GetValueProducingAttributes
490  bool shaderOutputsOnly = false) const;
491 
492  /// \deprecated in favor of calling GetValueProducingAttributes
495  UsdShadeAttributeType* attrType) const;
496 
497  /// @}
498 
499 private:
501 
502  // Constructor that creates a UsdShadeInput with the given name on the
503  // given prim.
504  // \p name here is the unnamespaced name of the input.
505  UsdShadeInput(UsdPrim prim,
506  TfToken const &name,
507  SdfValueTypeName const &typeName);
508 
509  UsdAttribute _attr;
510 };
511 
513 
514 #endif // PXR_USD_USD_SHADE_INPUT_H
bool IsDefined() const
Definition: input.h:221
#define USDSHADE_API
Definition: api.h:23
Hash functor.
Definition: input.h:91
USDSHADE_API std::string GetDisplayGroup() const
USDSHADE_API void ClearSdrMetadata() const
USDSHADE_API void SetSdrMetadataByKey(const TfToken &key, const std::string &value) const
USDSHADE_API bool GetRawConnectedSourcePaths(SdfPathVector *sourcePaths) const
static constexpr UsdTimeCode Default()
Definition: timeCode.h:113
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: attribute.h:452
USDSHADE_API bool IsSourceConnectionFromBaseMaterial() const
T mod(T x, int y)
Definition: chrono.h:1648
static USDSHADE_API bool IsInput(const UsdAttribute &attr)
USDSHADE_API bool CanConnect(const UsdAttribute &source) const
GT_API const UT_StringHolder time
USDSHADE_API bool GetConnectedSource(UsdShadeConnectableAPI *source, TfToken *sourceName, UsdShadeAttributeType *sourceType) const
GLsizei const GLfloat * value
Definition: glcorearb.h:824
USDSHADE_API bool ClearSource() const
USDSHADE_API std::string GetSdrMetadataByKey(const TfToken &key) const
UsdShadeConnectionModification
Definition: types.h:36
USDSHADE_API bool HasSdrMetadataByKey(const TfToken &key) const
USDSHADE_API bool Set(const VtValue &value, UsdTimeCode time=UsdTimeCode::Default()) const
OutGridT const XformOp bool bool
static USDSHADE_API bool IsInterfaceInputName(const std::string &name)
Definition: token.h:70
USDSHADE_API void SetSdrMetadata(const SdrTokenMap &sdrMetadata) const
friend bool operator==(const UsdShadeInput &lhs, const UsdShadeInput &rhs)
Definition: input.h:261
friend bool operator!=(const UsdShadeInput &lhs, const UsdShadeInput &rhs)
Definition: input.h:267
USDSHADE_API bool HasRenderType() const
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Convenience wrapper for the templated UsdAttribute::Get().
Definition: input.h:68
std::vector< class SdfPath > SdfPathVector
UsdPrim GetPrim() const
Get the prim that the input belongs to.
Definition: input.h:62
USDSHADE_API bool HasSdrMetadata() const
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
USDSHADE_API SdrTokenMap GetSdrMetadata() const
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: input.h:86
TfToken const & GetFullName() const
Definition: input.h:43
Definition: prim.h:116
USDSHADE_API TfToken GetBaseName() const
GLuint const GLchar * name
Definition: glcorearb.h:786
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: attribute.h:495
Definition: path.h:273
USDSHADE_API bool ClearConnectability() const
Clears any authored connectability on the Input.
USDSHADE_API bool HasConnectedSource() const
USDSHADE_API bool SetConnectedSources(std::vector< UsdShadeConnectionSourceInfo > const &sourceInfos) const
const TfToken & GetName() const
Definition: object.h:221
USDSHADE_API TfToken GetConnectability() const
Returns the connectability of the Input.
UsdShadeInput()
Definition: input.h:36
UsdPrim GetPrim() const
Definition: prim.h:2787
USDSHADE_API TfToken GetRenderType() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USDSHADE_API bool DisconnectSource(UsdAttribute const &sourceAttr=UsdAttribute()) const
USDSHADE_API bool ClearSources() const
size_t operator()(const UsdShadeInput &input) const
Definition: input.h:92
USDSHADE_API UsdShadeAttributeVector GetValueProducingAttributes(bool shaderOutputsOnly=false) const
Find what is connected to this Input recursively.
USDSHADE_API bool SetRenderType(TfToken const &renderType) const
USDSHADE_API SdfValueTypeName GetTypeName() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const UsdAttribute & GetAttr() const
Explicit UsdAttribute extractor.
Definition: input.h:212
USDSHADE_API std::string GetDocumentation() const
USDSHADE_API bool SetDisplayGroup(const std::string &displayGroup) const
USDSHADE_API UsdAttribute GetValueProducingAttribute(UsdShadeAttributeType *attrType) const
USDSHADE_API void ClearSdrMetadataByKey(const TfToken &key) const
USDSHADE_API bool ConnectToSource(UsdShadeConnectionSourceInfo const &source, ConnectionModification const mod=ConnectionModification::Replace) const
size_t hash_value(const CH_ChannelRef &ref)
USDSHADE_API bool SetConnectability(const TfToken &connectability) const
Set the connectability of the Input.
Definition: value.h:146
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:47
USDSHADE_API bool SetDocumentation(const std::string &docs) const
UsdShadeAttributeType
Definition: types.h:22
USDSHADE_API SourceInfoVector GetConnectedSources(SdfPathVector *invalidSourcePaths=nullptr) const