forked from dreamworksanimation/USD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectionAPI.h
451 lines (403 loc) · 16.9 KB
/
collectionAPI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef USDGEOM_COLLECTION_API_H
#define USDGEOM_COLLECTION_API_H
#include "pxr/pxr.h"
#include "pxr/usd/usdGeom/api.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/usd/usd/prim.h"
#include "pxr/usd/usd/schemaBase.h"
#include "pxr/usd/usdGeom/tokens.h"
#include "pxr/base/vt/value.h"
#include "pxr/base/tf/token.h"
#include <string>
PXR_NAMESPACE_OPEN_SCOPE
// -------------------------------------------------------------------------- //
// COLLECTION API //
// -------------------------------------------------------------------------- //
/// \class UsdGeomCollectionAPI
///
/// \deprecated This API schema has been deprecated in favor of UsdCollectionAPI.
///
/// This is a general purpose API schema, used to describe a collection of
/// heterogeneous objects within the scene. "Objects" here may be prims,
/// properties or face-sets belonging to prims. It's an add-on schema that can
/// be applied many times to a prim with different collection names. All the
/// properties authored by the schema are namespaced under "collection:". The
/// given name of the collection provides additional namespacing for the various
/// per-collection properties, which include the following:
///
/// \li <b>rel collection:collectionName</b> - specifies a list of targets
/// that are included in the collection. These can be entire prims or prims with
/// faces.
/// \li <b>int[] collection:collectionName:targetFaceCounts</b> - is authored
/// if the collection restricts to a face-set for any of its targets. It
/// contains an element for each target: zero if the target has no
/// face-restriction, or the number of consecutive face-indices in the
/// associated targetFaceIndices property that correspond to the target.
/// \li <b>int[] collection:myCollection:targetFaceIndices</b> - contains the
/// list of face indices that correspond to the various face counts in the
/// associated targetFaceCounts property, for targets that have a
/// face-restriction.
///
/// \note Each target object may only appear once in a collection since the
/// targets of a single relationship form a unique set.
///
/// Note that we have not referred anywhere to meshes or polygons in this class.
/// We use the term "face" generically, as this schema could be used equally
/// well to create collections containing UsdGeomCurves.
///
/// Here's some sample code to create a collection on a prim and include a
/// set of objects in the collection:
///
/// \code
/// UsdGeomModelAPI model(stage.GetPrimAtPath("/path/to/model"));
/// UsdGeomMesh sphere(stage.GetPrimAtPath("/path/to/sphereMesh"));
/// UsdGeomMesh cube(stage.GetPrimAtPath("/path/to/cubeMesh"));
///
/// UsdGeomCollectionAPI geomCollection = UsdGeomCollectionAPI::Create(model,
/// "geometry");
///
/// // This adds the entire sphere as a target of the collection.
/// geomCollection.AddTarget(sphere.GetPath());
///
/// VtIntArray cubeFaceIndices;
/// // ... populate faceIndices here.
/// // This adds the specified set of faceIndices belonging to the cube as a
/// // target of the collection.
/// geomCollection.AddTarget(cube.GetPath(), cubeFaceIndices);
/// \endcode
///
/// An alternate way to author a collection is by setting the individual
/// collection properties directly via API available in
/// \ref UsdGeomCollectionAPI_PropertyValueAPI.
///
/// \code
/// VtIntArray targetFaceCounts(2);
/// # face count of 0 indicates that the entire prim is part of the collection.
/// targetFaceCounts[0] = 0;
/// targetFaceCounts[1] = cubeFaceIndices.size();
///
/// VtIntArray targetFaceIndices;
/// // ... insert face indices belonging to the targets with face-restrictions
/// // ... here.
///
/// // Set the targets of the collection.
/// SdfPathVector targets;
/// targets.push_back(sphere.GetPath());
/// targets.push_back(cube.GetPath());
/// geomCollection.SetTargets(targets)
///
/// // Set the targetFaceIndices and targetFaceCounts.
/// geomCollection.SetTargetFaceCounts(targetFaceCounts);
/// geomCollection.SetTargetFaceIndices(targetFaceIndices);
/// \endcode
///
class UsdGeomCollectionAPI: public UsdSchemaBase
{
public:
/// Construct a UsdGeomCollectionAPI with the given \p name on
/// the UsdPrim \p prim .
///
explicit UsdGeomCollectionAPI(const UsdPrim& prim=UsdPrim(),
const TfToken &name=TfToken())
: UsdSchemaBase(prim),
_name(name)
{
}
/// Construct a UsdGeomCollectionAPI with the given \p name on the
/// prim held by \p schemaObj .
///
explicit UsdGeomCollectionAPI(const UsdSchemaBase& schemaObj,
const TfToken &name)
: UsdSchemaBase(schemaObj.GetPrim()),
_name(name)
{
}
/// Destructor
USDGEOM_API
virtual ~UsdGeomCollectionAPI();
protected:
/// Returns true if the relationship representing the collection is valid.
USDGEOM_API
virtual bool _IsCompatible() const override;
public:
/// \anchor UsdGeomCollectionAPI_PropertyValueAPI
/// \name Collection Property Value Getters and Setters
///
/// Convenience API for getting and setting values of the various
/// collection properties.
///
/// @{
/// Returns the name of the collection.
///
TfToken GetCollectionName() const {
return _name;
}
/// Returns true if the collection has no targets.
///
USDGEOM_API
bool IsEmpty() const;
/// Sets the paths to target objects that belong to the collection.
///
USDGEOM_API
bool SetTargets(const SdfPathVector &targets) const;
/// Returns the <b>unresolved paths</b> to target objects belonging to the
/// collection.
///
/// Since a collection can include a relationship, no relationship
/// forwarding is performed by the method. i.e., if the collection targets
/// a relationship, the target relationship is returned (and not the
/// ultimate targets of the target relationship).
///
/// \sa UsdRelationship::GetTargets
///
USDGEOM_API
bool GetTargets(SdfPathVector *targets) const;
/// Sets the targetFaceCounts property of the collection at the given
/// \p time. Returns true if the value was authored successfully, false
/// otherwise.
///
/// If the collection restricts to a face-set for any of its targets, then
/// "targetFaceCounts" specifies the number of faces included in the
/// various targets. The number of entries in \p targetFaceCounts should
/// always match the number of targets. If a target does not have
/// a face restriction, then it's count is set to 0, to indicate that
/// the entire prim is included in the collection.
///
/// \sa UsdGeomCollectionAPI::GetTargetFaceCounts()
///
USDGEOM_API
bool SetTargetFaceCounts(const VtIntArray &targetFaceCounts,
const UsdTimeCode &time=UsdTimeCode::Default()) const;
/// Retrieves the targetFaceCounts property value at the given \p time.
/// Returns false if no value is authored or if the "targetFaceCounts"
/// property does not exist.
///
/// \sa UsdGeomCollectionAPI::SetTargetFaceCounts()
/// \sa UsdGeomCollectionAPI::GetFaceIndices()
///
USDGEOM_API
bool GetTargetFaceCounts(VtIntArray *targetFaceCounts,
const UsdTimeCode &time=UsdTimeCode::Default()) const;
/// Sets the list of face indices belonging to the targets of the collection
/// that have a face-restriction. The total number of face indices should
/// be equal to the sum of all entires in the associated "targetFaceCounts"
/// value.
///
/// Returns true if the value was authored successfully, false
/// otherwise.
///
/// \sa UsdGeomCollectionAPI::GetTargetFaceIndices()
/// \sa UsdGeomCollectionAPI::SetTargetFaceCounts()
USDGEOM_API
bool SetTargetFaceIndices(const VtIntArray &targetFaceIndices,
const UsdTimeCode &time=UsdTimeCode::Default()) const;
/// Retrieves the targetFaceCounts property value at the given \p time.
/// Returns false if no value is authored or if the "targetFaceIndices"
/// property does not exist.
///
/// \sa UsdGeomCollectionAPI::SetTargetFaceIndices()
///
USDGEOM_API
bool GetTargetFaceIndices(VtIntArray *targetFaceIndices,
const UsdTimeCode &time=UsdTimeCode::Default()) const;
/// Adds a new target, \p target to the collection. The list of
/// face indices in the array, \p faceIndices is used to specify a
/// face-restriction on the target at the given time, \p time.
///
/// Returns true only upon success.
///
/// Here are a few things worth noting about this method:
///
/// \li The target face-count is gleaned from the length of the
/// \p faceIndices array.
///
/// \li If \p faceIndices is empty and there is an existing value for
/// "targetFaceCounts", then 0 is appended to the list of target face-counts
/// to indicate that the entire target is included in the collection.
///
/// \li If \p faceIndices is empty and the collection does have not a value
/// for the "targetFaceCounts" property, then only the target is appended.
/// targetFaceCounts and targetFaceIndices are not authored (or even created)
/// in this case.
///
USDGEOM_API
bool AddTarget(const SdfPath &target,
const VtIntArray &faceIndices=VtIntArray(),
const UsdTimeCode &time=UsdTimeCode::Default()) const;
/// @}
/// \anchor UsdGeomCollectionAPI_RawProperties
/// \name Collection Property API
///
/// API for getting and creating the "raw" properties associated with a
/// collection.
///
/// @{
/// Returns the "targetFaceCounts" attribute associated with the collection.
///
/// \n C++ Type: VtIntArray
/// \n Usd Type: SdfValueTypeNames->IntArray
/// \n Variability: SdfVariabilityVarying
///
/// \sa GetTargetFaceCounts()
///
USDGEOM_API
UsdAttribute GetTargetFaceCountsAttr() const;
/// Creates the "targetFaceCounts" attribute associated with the collection.
///
/// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
/// If specified, author \p defaultValue as the attribute's default,
/// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
/// the default for \p writeSparsely is \c false.
///
/// \sa GetTargetFaceCountsAttr()
///
USDGEOM_API
UsdAttribute CreateTargetFaceCountsAttr(const VtValue &defaultValue=VtValue(),
bool writeSparsely=false) const;
/// Returns the "targetFaceIndices" attribute associated with the collection.
///
/// \n C++ Type: VtIntArray
/// \n Usd Type: SdfValueTypeNames->IntArray
/// \n Variability: SdfVariabilityVarying
///
/// \sa GetTargetFaceIndices()
///
USDGEOM_API
UsdAttribute GetTargetFaceIndicesAttr() const;
/// Creates the "targetFaceIndices" attribute associated with the collection.
///
/// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
/// If specified, author \p defaultValue as the attribute's default,
/// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
/// the default for \p writeSparsely is \c false.
///
/// \sa GetFaceIndicesAttr()
///
USDGEOM_API
UsdAttribute CreateTargetFaceIndicesAttr(const VtValue &defaultValue=VtValue(),
bool writeSparsely=false) const;
/// Returns the relationship that targets the prims included in the
/// collection.
///
/// \sa GetTargets()
///
USDGEOM_API
UsdRelationship GetTargetsRel() const;
/// Creates the relationship that targets the prims included in the
/// collection.
///
/// \sa GetTargetsRel()
///
USDGEOM_API
UsdRelationship CreateTargetsRel() const;
/// @}
/// \anchor UsdGeomCollectionAPI_StaticHelpers
/// \name Static API
///
/// Convenience API for creating a collection on a prim and for retrieving
/// all the collections on a prim.
///
/// @{
/// Creates a new collection on the given \p prim with the given \p name.
///
/// If \p targets, \p targetFaceCounts and \p targetFaceIndices are set
/// if specified. No validation is performed on the values passed in.
///
/// If a collection already exists with the given name, it's targets
/// are reset to the specified set of targets, if \p targets is non-empty.
///
USDGEOM_API
static UsdGeomCollectionAPI Create(
const UsdPrim &prim,
const TfToken &name,
const SdfPathVector &targets=SdfPathVector(),
const VtIntArray &targetFaceCounts=VtIntArray(),
const VtIntArray &targetFaceIndices=VtIntArray());
/// \overload
/// Creates a new collection with the given \p name on the prim held in
/// the given \p schemaObj.
///
/// If \p targets, \p targetFaceCounts and \p targetFaceIndices are set
/// if specified. No validation is performed on the values passed in.
///
/// If a collection already exists with the given name, it's targets
/// are reset to the specified set of targets, if \p targets is non-empty.
///
USDGEOM_API
static UsdGeomCollectionAPI Create(
const UsdSchemaBase &schemaObj,
const TfToken &name,
const SdfPathVector &targets=SdfPathVector(),
const VtIntArray &targetFaceCounts=VtIntArray(),
const VtIntArray &targetFaceIndices=VtIntArray());
/// Returns the list of all collections on the given prim, \p prim.
///
/// This will return both empty and non-empty collections.
///
USDGEOM_API
static std::vector<UsdGeomCollectionAPI> GetCollections(const UsdPrim &prim);
/// Returns the list of all face-sets on the prim held by \p schemaObj.
USDGEOM_API
static std::vector<UsdGeomCollectionAPI> GetCollections(
const UsdSchemaBase &schemaObj);
/// @}
/// \anchor UsdGeomCollectionAPI_Validation
/// \name Collection Validation API
///
/// API for validating the properties of a collection.
///
/// @{
/// Validates the properties belonging to the collection. Returns true
/// if the collection has all valid properties. Returns false and
/// populates the \p reason output argument if the collection is invalid.
///
/// Here's the list of validations performed by this method:
/// \li A collection is considered to be invalid if it has no data authored.
/// i.e. when the collection relationship does not exist.
/// \li The number of entries in "targetFaceCounts" should match the number
/// of targets in the collection over all timeSamples.
/// \li The sum all values in the "targetFaceCounts" array should be equal
/// to the length of the "targetFaceIndices" array over all timeSamples.
///
USDGEOM_API
bool Validate(std::string *reason) const;
/// @}
private:
// Returns the collection:<name relationship.
UsdRelationship _GetTargetsRel(bool create=false) const;
// Returns the collection:<name>:targetFaceCounts attribute.
UsdAttribute _GetTargetFaceCountsAttr(bool create=false) const;
// Returns the collection:<name>:targetFaceIndices attribute.
UsdAttribute _GetTargetFaceIndicesAttr(bool create=false) const;
// Returns the name of the property belonging to this collection, given the
// base name of the attribute. Eg, if baseName is 'targetFaceCounts', this
// returns 'collection:<name>:targetFaceCounts'.
TfToken _GetCollectionPropertyName(const TfToken &baseName=TfToken()) const;
// The name of the collection.
TfToken _name;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif