HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HUSD_MaterialUniGraph.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef __HUSD_MaterialUniGraph_h__
19 #define __HUSD_MaterialUniGraph_h__
20 
21 #include "HUSD_API.h"
22 #include "HUSD_DataHandle.h"
23 #include "HUSD_Path.h"
24 #include <UN/UN_GraphData.h>
25 #include <UN/UN_UniGraph.h>
26 #include <string>
27 
28 #define HUSD_SHADER_GRAPH_CATEGORY "UsdShadeGraph"
29 #define HUSD_NODEGRAPH_NODE_TYPE "NodeGraph"
30 using HUSD_MaterialUniGraphDataHandleProvider = std::function<const HUSD_DataHandle & ()>;
31 
32 /// Why do we inherit from UN_BasicGraph and construct a UN_Graph from the
33 /// UsdShade network? Why not just look at the stage to answer queries about
34 /// the network? Two reasons:
35 /// 1. The USD graph is not designed to answer queries like the ones we make
36 /// through UNI_Graph. dstWires is a good example - you have to scan all
37 /// input ports in the whole subnet. Even wires themselves don't actually
38 /// exist so building "ids" for them could be complex, especially if you
39 /// want to enable quick looks for all the queries we need to support.
40 ///
41 /// With enough clever caching of the graph structure, we can answer the
42 /// UNI_Graph queries. With even more caching we can answer the questions
43 /// quickly. But by then we've basically rebuilt UN_Graph. So instead we
44 /// accept that UN_Graph is the ultra-fast already-written caching
45 /// mechanism for the graph structure that lets us satisfy the UNI_Graph
46 /// API we need to make for a satisfying graph editing experience.
47 /// 2. In order to answer queries based on the USD stage, every time anything
48 /// changes in the graph, we need to recompose the USD stage. When editing
49 /// a cached UN_Graph representation we are free to edit the overlay layer
50 /// as much as we want, with no obligation to actually recompose the USD
51 /// stage just to keep giving up to date answers to queries. This is a
52 /// huge performance boost when making a bunch of small changes to a
53 /// graph. It gives us the equivalent of an SdfChangeBlock around an
54 /// arbitrarily large/complex series of graph edit operations.
56 {
57 public:
58  static UNI_GraphHandle createGraph(
59  HUSD_MaterialUniGraphDataHandleProvider data_handle_provider,
60  const HUSD_Path &material_path,
61  const std::string &in_layer,
62  int material_index);
63 
64  ~HUSD_MaterialUniGraph() override;
65 
66  /// Fetch all our edits as a USDA layer.
67  bool getEditLayerContent(std::string *out_layer) const;
68  /// Get the index of the material on the node we are representing.
69  int getMaterialIndex() const;
70 
71  /// @{ Overrides of UN_UniGraph "getter" methods that need to be
72  /// specialized for UsdShade graphs.
73  UT_StringHolder parmDialogScript( UNI_NodeID node_id ) const override;
74  UNI_ParmValue parmValue( UNI_NodeID node_id,
75  const UT_StringRef &parm_name ) const override;
76  /// @}
77 
78  /// Save items to a stream.
79  bool copyItems(UNI_NodeID parent_id,
80  const UNI_NodeIDList &node_ids,
81  const UNI_StickyNoteIDList &note_ids,
82  std::ostream &os) const override;
83 
84 protected:
85  /// @{ Non-const methods are all protected.
86  /// Unhide base class virtual method with other signatures besides the
87  /// ones explicitly overridden in this class.
92 
93  void setWireStyle(UNI_WireStyle wire_style) override;
94 
95  UNI_NodeID createNode( UNI_NodeID parent_id,
96  const UT_StringHolder &node_name,
97  const UT_StringHolder &node_type_name,
98  const UT_StringHolder &signature_name
99  = UT_StringHolder()) override;
100  void setName( UNI_NodeID node_id,
101  const UT_StringHolder &name) override;
102  void setSignature( UNI_NodeID node_id,
103  const UT_StringHolder &signature_name ) override;
104  void setPosition(UNI_NodeID node_id,
105  const UT_Vector2D &pos) override;
106  void setColor( UNI_NodeID node_id,
107  const UT_Color &clr) override;
108  void setComment( UNI_NodeID node_id,
109  const UT_StringHolder &comment) override;
110  void setTags( UNI_NodeID node_id,
111  const UT_StringArray &tags) override;
112  void setParmValue( UNI_NodeID node_id,
113  const UT_StringRef &parm_name,
114  const UNI_ParmValue &value ) override;
115  void destroy( UNI_NodeID node_id ) override;
116 
117  void setContainerConnectPosition( UNI_NodeID parent_id,
118  UNI_ContainerConnectType contype,
119  const UT_Vector2D &pos ) override;
120  void setContainerConnectColor( UNI_NodeID parent_id,
121  UNI_ContainerConnectType contype,
122  const UT_Color &clr ) override;
123 
124  UNI_WireID createWire( UNI_PortID src, UNI_PortID dst ) override;
125  void destroy( UNI_WireID wire_id ) override;
126 
127  UNI_StickyNoteID createStickyNote( UNI_NodeID parent_id,
128  const UT_StringHolder &name ) override;
129  void setName( UNI_StickyNoteID note_id,
130  const UT_StringHolder &name ) override;
131  void setPosition( UNI_StickyNoteID note_id,
132  const UT_Vector2D &pos ) override;
133  void setSize( UNI_StickyNoteID note_id,
134  const UT_Vector2D &size ) override;
135  void setColor( UNI_StickyNoteID note_id,
136  const UT_Color &clr ) override;
137  void setText( UNI_StickyNoteID note_id,
138  const UT_StringHolder &text ) override;
139  void setTextColor( UNI_StickyNoteID note_id,
140  const UT_Color &clr ) override;
141  void setTextSize( UNI_StickyNoteID note_id,
142  const float size ) override;
143  void setCollapsed( UNI_StickyNoteID note_id,
144  bool collapsed ) override;
145  void setBackgroundHidden( UNI_StickyNoteID note_id,
146  bool hidden ) override;
147  void destroy( UNI_StickyNoteID note_id ) override;
148  bool pasteItems(UNI_NodeID parent_id,
149  UNI_NodeIDList &node_ids,
150  UNI_StickyNoteIDList &note_ids,
151  std::istream &is,
152  UT_StringMap<UT_StringHolder> *rename_map) override;
153  /// @}
154 
156  HUSD_MaterialUniGraphDataHandleProvider data_handle_provider,
157  const HUSD_Path &material_path,
158  const std::string &in_layer,
159  int material_index);
160 
161 private:
162  void buildGraphFromMaterial(const HUSD_AutoAnyLock &lock);
163 
164  class husd_MaterialUniGraphPrivate;
166  HUSD_MaterialUniGraphDataHandleProvider myDataHandleProviderFn;
167  const HUSD_Path myMaterialPath;
168  const int myMaterialIndex;
169  UT_Map<UNI_NodeID, HUSD_Path> myNodeIdToPathMap;
170  UT_Map<UNI_StickyNoteID, HUSD_Path> myStickyNoteIdToPathMap;
171 };
172 
173 #endif
174 
void setText(UNI_StickyNoteID note_id, const UT_StringHolder &text) override
Non-const methods are all protected.
void setTags(UNI_NodeID node_id, const UT_StringArray &tags) override
Non-const methods are all protected.
UNI_NodeID createNode(UNI_NodeID parent_id, const UT_StringHolder &node_name, const UT_StringHolder &node_type_name, const UT_StringHolder &signature_name=UT_StringHolder()) override
Non-const methods are all protected.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define HUSD_API
Definition: HUSD_API.h:31
void setSignature(UNI_NodeID node_id, const UT_StringHolder &signature_name) override
Non-const methods are all protected.
bool pasteItems(UNI_NodeID parent_id, UNI_NodeIDList &node_ids, UNI_StickyNoteIDList &note_ids, std::istream &is, UT_StringMap< UT_StringHolder > *rename_map) override
Non-const methods are all protected.
void setContainerConnectPosition(UNI_NodeID parent_id, UNI_ContainerConnectType contype, const UT_Vector2D &pos) override
Non-const methods are all protected.
void setContainerConnectColor(UNI_NodeID parent_id, UNI_ContainerConnectType contype, const UT_Color &clr) override
Non-const methods are all protected.
void setSize(UNI_StickyNoteID note_id, const UT_Vector2D &size) override
Non-const methods are all protected.
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
void setCollapsed(UNI_StickyNoteID note_id, bool collapsed) override
Non-const methods are all protected.
void setComment(UNI_NodeID node_id, const UT_StringHolder &comment) override
Non-const methods are all protected.
void destroy(UNI_NodeID node_id) override
Non-const methods are all protected.
UT_StringHolder parmDialogScript(UNI_NodeID node_id) const override
void setTextSize(UNI_StickyNoteID note_id, const float size) override
Non-const methods are all protected.
void setColor(UNI_NodeID node_id, const UT_Color &clr) override
Non-const methods are all protected.
GLuint const GLchar * name
Definition: glcorearb.h:786
std::variant< UNI_InvalidParmValue, UNI_DefaultParmValue, bool, int64, fpreal64, UT_Vector2D, UT_Vector3D, UT_Vector4D, UT_Matrix3D, UT_Matrix4D, UT_StringHolder, UNI_RampParmValue > UNI_ParmValue
Variant data type that holds all the parm value types allowed in UNI parms.
Definition: UNI_Graph.h:126
void setWireStyle(UNI_WireStyle wire_style) override
The drawing style of the connection wires in the graph.
bool copyItems(UNI_NodeID parent_id, const UNI_NodeIDList &node_ids, const UNI_StickyNoteIDList &note_ids, std::ostream &os) const override
UNI_ContainerConnectType
Definition: UNI_Graph.h:35
UNI_ParmValue parmValue(UNI_NodeID node_id, const UT_StringRef &parm_name) const override
Get the value of a parm on this node.
UNI_StickyNoteID createStickyNote(UNI_NodeID parent_id, const UT_StringHolder &name) override
Non-const methods are all protected.
GLsizeiptr size
Definition: glcorearb.h:664
GLenum GLenum dst
Definition: glcorearb.h:1793
UNI_WireStyle
Definition: UNI_Include.h:38
void setTextColor(UNI_StickyNoteID note_id, const UT_Color &clr) override
Non-const methods are all protected.
std::function< const HUSD_DataHandle &()> HUSD_MaterialUniGraphDataHandleProvider
UNI_WireID createWire(UNI_PortID src, UNI_PortID dst) override
Non-const methods are all protected.
void setBackgroundHidden(UNI_StickyNoteID note_id, bool hidden) override
Non-const methods are all protected.
void setPosition(UNI_NodeID node_id, const UT_Vector2D &pos) override
Non-const methods are all protected.
void setParmValue(UNI_NodeID node_id, const UT_StringRef &parm_name, const UNI_ParmValue &value) override
Non-const methods are all protected.
void setName(UNI_NodeID node_id, const UT_StringHolder &name) override
Non-const methods are all protected.
GLenum src
Definition: glcorearb.h:1793