HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HUSD_BindMaterial.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_BindMaterial__
19 #define __HUSD_BindMaterial__
20 
21 #include "HUSD_API.h"
22 #include "HUSD_DataHandle.h"
23 #include "HUSD_Path.h"
24 #include "HUSD_TimeCode.h"
25 #include <SYS/SYS_Types.h>
26 #include <UT/UT_Optional.h>
27 
28 
29 template <typename T> class UT_Array;
30 
31 
33 {
34 public:
35  /// Standard constructor.
37 
38  /// Assigns the given material to the given geometry primitive(s).
39  bool bind(const UT_StringRef &mat_prim_path,
40  const UT_StringRef &geo_prim_path) const;
41  bool bind(const UT_StringRef &mat_prim_path,
42  const HUSD_FindPrims &find_geo_prims) const;
43  bool bindWithCollection(const UT_StringRef &mat_prim_path,
44  const HUSD_FindPrims &include_geo_prims,
45  const HUSD_FindPrims &exclude_geo_prims) const;
46  bool bindWithPathExpression(const UT_StringRef &mat_prim_path,
47  const UT_StringRef &path_expr) const;
48 
49  /// Assigns the given material to a subset of the geometry primitive. This
50  /// method creates the geometry subset. If a geometry subset already
51  /// exists, it can be bound using the regular bind methods above.
52  bool bindSubset(const UT_StringRef &mat_prim_path,
53  const UT_StringRef &geo_prim_path,
54  const UT_Array<exint> *face_indices,
55  const HUSD_TimeCode &tc = HUSD_TimeCode()) const;
56 
57  /// Creates a geometry subset for material binding, but does not actually
58  /// bind any material to the prim.
59  HUSD_Path createSubset(const UT_StringRef &subset_name,
60  const UT_StringRef &geo_prim_path,
61  const UT_Array<exint> &face_indices,
62  const HUSD_TimeCode &tc = HUSD_TimeCode()) const;
63 
64  /// Returns true if the given path points to a primitive,
65  /// and that primitive does not have any material bound to it.
66  bool isPrimWithoutBoundMaterial( const UT_StringRef &prim_path,
67  const UT_Optional<UT_StringHolder> &purpose = std::nullopt);
68 
69  /// Makes sure the primitives are not bound to any material.
70  /// I.e, if there is any direct material binding (on the given prim
71  /// or its ancestor), authors a binding block on the given prim,
72  /// and if there is any collection-based assignment on the prim or ancestor,
73  /// removes the given prim from the collection.
74  bool unbindAll( const HUSD_FindPrims &find_prims ) const;
75 
76  /// Removes the material binding from the geometry primitive(s).
77  /// The @p unbind_limit determines how many material bindings are blocked.
78  //
79  /// The binding limit of 1 unassigns the currently bound material,
80  /// allowing any other candidate moaterial to take over.
81  ///
82  /// The binding limit of 2 unassigns the currently bound material (if any),
83  /// and the second candidate material (if any), allowing the next candidate
84  /// material to take effect.
85  bool unbind( const HUSD_FindPrims &find_prims,
86  const UT_StringHolder &purpose,
87  int unbind_limit = 1) const;
88 
89  /// Looks for the specified attrname on any prims in the named layer.
90  /// This string attribute is turned into an SdfPath that is bound as the
91  /// material to the primitive with the attribute. If the attribute string
92  /// points to a geometry primitive, we assign the material bound to that
93  /// geometry primitive. Return false if the operation fails in an
94  /// unrecoverable way. An error will have been added to the error scope
95  /// to explain the failure. Warnings may also be generated for more minor
96  /// issues such as invalid material paths.
97  /// If create_empty_materials is enabled, the reffileattrname and
98  /// refprimattrname can be used to optionally author a reference to a file
99  /// (and reference prim within that file) rather than an empty material.
100  bool assignMaterialsFromAttribute(
101  const UT_StringRef &layername,
103  const UT_StringRef &primpath,
104  bool loadasreference,
105  const UT_StringRef &refprimpath,
106  const UT_StringRef &attrname,
107  bool remove_attr,
108  bool create_empty_materials,
109  const UT_StringRef &reffileattrname,
110  const UT_StringRef &refprimattrname) const;
111 
112  /// Enumeration of the ways in which a binding can be performed.
113  enum class BindMethod
114  {
115  DIRECT, // direct binding
116  COLLECTION // collection-based binding
117  };
118 
119  /// Sets the method of defining bindings.
120  void setBindMethod( BindMethod method )
121  { myBindMethod = method; }
123  { return myBindMethod; }
124 
125  /// Sets the collection expansion option when defining collections.
126  void setBindCollectionExpand( bool expand )
127  { myBindCollectionExpand = expand; }
129  { return myBindCollectionExpand; }
130 
131  /// For collection-based bindings, sets the USD primitive path on which the
132  /// collection-based binding is defined, and the collection name. If these
133  /// are not supplied, we'll do a best guess for these values.
135  const UT_StringHolder &primpath,
136  const UT_StringHolder &collectionname)
137  {
138  myBindCollectionPrimPath = primpath;
139  myBindCollectionName = collectionname;
140  }
142  { return myBindCollectionPrimPath; }
144  { return myBindCollectionName; }
145 
146  /// Indicates whether a warning should be generated if we are asked to
147  /// create a collection binding, but are not explicitly given a prim on
148  /// which to author the binding (and we have to calculate one instead).
150  { myBindCollectionWarnUnspecifiedPrim = warn; }
152  { return myBindCollectionWarnUnspecifiedPrim; }
153 
154  /// Enumeration of the material binding strength.
155  enum class Strength
156  {
157  DEFAULT, // fallback
158  STRONG, // stronger than descendants
159  WEAK // weaker than descendants
160  };
161 
162  /// Sets the strength preference for material assignments.
163  void setStrength( Strength strength )
164  { myStrength = strength; }
166  { return myStrength; }
167 
168  /// Sets the purpose for material assignments.
169  void setPurpose( const UT_StringHolder &purpose )
170  { myPurpose = purpose; }
171  const UT_StringHolder & getPurpose() const
172  { return myPurpose; }
173 
174 
175 private:
176  HUSD_AutoWriteLock & myWriteLock;
177  BindMethod myBindMethod; // Collection based?
178  UT_StringHolder myBindCollectionPrimPath; // Collection location
179  UT_StringHolder myBindCollectionName; // Collection bind name
180  bool myBindCollectionExpand; // Expand collections
181  bool myBindCollectionWarnUnspecifiedPrim;
182  Strength myStrength; // Binding strength
183  UT_StringHolder myPurpose; // Binding purpose
184 };
185 
186 #endif
187 
BindMethod getBindMethod() const
const UT_StringHolder & getBindCollectionPrimPath() const
const UT_StringHolder & getBindCollectionName() const
#define HUSD_API
Definition: HUSD_API.h:31
const UT_StringHolder & getPurpose() const
void setBindCollectionExpand(bool expand)
Sets the collection expansion option when defining collections.
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
bool getBindCollectionWarnUnspecifiedPrim() const
void setPurpose(const UT_StringHolder &purpose)
Sets the purpose for material assignments.
BindMethod
Enumeration of the ways in which a binding can be performed.
Strength
Enumeration of the material binding strength.
void setBindMethod(BindMethod method)
Sets the method of defining bindings.
void setStrength(Strength strength)
Sets the strength preference for material assignments.
**If you just want to fire and args
Definition: thread.h:618
void setBindCollectionWarnUnspecifiedPrim(bool warn)
void setBindCollectionInfo(const UT_StringHolder &primpath, const UT_StringHolder &collectionname)
bool getBindCollectionExpand() const
Strength getStrength() const