HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nodeGraph.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 USDSHADE_GENERATED_NODEGRAPH_H
8 #define USDSHADE_GENERATED_NODEGRAPH_H
9 
10 /// \file usdShade/nodeGraph.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usdShade/api.h"
14 #include "pxr/usd/usd/typed.h"
15 #include "pxr/usd/usd/prim.h"
16 #include "pxr/usd/usd/stage.h"
17 
18 #include <utility>
19 #include "pxr/usd/usd/editTarget.h"
21 #include "pxr/usd/usdShade/input.h"
25 
26 #include "pxr/base/vt/value.h"
27 
28 #include "pxr/base/gf/vec3d.h"
29 #include "pxr/base/gf/vec3f.h"
30 #include "pxr/base/gf/matrix4d.h"
31 
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/tf/type.h"
34 
36 
37 class SdfAssetPath;
38 
39 // -------------------------------------------------------------------------- //
40 // NODEGRAPH //
41 // -------------------------------------------------------------------------- //
42 
43 /// \class UsdShadeNodeGraph
44 ///
45 /// A node-graph is a container for shading nodes, as well as other
46 /// node-graphs. It has a public input interface and provides a list of public
47 /// outputs.
48 ///
49 /// <b>Node Graph Interfaces</b>
50 ///
51 /// One of the most important functions of a node-graph is to host the "interface"
52 /// with which clients of already-built shading networks will interact. Please
53 /// see \ref UsdShadeNodeGraph_Interfaces "Interface Inputs" for a detailed
54 /// explanation of what the interface provides, and how to construct and
55 /// use it, to effectively share/instance shader networks.
56 ///
57 /// <b>Node Graph Outputs</b>
58 ///
59 /// These behave like outputs on a shader and are typically connected to an
60 /// output on a shader inside the node-graph.
61 ///
62 ///
64 {
65 public:
66  /// Compile time constant representing what kind of schema this class is.
67  ///
68  /// \sa UsdSchemaKind
70 
71  /// Construct a UsdShadeNodeGraph on UsdPrim \p prim .
72  /// Equivalent to UsdShadeNodeGraph::Get(prim.GetStage(), prim.GetPath())
73  /// for a \em valid \p prim, but will not immediately throw an error for
74  /// an invalid \p prim
75  explicit UsdShadeNodeGraph(const UsdPrim& prim=UsdPrim())
76  : UsdTyped(prim)
77  {
78  }
79 
80  /// Construct a UsdShadeNodeGraph on the prim held by \p schemaObj .
81  /// Should be preferred over UsdShadeNodeGraph(schemaObj.GetPrim()),
82  /// as it preserves SchemaBase state.
83  explicit UsdShadeNodeGraph(const UsdSchemaBase& schemaObj)
84  : UsdTyped(schemaObj)
85  {
86  }
87 
88  /// Destructor.
90  virtual ~UsdShadeNodeGraph();
91 
92  /// Return a vector of names of all pre-declared attributes for this schema
93  /// class and all its ancestor classes. Does not include attributes that
94  /// may be authored by custom/extended methods of the schemas involved.
96  static const TfTokenVector &
97  GetSchemaAttributeNames(bool includeInherited=true);
98 
99  /// Return a UsdShadeNodeGraph holding the prim adhering to this
100  /// schema at \p path on \p stage. If no prim exists at \p path on
101  /// \p stage, or if the prim at that path does not adhere to this schema,
102  /// return an invalid schema object. This is shorthand for the following:
103  ///
104  /// \code
105  /// UsdShadeNodeGraph(stage->GetPrimAtPath(path));
106  /// \endcode
107  ///
109  static UsdShadeNodeGraph
110  Get(const UsdStagePtr &stage, const SdfPath &path);
111 
112  /// Attempt to ensure a \a UsdPrim adhering to this schema at \p path
113  /// is defined (according to UsdPrim::IsDefined()) on this stage.
114  ///
115  /// If a prim adhering to this schema at \p path is already defined on this
116  /// stage, return that prim. Otherwise author an \a SdfPrimSpec with
117  /// \a specifier == \a SdfSpecifierDef and this schema's prim type name for
118  /// the prim at \p path at the current EditTarget. Author \a SdfPrimSpec s
119  /// with \p specifier == \a SdfSpecifierDef and empty typeName at the
120  /// current EditTarget for any nonexistent, or existing but not \a Defined
121  /// ancestors.
122  ///
123  /// The given \a path must be an absolute prim path that does not contain
124  /// any variant selections.
125  ///
126  /// If it is impossible to author any of the necessary PrimSpecs, (for
127  /// example, in case \a path cannot map to the current UsdEditTarget's
128  /// namespace) issue an error and return an invalid \a UsdPrim.
129  ///
130  /// Note that this method may return a defined prim whose typeName does not
131  /// specify this schema class, in case a stronger typeName opinion overrides
132  /// the opinion at the current EditTarget.
133  ///
135  static UsdShadeNodeGraph
136  Define(const UsdStagePtr &stage, const SdfPath &path);
137 
138 protected:
139  /// Returns the kind of schema this class belongs to.
140  ///
141  /// \sa UsdSchemaKind
143  UsdSchemaKind _GetSchemaKind() const override;
144 
145 private:
146  // needs to invoke _GetStaticTfType.
147  friend class UsdSchemaRegistry;
149  static const TfType &_GetStaticTfType();
150 
151  static bool _IsTypedSchema();
152 
153  // override SchemaBase virtuals.
155  const TfType &_GetTfType() const override;
156 
157 public:
158  // ===================================================================== //
159  // Feel free to add custom code below this line, it will be preserved by
160  // the code generator.
161  //
162  // Just remember to:
163  // - Close the class declaration with };
164  // - Close the namespace with PXR_NAMESPACE_CLOSE_SCOPE
165  // - Close the include guard with #endif
166  // ===================================================================== //
167  // --(BEGIN CUSTOM CODE)--
168 
169  /// Constructor that takes a ConnectableAPI object. Allow implicit
170  /// (auto) conversion of UsdShadeConnectableAPI to UsdShadeNodeGraph, so
171  /// that a ConnectableAPI can be passed into any function that accepts a
172  /// NodeGraph.
173  ///
174  /// \note that the conversion may produce an invalid NodeGraph object,
175  /// because not all UsdShadeConnectableAPI%s are UsdShadeNodeGraph%s
177  UsdShadeNodeGraph(const UsdShadeConnectableAPI &connectable);
178 
179  /// Contructs and returns a UsdShadeConnectableAPI object with this
180  /// node-graph.
181  ///
182  /// Note that most tasks can be accomplished without explicitly constructing
183  /// a UsdShadeConnectable API, since connection-related API such as
184  /// UsdShadeConnectableAPI::ConnectToSource() are static methods, and
185  /// UsdShadeNodeGraph will auto-convert to a UsdShadeConnectableAPI when
186  /// passed to functions that want to act generically on a connectable
187  /// UsdShadeConnectableAPI object.
190 
191  /// \anchor UsdShadeNodeGraph_Output
192  /// \name Outputs of a node-graph. These typically connect to outputs of
193  /// shaders or nested node-graphs within the node-graph.
194  ///
195  /// @{
196 
197  /// Create an output which can either have a value or can be connected.
198  /// The attribute representing the output is created in the "outputs:"
199  /// namespace.
200  ///
203  const SdfValueTypeName& typeName) const;
204 
205  /// Return the requested output if it exists.
206  ///
208  UsdShadeOutput GetOutput(const TfToken &name) const;
209 
210  /// Outputs are represented by attributes in the "outputs:" namespace.
211  /// If \p onlyAuthored is true (the default), then only return authored
212  /// attributes; otherwise, this also returns un-authored builtins.
213  ///
215  std::vector<UsdShadeOutput> GetOutputs(bool onlyAuthored=true) const;
216 
217  /// \deprecated in favor of GetValueProducingAttributes on UsdShadeOutput
218  /// Resolves the connection source of the requested output, identified by
219  /// \p outputName to a shader output.
220  ///
221  /// \p sourceName is an output parameter that is set to the name of the
222  /// resolved output, if the node-graph output is connected to a valid
223  /// shader source.
224  ///
225  /// \p sourceType is an output parameter that is set to the type of the
226  /// resolved output, if the node-graph output is connected to a valid
227  /// shader source.
228  ///
229  /// \return Returns a valid shader object if the specified output exists and
230  /// is connected to one. Return an empty shader object otherwise.
231  /// The python version of this method returns a tuple containing three
232  /// elements (the source shader, sourceName, sourceType).
235  const TfToken &outputName,
236  TfToken *sourceName,
237  UsdShadeAttributeType *sourceType) const;
238 
239  /// @}
240 
241  /// \anchor UsdShadeNodeGraph_Interfaces
242  /// \name Interface inputs of a node-graph.
243  ///
244  /// In addition to serving as the "head" for all of the shading networks
245  /// that describe each render target's particular node-graph, the node-graph
246  /// prim provides a unified "interface" that allows node-graphs to share
247  /// shading networks while retaining the ability for each to specify its own
248  /// set of unique values for the interface inputs that users may need to
249  /// modify.
250  ///
251  /// A "Node-graph Interface" is a combination of:
252  /// \li a flat collection of attributes, of arbitrary names
253  /// \li for each such attribute, a list of UsdShaderInput targets
254  /// whose attributes on Shader prims should be driven by the interface
255  /// input.
256  ///
257  /// A single interface input can drive multiple shader inputs and be
258  /// consumed by multiple render targets. The set of interface inputs itself
259  /// is intentionally flat, to encourage sharing of the interface between
260  /// render targets. Clients are always free to create interface inputs with
261  /// namespacing to segregate "private" attributes exclusive to the render
262  /// target, but we hope this will be an exception.
263  ///
264  /// To facilitate connecting, qualifying, and interrogating interface
265  /// attributes, we use the attribute schema UsdShadeInput, which also
266  /// serves as an abstraction for shader inputs.
267  ///
268  /// <b>Scoped Interfaces</b>
269  ///
270  /// \todo describe scoped interfaces and fix bug/108940 to account for them.
271  ///
272  /// @{
273 
274  /// Create an Input which can either have a value or can be connected.
275  /// The attribute representing the input is created in the "inputs:"
276  /// namespace.
277  ///
278  /// \todo clarify error behavior if typeName does not match existing,
279  /// defined attribute - should match UsdPrim::CreateAttribute - bug/108970
280  ///
283  const SdfValueTypeName& typeName) const;
284 
285  /// Return the requested input if it exists.
286  ///
288  UsdShadeInput GetInput(const TfToken &name) const;
289 
290  /// Returns all inputs present on the node-graph. These are represented by
291  /// attributes in the "inputs:" namespace.
292  /// If \p onlyAuthored is true (the default), then only return authored
293  /// attributes; otherwise, this also returns un-authored builtins.
294  ///
296  std::vector<UsdShadeInput> GetInputs(bool onlyAuthored=true) const;
297 
298  /// @}
299 
300  // Provide custom hash and equality comparison function objects for
301  // UsdShadeNodeGraph until bug 143077 is resolved.
302 
303  /// Hash functor for UsdShadeNodeGraph objects.
305  inline size_t operator()(const UsdShadeNodeGraph &nodeGraph) const {
306  return hash_value(nodeGraph.GetPrim());
307  }
308  };
309  /// Equality comparator for UsdShadeNodeGraph objects.
311  {
312  inline bool operator() (UsdShadeNodeGraph const& s1,
313  UsdShadeNodeGraph const& s2) const
314  {
315  return s1.GetPrim() == s2.GetPrim();
316  }
317  };
318 
319  // ---------------------------------------------------------------------- //
320  /// \anchor UsdShadeNodeGraph_InterfaceInputs
321  /// \name Interface Inputs
322  ///
323  /// API to query the inputs that form the interface of the node-graph and
324  /// their connections.
325  ///
326  /// @{
327 
328  /// Returns all the "Interface Inputs" of the node-graph. This is the same
329  /// as GetInputs(), but is provided as a convenience, to allow clients to
330  /// distinguish between inputs on shaders vs. interface-inputs on
331  /// node-graphs.
333  std::vector<UsdShadeInput> GetInterfaceInputs() const;
334 
335  /// Map of interface inputs to corresponding vectors of inputs that
336  /// consume their values.
337  typedef std::unordered_map<UsdShadeInput, std::vector<UsdShadeInput>,
339 
340  /// Map of node-graphs to their associated input-consumers map.
341  typedef std::unordered_map<UsdShadeNodeGraph,
346 
347  /// Walks the namespace subtree below the node-graph and computes a map
348  /// containing the list of all inputs on the node-graph and the associated
349  /// vector of consumers of their values. The consumers can be inputs on
350  /// shaders within the node-graph or on nested node-graphs).
351  ///
352  /// If \p computeTransitiveConsumers is true, then value consumers
353  /// belonging to <b>node-graphs</b> are resolved transitively to compute the
354  /// transitive mapping from inputs on the node-graph to inputs on shaders
355  /// inside the material. Note that inputs on node-graphs that don't have
356  /// value consumers will continue to be included in the result.
357  ///
358  /// This API is provided for use by DCC's that want to present node-graph
359  /// interface / shader connections in the opposite direction than they are
360  /// encoded in USD.
361  ///
364  bool computeTransitiveConsumers=false) const;
365 
366  /// @}
367 
368 };
369 
371 
372 #endif
#define USDSHADE_API
Definition: api.h:23
Hash functor.
Definition: input.h:91
virtual USDSHADE_API ~UsdShadeNodeGraph()
Destructor.
std::unordered_map< UsdShadeInput, std::vector< UsdShadeInput >, UsdShadeInput::Hash > InterfaceInputConsumersMap
Definition: nodeGraph.h:338
USDSHADE_API UsdShadeShader ComputeOutputSource(const TfToken &outputName, TfToken *sourceName, UsdShadeAttributeType *sourceType) const
std::unordered_map< UsdShadeNodeGraph, InterfaceInputConsumersMap, NodeGraphHasher, NodeGraphEqualFn > NodeGraphInputConsumersMap
Map of node-graphs to their associated input-consumers map.
Definition: nodeGraph.h:345
bool operator()(UsdShadeNodeGraph const &s1, UsdShadeNodeGraph const &s2) const
Definition: nodeGraph.h:312
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USDSHADE_API std::vector< UsdShadeInput > GetInputs(bool onlyAuthored=true) const
USDSHADE_API UsdShadeConnectableAPI ConnectableAPI() const
UsdShadeNodeGraph(const UsdPrim &prim=UsdPrim())
Definition: nodeGraph.h:75
static USDSHADE_API UsdShadeNodeGraph Get(const UsdStagePtr &stage, const SdfPath &path)
Definition: token.h:70
Represents a concrete typed schema.
USDSHADE_API std::vector< UsdShadeInput > GetInterfaceInputs() const
UsdShadeNodeGraph(const UsdSchemaBase &schemaObj)
Definition: nodeGraph.h:83
USDSHADE_API std::vector< UsdShadeOutput > GetOutputs(bool onlyAuthored=true) const
static USDSHADE_API const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
Definition: prim.h:116
static const UsdSchemaKind schemaKind
Definition: nodeGraph.h:69
Definition: typed.h:44
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:273
USDSHADE_API UsdShadeOutput GetOutput(const TfToken &name) const
size_t operator()(const UsdShadeNodeGraph &nodeGraph) const
Definition: nodeGraph.h:305
UsdSchemaKind
Definition: common.h:112
Hash functor for UsdShadeNodeGraph objects.
Definition: nodeGraph.h:304
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDSHADE_API UsdShadeOutput CreateOutput(const TfToken &name, const SdfValueTypeName &typeName) const
static USDSHADE_API UsdShadeNodeGraph Define(const UsdStagePtr &stage, const SdfPath &path)
Definition: type.h:47
USDSHADE_API UsdShadeInput CreateInput(const TfToken &name, const SdfValueTypeName &typeName) const
USDSHADE_API UsdShadeInput GetInput(const TfToken &name) const
size_t hash_value(const CH_ChannelRef &ref)
USDSHADE_API InterfaceInputConsumersMap ComputeInterfaceInputConsumersMap(bool computeTransitiveConsumers=false) const
Equality comparator for UsdShadeNodeGraph objects.
Definition: nodeGraph.h:310
UsdPrim GetPrim() const
Return this schema object's held prim.
Definition: schemaBase.h:103
USDSHADE_API UsdSchemaKind _GetSchemaKind() const override
UsdShadeAttributeType
Definition: types.h:22