HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
output.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_OUTPUT_H
8 #define PXR_USD_USD_SHADE_OUTPUT_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 #include "pxr/usd/sdr/declare.h"
16 
17 #include <vector>
18 
20 
23 class UsdShadeInput;
24 
25 /// \class UsdShadeOutput
26 ///
27 /// This class encapsulates a shader or node-graph output, which is a
28 /// connectable attribute representing a typed, externally computed value.
29 ///
31 {
32 public:
33  /// Default constructor returns an invalid Output. Exists for
34  /// container classes
36  {
37  // nothing
38  }
39 
40  /// Get the name of the attribute associated with the output.
41  ///
42  TfToken const &GetFullName() const {
43  return _attr.GetName();
44  }
45 
46  /// Returns the name of the output.
47  ///
48  /// We call this the base name since it strips off the "outputs:" namespace
49  /// prefix from the attribute name, and returns it.
50  ///
52  TfToken GetBaseName() const;
53 
54  /// Get the prim that the output belongs to.
55  UsdPrim GetPrim() const {
56  return _attr.GetPrim();
57  }
58 
59  /// Get the "scene description" value type name of the attribute associated
60  /// with the output.
61  ///
64 
65  /// Set a value for the output.
66  ///
67  /// It's unusual to be setting a value on an output since it represents
68  /// an externally computed value. The Set API is provided here just for the
69  /// sake of completeness and uniformity with other property schema.
70  ///
72  bool Set(const VtValue& value,
74 
75  /// \overload
76  /// Set the attribute value of the Output at \p time
77  ///
78  template <typename T>
79  bool Set(const T& value, UsdTimeCode time = UsdTimeCode::Default()) const {
80  if (UsdAttribute attr = GetAttr()) {
81  return attr.Set(value, time);
82  }
83  return false;
84  }
85 
86  /// \name Configuring the Output's Type
87  /// @{
88 
89  /// Specify an alternative, renderer-specific type to use when
90  /// emitting/translating this output, rather than translating based
91  /// on its GetTypeName()
92  ///
93  /// For example, we set the renderType to "struct" for outputs that
94  /// are of renderman custom struct types.
95  ///
96  /// \return true on success
98  bool SetRenderType(TfToken const& renderType) const;
99 
100  /// Return this output's specialized renderType, or an empty
101  /// token if none was authored.
102  ///
103  /// \sa SetRenderType()
105  TfToken GetRenderType() const;
106 
107  /// Return true if a renderType has been specified for this
108  /// output.
109  ///
110  /// \sa SetRenderType()
112  bool HasRenderType() const;
113 
114  /// @}
115 
116  /// \name API to author and query an Output's sdrMetadata
117  ///
118  /// This section provides API for authoring and querying shader registry
119  /// metadata on an Output. When the owning shader prim is providing a shader
120  /// definition, the authored "sdrMetadata" dictionary value provides
121  /// metadata needed to populate the Output correctly in the shader registry.
122  ///
123  /// We expect the keys in sdrMetadata to correspond to the keys
124  /// in \ref SdrPropertyMetadata. However, this is not strictly enforced by
125  /// the API. The only allowed value type in the "sdrMetadata" dictionary is
126  /// a std::string since it needs to be converted into a SdrTokenMap, which
127  /// Sdr will parse using the utilities available in \ref SdrMetadataHelpers.
128  ///
129  /// @{
130 
131  /// Returns this Output's composed "sdrMetadata" dictionary as a
132  /// SdrTokenMap.
134  SdrTokenMap GetSdrMetadata() const;
135 
136  /// Returns the value corresponding to \p key in the composed
137  /// <b>sdrMetadata</b> dictionary.
139  std::string GetSdrMetadataByKey(const TfToken &key) const;
140 
141  /// Authors the given \p sdrMetadata value on this Output at the current
142  /// EditTarget.
144  void SetSdrMetadata(const SdrTokenMap &sdrMetadata) const;
145 
146  /// Sets the value corresponding to \p key to the given string \p value, in
147  /// the Output's "sdrMetadata" dictionary at the current EditTarget.
149  void SetSdrMetadataByKey(
150  const TfToken &key,
151  const std::string &value) const;
152 
153  /// Returns true if the Output has a non-empty composed "sdrMetadata"
154  /// dictionary value.
156  bool HasSdrMetadata() const;
157 
158  /// Returns true if there is a value corresponding to the given \p key in
159  /// the composed "sdrMetadata" dictionary.
161  bool HasSdrMetadataByKey(const TfToken &key) const;
162 
163  /// Clears any "sdrMetadata" value authored on the Output in the current
164  /// EditTarget.
166  void ClearSdrMetadata() const;
167 
168  /// Clears the entry corresponding to the given \p key in the
169  /// "sdrMetadata" dictionary authored in the current EditTarget.
171  void ClearSdrMetadataByKey(const TfToken &key) const;
172 
173  /// @}
174 
175  // ---------------------------------------------------------------
176  /// \name UsdAttribute API
177  // ---------------------------------------------------------------
178 
179  /// @{
180 
181  /// Speculative constructor that will produce a valid UsdShadeOutput when
182  /// \p attr already represents a shade Output, and produces an \em invalid
183  /// UsdShadeOutput otherwise (i.e. the explicit bool conversion operator
184  /// will return false).
186  explicit UsdShadeOutput(const UsdAttribute &attr);
187 
188  /// Test whether a given UsdAttribute represents a valid Output, which
189  /// implies that creating a UsdShadeOutput from the attribute will succeed.
190  ///
191  /// Success implies that \c attr.IsDefined() is true.
193  static bool IsOutput(const UsdAttribute &attr);
194 
195  /// Explicit UsdAttribute extractor.
196  const UsdAttribute &GetAttr() const { return _attr; }
197 
198  /// Allow UsdShadeOutput to auto-convert to UsdAttribute, so you can
199  /// pass a UsdShadeOutput to any function that accepts a UsdAttribute or
200  /// const-ref thereto.
201  operator UsdAttribute () const { return GetAttr(); }
202 
203  /// Return true if the wrapped UsdAttribute is defined, and in
204  /// addition the attribute is identified as an output.
205  ///
206  bool IsDefined() const {
207  return IsOutput(_attr);
208  }
209 
210  /// @}
211 
212  // -------------------------------------------------------------------------
213  /// \name Connections API
214  // -------------------------------------------------------------------------
215  /// @{
216 
217  /// Determines whether this Output can be connected to the given
218  /// source attribute, which can be an input or an output.
219  ///
220  /// An output is considered to be connectable only if it belongs to a
221  /// node-graph. Shader outputs are not connectable.
222  ///
223  /// \sa UsdShadeConnectableAPI::CanConnect
225  bool CanConnect(const UsdAttribute &source) const;
226 
227  /// \overload
229  bool CanConnect(const UsdShadeInput &sourceInput) const;
230 
231  /// \overload
233  bool CanConnect(const UsdShadeOutput &sourceOutput) const;
234 
236 
237  /// Authors a connection for this Output
238  ///
239  /// \p source is a struct that describes the upstream source attribute
240  /// with all the information necessary to make a connection. See the
241  /// documentation for UsdShadeConnectionSourceInfo.
242  /// \p mod describes the operation that should be applied to the list of
243  /// connections. By default the new connection will replace any existing
244  /// connections, but it can add to the list of connections to represent
245  /// multiple input connections.
246  ///
247  /// \return
248  /// \c true if a connection was created successfully.
249  /// \c false if \p shadingAttr or \p source is invalid.
250  ///
251  /// \note This method does not verify the connectability of the shading
252  /// attribute to the source. Clients must invoke CanConnect() themselves
253  /// to ensure compatibility.
254  /// \note The source shading attribute is created if it doesn't exist
255  /// already.
256  ///
257  /// \sa UsdShadeConnectableAPI::ConnectToSource
258  ///
260  bool ConnectToSource(
264 
265  /// \deprecated
266  /// \overload
268  bool ConnectToSource(
270  TfToken const &sourceName,
272  SdfValueTypeName typeName=SdfValueTypeName()) const;
273 
274  /// Authors a connection for this Output to the source at the given path.
275  ///
276  /// \sa UsdShadeConnectableAPI::ConnectToSource
277  ///
279  bool ConnectToSource(SdfPath const &sourcePath) const;
280 
281  /// Connects this Output to the given input, \p sourceInput.
282  ///
283  /// \sa UsdShadeConnectableAPI::ConnectToSource
284  ///
286  bool ConnectToSource(UsdShadeInput const &sourceInput) const;
287 
288  /// Connects this Output to the given output, \p sourceOutput.
289  ///
290  /// \sa UsdShadeConnectableAPI::ConnectToSource
291  ///
293  bool ConnectToSource(UsdShadeOutput const &sourceOutput) const;
294 
295  /// Connects this Output to the given sources, \p sourceInfos
296  ///
297  /// \sa UsdShadeConnectableAPI::SetConnectedSources
298  ///
300  bool SetConnectedSources(
301  std::vector<UsdShadeConnectionSourceInfo> const &sourceInfos) const;
302 
303  // XXX move to new header
305 
306  /// Finds the valid sources of connections for the Output.
307  ///
308  /// \p invalidSourcePaths is an optional output parameter to collect the
309  /// invalid source paths that have not been reported in the returned vector.
310  ///
311  /// Returns a vector of \p UsdShadeConnectionSourceInfo structs with
312  /// information about each upsteam attribute. If the vector is empty, there
313  /// have been no valid connections.
314  ///
315  /// \note A valid connection requires the existence of the source attribute
316  /// and also requires that the source prim is UsdShadeConnectableAPI
317  /// compatible.
318  /// \note The python wrapping returns a tuple with the valid connections
319  /// first, followed by the invalid source paths.
320  ///
321  /// \sa UsdShadeConnectableAPI::GetConnectedSources
322  ///
325  SdfPathVector *invalidSourcePaths = nullptr) const;
326 
327  /// \deprecated Please use GetConnectedSources instead
330  TfToken *sourceName,
331  UsdShadeAttributeType *sourceType) const;
332 
333  /// \deprecated
334  /// Returns the "raw" (authored) connected source paths for this Output.
335  ///
336  /// \sa UsdShadeConnectableAPI::GetRawConnectedSourcePaths
337  ///
339  bool GetRawConnectedSourcePaths(SdfPathVector *sourcePaths) const;
340 
341  /// Returns true if and only if this Output is currently connected to a
342  /// valid (defined) source.
343  ///
344  /// \sa UsdShadeConnectableAPI::HasConnectedSource
345  ///
347  bool HasConnectedSource() const;
348 
349  /// Returns true if the connection to this Output's source, as returned by
350  /// GetConnectedSource(), is authored across a specializes arc, which is
351  /// used to denote a base material.
352  ///
353  /// \sa UsdShadeConnectableAPI::IsSourceConnectionFromBaseMaterial
354  ///
357 
358  /// Disconnect source for this Output. If \p sourceAttr is valid, only a
359  /// connection to the specified attribute is disconnected, otherwise all
360  /// connections are removed.
361  ///
362  /// \sa UsdShadeConnectableAPI::DisconnectSource
363  ///
365  bool DisconnectSource(UsdAttribute const &sourceAttr = UsdAttribute()) const;
366 
367  /// Clears sources for this Output in the current UsdEditTarget.
368  ///
369  /// Most of the time, what you probably want is DisconnectSource()
370  /// rather than this function.
371  ///
372  /// \sa UsdShadeConnectableAPI::ClearSources
373  ///
375  bool ClearSources() const;
376 
377  /// \deprecated
379  bool ClearSource() const;
380 
381  /// @}
382 
383  // -------------------------------------------------------------------------
384  /// \name Connected Value API
385  // -------------------------------------------------------------------------
386  /// @{
387 
388  /// \brief Find what is connected to this Output recursively
389  ///
390  /// \sa UsdShadeUtils::GetValueProducingAttributes
393  bool shaderOutputsOnly = false) const;
394 
395  /// @}
396 
397  /// Return true if this Output is valid for querying and authoring
398  /// values and metadata, which is identically equivalent to IsDefined().
399  explicit operator bool() const {
400  return IsDefined();
401  }
402 
403  /// Equality comparison. Returns true if \a lhs and \a rhs represent the
404  /// same UsdShadeOutput, false otherwise.
405  friend bool operator==(const UsdShadeOutput &lhs, const UsdShadeOutput &rhs) {
406  return lhs.GetAttr() == rhs.GetAttr();
407  }
408 
409  /// Inequality comparison. Return false if \a lhs and \a rhs represent the
410  /// same UsdShadeOutput, true otherwise.
411  friend bool operator!=(const UsdShadeOutput &lhs, const UsdShadeOutput &rhs) {
412  return !(lhs == rhs);
413  }
414 
415 private:
417 
418  // Constructor that creates a UsdShadeOutput with the given name on the
419  // given prim.
420  // \p name here is the unnamespaced name of the output.
421  UsdShadeOutput(UsdPrim prim,
422  TfToken const &name,
423  SdfValueTypeName const &typeName);
424 
425  UsdAttribute _attr;
426 };
427 
428 
430 
431 #endif // PXR_USD_USD_SHADE_OUTPUT_H
UsdShadeOutput()
Definition: output.h:35
USDSHADE_API SdfValueTypeName GetTypeName() const
#define USDSHADE_API
Definition: api.h:23
USDSHADE_API bool HasSdrMetadataByKey(const TfToken &key) const
USDSHADE_API bool HasConnectedSource() const
static constexpr UsdTimeCode Default()
Definition: timeCode.h:113
T mod(T x, int y)
Definition: chrono.h:1648
USDSHADE_API bool Set(const VtValue &value, UsdTimeCode time=UsdTimeCode::Default()) const
GT_API const UT_StringHolder time
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UsdShadeConnectionModification
Definition: types.h:36
USDSHADE_API bool HasRenderType() const
USDSHADE_API bool ClearSources() const
USDSHADE_API TfToken GetRenderType() const
friend bool operator!=(const UsdShadeOutput &lhs, const UsdShadeOutput &rhs)
Definition: output.h:411
USDSHADE_API bool CanConnect(const UsdAttribute &source) const
OutGridT const XformOp bool bool
USDSHADE_API void ClearSdrMetadataByKey(const TfToken &key) const
USDSHADE_API bool SetRenderType(TfToken const &renderType) const
const UsdAttribute & GetAttr() const
Explicit UsdAttribute extractor.
Definition: output.h:196
bool IsDefined() const
Definition: output.h:206
Definition: token.h:70
USDSHADE_API std::string GetSdrMetadataByKey(const TfToken &key) const
std::vector< class SdfPath > SdfPathVector
USDSHADE_API bool ConnectToSource(UsdShadeConnectionSourceInfo const &source, ConnectionModification const mod=ConnectionModification::Replace) const
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
Definition: prim.h:116
USDSHADE_API UsdShadeAttributeVector GetValueProducingAttributes(bool shaderOutputsOnly=false) const
Find what is connected to this Output recursively.
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:273
const TfToken & GetName() const
Definition: object.h:221
USDSHADE_API bool HasSdrMetadata() const
USDSHADE_API void ClearSdrMetadata() const
USDSHADE_API TfToken GetBaseName() const
UsdPrim GetPrim() const
Definition: prim.h:2787
USDSHADE_API bool GetRawConnectedSourcePaths(SdfPathVector *sourcePaths) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: output.h:79
USDSHADE_API bool SetConnectedSources(std::vector< UsdShadeConnectionSourceInfo > const &sourceInfos) const
USDSHADE_API void SetSdrMetadataByKey(const TfToken &key, const std::string &value) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDSHADE_API SourceInfoVector GetConnectedSources(SdfPathVector *invalidSourcePaths=nullptr) const
USDSHADE_API void SetSdrMetadata(const SdrTokenMap &sdrMetadata) const
UsdPrim GetPrim() const
Get the prim that the output belongs to.
Definition: output.h:55
TfToken const & GetFullName() const
Definition: output.h:42
USDSHADE_API bool IsSourceConnectionFromBaseMaterial() const
USDSHADE_API SdrTokenMap GetSdrMetadata() const
static USDSHADE_API bool IsOutput(const UsdAttribute &attr)
USDSHADE_API bool GetConnectedSource(UsdShadeConnectableAPI *source, TfToken *sourceName, UsdShadeAttributeType *sourceType) const
friend bool operator==(const UsdShadeOutput &lhs, const UsdShadeOutput &rhs)
Definition: output.h:405
USDSHADE_API bool DisconnectSource(UsdAttribute const &sourceAttr=UsdAttribute()) const
USDSHADE_API bool ClearSource() const
Definition: value.h:146
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:47
UsdShadeAttributeType
Definition: types.h:22