forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayer.h
1654 lines (1367 loc) · 64.9 KB
/
layer.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 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 SDF_LAYER_H
#define SDF_LAYER_H
/// \file sdf/layer.h
#include "pxr/usd/sdf/data.h"
#include "pxr/usd/sdf/declareHandles.h"
#include "pxr/usd/sdf/identity.h"
#include "pxr/usd/sdf/layerBase.h"
#include "pxr/usd/sdf/layerOffset.h"
#include "pxr/usd/sdf/namespaceEdit.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/usd/sdf/proxyTypes.h"
#include "pxr/usd/sdf/spec.h"
#include "pxr/usd/sdf/types.h"
#include "pxr/usd/ar/assetInfo.h"
#include "pxr/base/tf/declarePtrs.h"
#include "pxr/base/vt/value.h"
#include <boost/optional.hpp>
#include <boost/scoped_ptr.hpp>
#include <mutex>
#include <set>
#include <string>
#include <vector>
TF_DECLARE_WEAK_PTRS(SdfFileFormat);
TF_DECLARE_WEAK_AND_REF_PTRS(SdfLayerStateDelegateBase);
struct Sdf_AssetInfo;
/// \class SdfLayer
///
/// A unit of scene description that you combine with other units of scene
/// description to form a shot, model, set, shader, and so on.
///
/// SdfLayer objects provide a persistent way to store layers on the
/// filesystem in .menva files. Currently the supported file format is
/// <c>.menva</c>, the ASCII file format.
///
/// The FindOrOpen() method returns a new SdfLayer object with scene
/// description from a <c>.menva</c> file. Once read, a layer remembers which
/// asset it was read from. The Save() method saves the layer back out to the
/// original file. You can use the Export() method to write the layer to a
/// different location. You can use the GetIdentifier() method to get the layer's
/// Id or GetRealPath() to get the resolved, full file path.
///
/// Layers can have a timeCode range (startTimeCode and endTimeCode). This range
/// represents the suggested playback range, but has no impact on the extent of
/// the animation data that may be stored in the layer. The metadatum
/// "timeCodesPerSecond" is used to annotate how the time ordinate for samples
/// contained in the file scales to seconds. For example, if timeCodesPerSecond
/// is 24, then a sample at time ordinate 24 should be viewed exactly one second
/// after the sample at time ordinate 0.
///
/// Compared to Menv2x, layers are most closely analogous to
/// hooksets, <c>.hook</c> files, and <c>.cue</c> files.
///
/// \todo
/// \li Insert a discussion of subLayers semantics here.
///
/// \todo
/// \li Should have validate... methods for rootPrims
///
class SdfLayer : public SdfLayerBase
{
typedef SdfLayerBase Parent;
typedef SdfLayer This;
public:
/// Destructor.
virtual ~SdfLayer();
///
/// \name Primary API
/// @{
using SdfLayerBase::FileFormatArguments;
/// Creates a new empty layer with the given identifier.
///
/// The \p identifier must be either a real filesystem path or an asset
/// path without version modifier. Attempting to create a layer using an
/// identifier with a version specifier (e.g. layer.menva@300100,
/// layer.menva#5) raises a coding error, and returns a null layer
/// pointer.
///
/// Additional arguments may be supplied via the \p args parameter.
/// These arguments may control behavior specific to the layer's
/// file format.
static SdfLayerRefPtr CreateNew(const std::string &identifier,
const std::string &realPath = std::string(),
const FileFormatArguments &args =
FileFormatArguments());
/// Creates a new empty layer with the given identifier for a given file
/// format class.
///
/// This function has the same behavior as the other CreateNew function,
/// but uses the explicitly-specified \p fileFormat instead of attempting
/// to discern the format from \p identifier.
static SdfLayerRefPtr CreateNew(const SdfFileFormatConstPtr& fileFormat,
const std::string &identifier,
const std::string &realPath = std::string(),
const FileFormatArguments &args =
FileFormatArguments());
/// Creates a new empty layer with the given identifier for a given file
/// format class.
///
/// This is so that Python File Format classes can create layers
/// (CreateNew() doesn't work, because it already saves during
/// construction of the layer. That is something specific (python
/// generated) layer types may choose to not do.)
///
/// The new layer will not be dirty.
///
/// Additional arguments may be supplied via the \p args parameter.
/// These arguments may control behavior specific to the layer's
/// file format.
static SdfLayerRefPtr New(const SdfFileFormatConstPtr& fileFormat,
const std::string &identifier,
const std::string &realPath = std::string(),
const FileFormatArguments &args =
FileFormatArguments());
/// Returns the layer for the given path if found in the layer registry.
/// If the layer cannot be found, a null handle is returned.
static SdfLayerHandle Find(
const std::string &identifier,
const FileFormatArguments &args = FileFormatArguments());
/// Returns the layer for \p layerPath, assumed to be relative to the path
/// of the \p anchor layer. If the \p anchor layer is invalid, a coding
/// error is raised, and a null handle is returned. If \p layerPath is not
/// relative, this method is equivalent to \c Find(layerPath).
static SdfLayerHandle FindRelativeToLayer(
const SdfLayerHandle &anchor,
const std::string &layerPath,
const FileFormatArguments &args = FileFormatArguments());
/// Return an existing layer with the given \p identifier and \p args, or
/// else load it from disk. If the layer can't be found or loaded,
/// an error is posted and a null layer is returned.
///
/// Arguments in \p args will override any arguments specified in
/// \p identifier.
static SdfLayerRefPtr FindOrOpen(
const std::string &identifier,
const FileFormatArguments &args = FileFormatArguments());
/// Load the given layer from disk as a new anonymous layer. If the
/// layer can't be found or loaded, an error is posted and a null
/// layer is returned.
///
/// The anonymous layer does not retain any knowledge of the backing
/// file on the filesystem.
///
/// \p metadataOnly is a flag that asks for only the layer metadata
/// to be read in, which can be much faster if that is all that is
/// required. Note that this is just a hint: some FileFormat readers
/// may disregard this flag and still fully populate the layer contents.
static SdfLayerRefPtr OpenAsAnonymous(
const std::string &layerPath,
bool metadataOnly = false);
/// Returns the scene description schema for this layer.
virtual const SdfSchemaBase& GetSchema() const;
/// Returns the data from the absolute root path of this layer.
SdfDataRefPtr GetMetadata() const;
/// Returns handles for all layers currently held by the layer registry.
static SdfLayerHandleSet GetLoadedLayers();
/// Returns whether this layer has no significant data.
bool IsEmpty() const;
/// Copies the content of the given layer into this layer.
/// Source layer is unmodified.
void TransferContent(const SdfLayerHandle& layer);
/// Creates a new \e anonymous layer with an optional \p tag. An anonymous
/// layer is a layer with a system assigned identifier, that cannot be
/// saved to disk via Save(). Anonymous layers have an identifier, but no
/// repository, overlay, real path, or other asset information fields.
/// Anonymous layers may be tagged, which can be done to aid debugging
/// subsystems that make use of anonymous layers. The tag becomes the
/// display name of an anonymous layer. Untagged anonymous layers have an
/// empty display name.
static SdfLayerRefPtr CreateAnonymous(
const std::string& tag = std::string());
/// Returns true if this layer is an anonymous layer.
bool IsAnonymous() const;
/// Returns true if the \p identifier is an anonymous layer unique
/// identifier.
static bool IsAnonymousLayerIdentifier(const std::string& identifier);
/// Returns the display name for the given \p identifier, using the same
/// rules as GetDisplayName.
static std::string GetDisplayNameFromIdentifier(
const std::string& identifier);
/// @}
/// \name File I/O
/// @{
/// Converts \e layerPath to a file system path.
static std::string ComputeRealPath(const std::string &layerPath);
/// Returns \c true if successful, \c false if an error occurred.
/// Returns \c false if the layer has no remembered file name or the
/// layer type cannot be saved.
bool Save() const;
/// Exports this layer to a file.
/// Returns \c true if successful, \c false if an error occurred.
///
/// If \p comment is not empty, the layer gets exported with the given
/// comment. Additional arguments may be supplied via the \p args parameter.
/// These arguments may control behavior specific to the exported layer's
/// file format.
///
/// Note that the file name or comment of the original layer is not
/// updated. This only saves a copy of the layer to the given filename.
/// Subsequent calls to Save() will still save the layer to it's
/// previously remembered file name.
bool Export(const std::string& filename,
const std::string& comment = std::string(),
const FileFormatArguments& args = FileFormatArguments()) const;
/// Writes this layer to the given string.
///
/// Returns \c true if successful and sets \p result, otherwise
/// returns \c false.
bool ExportToString(std::string* result) const;
/// Reads this layer from the given string.
///
/// Returns \c true if successful, otherwise returns \c false.
bool ImportFromString(const std::string &string);
/// Clears the layer of all content.
///
/// This restores the layer to a state as if it had just been created
/// with CreateNew(). This operation is Undo-able.
///
/// The fileName and whether journaling is enabled are not affected
/// by this method.
void Clear();
/// Reloads the layer from its persistent representation.
///
/// This restores the layer to a state as if it had just been created
/// with FindOrOpen(). This operation is Undo-able.
///
/// The fileName and whether journaling is enabled are not affected
/// by this method.
///
/// When called with force = false (the default), Reload attempts to
/// avoid reloading layers that have not changed on disk. It does so
/// by comparing the file's modification time (mtime) to when the
/// file was loaded. If the layer has unsaved modifications, this
/// mechanism is not used, and the layer is reloaded from disk.
///
/// Passing true to the \p force parameter overrides this behavior,
/// forcing the layer to be reloaded from disk regardless of whether
/// it has changed.
bool Reload(bool force = false);
/// Reloads the specified layers.
///
/// Returns \c false if one or more layers failed to reload.
///
/// See \c Reload() for a description of the \p force flag.
///
static bool ReloadLayers(const std::set<SdfLayerHandle>& layers,
bool force = false);
/// Imports the content of the given layer path, replacing the content
/// of the current layer.
/// Note: If the layer path is the same as the current layer's real path,
/// no action is taken (and a warning occurs). For this case use
/// Reload().
bool Import(const std::string &layerPath);
/// @}
/// \name External references
/// @{
/// Return paths of all external references of layer.
std::set<std::string> GetExternalReferences();
/// Updates the external references of the layer.
///
/// If only the old asset path is given, this update works as delete,
/// removing any sublayers or prims referencing the pathtype using the
/// old asset path as reference.
///
/// If new asset path is supplied, the update works as "rename", updating
/// any occurence of the old reference to the new reference.
bool UpdateExternalReference(
const std::string &oldAssetPath,
const std::string &newAssetPath=std::string());
/// @}
/// \name Identification
///
/// A layer's identifier is a string that uniquely identifies a layer.
/// At minimum, it is the string by which the layer was created, either
/// via FindOrOpen or CreateNew. If additional arguments were passed
/// to those functions, those arguments will be encoded in the identifier.
///
/// For example:
/// FindOrOpen('foo.sdf', args={'a':'b', 'c':'d'}).identifier
/// => "foo.sdf?a=b&c=d"
///
/// Note that this means the identifier may in general not be a path.
///
/// The identifier format is subject to change; consumers should NOT
/// parse layer identifiers themselves, but should use the supplied
/// SplitIdentifier and CreateIdentifier helper functions.
///
/// @{
/// Splits the given layer identifier into its constituent layer path
/// and arguments.
static bool SplitIdentifier(
const std::string& identifier,
std::string* layerPath,
FileFormatArguments* arguments);
/// Joins the given layer path and arguments into an identifier.
static std::string CreateIdentifier(
const std::string& layerPath,
const FileFormatArguments& arguments);
/// Returns the layer identifier.
const std::string& GetIdentifier() const;
/// Sets the layer identifier.
/// Note that the new identifier must have the same arguments (if any)
/// as the old identifier.
void SetIdentifier(const std::string& identifier);
/// Update layer asset information. Calling this method re-resolves the
/// layer identifier, which updates asset information such as the layer
/// file revision, real path, and repository path. If \p fileVersion is
/// supplied, it is used as the layer version if the identifier does not
/// have a version or label specifier. This is typically used to tell Sd
/// what the version of a layer is after submitting a new revision to the
/// asset system.
void UpdateAssetInfo(const std::string& fileVersion = std::string());
/// Returns the layer's display name.
///
/// The display name is the base filename of the identifier.
std::string GetDisplayName() const;
/// Returns the file system path where this layer exists or may exist
/// after a call to Save.
const std::string& GetRealPath() const;
/// Returns the file extension to use for this layer.
/// If this layer was loaded from disk, it should match the extension
/// of the file format it was loaded as; if this is an anonymous
/// in-memory layer it will be the default extension.
std::string GetFileExtension() const;
/// Returns the asset system version of this layer. If a layer is loaded
/// from a location that is not version managed, or a configured asset
/// system is not present when the layer is loaded or created, the version
/// is empty. By default, asset version tracking is disabled; this method
/// returns empty unless asset version tracking is enabled.
const std::string& GetVersion() const;
/// Returns the layer identifier in asset path form. In the presence of a
/// properly configured path resolver, the asset path is a double-slash
/// prefixed depot path. If the path resolver is not configured, the asset
/// path of a layer is empty.
const std::string& GetRepositoryPath() const;
/// Returns the asset name associated with this layer.
const std::string& GetAssetName() const;
/// Returns resolve information from the last time the layer identifier
/// was resolved.
const VtValue& GetAssetInfo() const;
/// Make the given \p relativePath absolute using the identifier of this
/// layer. If this layer does not have an identifier, or if the layer
/// identifier is itself relative, \p relativePath is returned unmodified.
std::string ComputeAbsolutePath(const std::string &relativePath);
/// @}
/// \name Fields
///
/// All scene description for a given object is stored as a set of
/// key/value pairs called fields. These methods provide direct access to
/// those fields, though most clients should use the Spec API to ensure
/// data consistency.
///
/// These methods all take SdfAbstractDataSpecId to identify the spec
/// being queried. Note that these are implicitly convertible from
/// SdPath.
///
/// @{
/// Return the specifiers for \a id. This returns default constructed
/// specifiers if no spec exists at \a id.
SdfSpecType GetSpecType(const SdfAbstractDataSpecId& id) const;
/// Return whether a spec exists at \a id.
bool HasSpec(const SdfAbstractDataSpecId& id) const;
/// Return the names of all the fields that are set at \p id.
std::vector<TfToken> ListFields(const SdfAbstractDataSpecId& id) const;
/// Return whether a value exists for the given \a id and \a fieldName.
/// Optionally returns the value if it exists.
bool HasField(const SdfAbstractDataSpecId& id, const TfToken& fieldName,
VtValue *value=NULL) const;
bool HasField(const SdfAbstractDataSpecId& id, const TfToken& fieldName,
SdfAbstractDataValue *value) const;
/// Returns \c true if the object has a non-empty value with name
/// \p name and type \p T. If value ptr is provided, returns the
/// value found.
template <class T>
bool HasField(const SdfAbstractDataSpecId& id, const TfToken &name,
T* value) const
{
if (not value) {
return HasField(id, name, static_cast<VtValue *>(NULL));
}
SdfAbstractDataTypedValue<T> outValue(value);
const bool hasValue = HasField(
id, name, static_cast<SdfAbstractDataValue *>(&outValue));
if (std::is_same<T, SdfValueBlock>::value) {
return hasValue and outValue.isValueBlock;
}
return hasValue and (not outValue.isValueBlock);
}
/// Return whether a value exists for the given \a id and \a fieldName and
/// \a keyPath. The \p keyPath is a ':'-separated path addressing an
/// element in sub-dictionaries. Optionally returns the value if it exists.
bool HasFieldDictKey(const SdfAbstractDataSpecId& id,
const TfToken &fieldName,
const TfToken &keyPath,
VtValue *value=NULL) const;
bool HasFieldDictKey(const SdfAbstractDataSpecId& id,
const TfToken &fieldName,
const TfToken &keyPath,
SdfAbstractDataValue *value) const;
/// Returns \c true if the object has a non-empty value with name \p name
/// and \p keyPath and type \p T. If value ptr is provided, returns the
/// value found. The \p keyPath is a ':'-separated path addressing an
/// element in sub-dictionaries.
template <class T>
bool HasFieldDictKey(const SdfAbstractDataSpecId& id, const TfToken &name,
const TfToken &keyPath, T* value) const
{
if (not value) {
return HasFieldDictKey(id, name, keyPath,
static_cast<VtValue *>(NULL));
}
SdfAbstractDataTypedValue<T> outValue(value);
return HasFieldDictKey(id, name, keyPath,
static_cast<SdfAbstractDataValue *>(&outValue));
}
/// Return the value for the given \a id and \a fieldName. Returns an
/// empty value if none is set.
VtValue GetField(const SdfAbstractDataSpecId& id,
const TfToken& fieldName) const;
/// Return the value for the given \a id and \a fieldName. Returns the
/// provided \a defaultValue value if none is set.
template <class T>
inline T GetFieldAs(const SdfAbstractDataSpecId& id,
const TfToken& fieldName, const T& defaultValue = T()) const
{
return _data->GetAs<T>(id, fieldName, defaultValue);
}
/// Return the value for the given \a id and \a fieldName at \p
/// keyPath. Returns an empty value if none is set. The \p keyPath is a
/// ':'-separated path addressing an element in sub-dictionaries.
VtValue GetFieldDictValueByKey(const SdfAbstractDataSpecId& id,
const TfToken& fieldName,
const TfToken& keyPath) const;
/// Set the value of the given \a id and \a fieldName.
void SetField(const SdfAbstractDataSpecId& id, const TfToken& fieldName,
const VtValue& value);
void SetField(const SdfAbstractDataSpecId& id, const TfToken& fieldName,
const SdfAbstractDataConstValue& value);
/// Set the value of the given \a id and \a fieldName.
template <class T>
void SetField(const SdfAbstractDataSpecId& id, const TfToken& fieldName,
const T& val)
{
// Ideally, this would make use of the SdfAbstractDataConstValue
// API to avoid unnecessarily copying the value into a VtValue.
// However, Sdf needs to create a VtValue for change processing.
// If the underlying SdAbstractData implementation also needs a
// VtValue, using the SdfAbstractDataConstValue API would cause
// another copy to be made. So, it's more efficient to just create
// the VtValue once here and push that along.
SetField(id, fieldName, VtValue(val));
}
/// Set the value of the given \a id and \a fieldName. The \p keyPath is a
/// ':'-separated path addressing an element in sub-dictionaries.
void SetFieldDictValueByKey(const SdfAbstractDataSpecId& id,
const TfToken& fieldName,
const TfToken& keyPath,
const VtValue& value);
void SetFieldDictValueByKey(const SdfAbstractDataSpecId& id,
const TfToken& fieldName,
const TfToken& keyPath,
const SdfAbstractDataConstValue& value);
/// Set the value of the given \a id and \a fieldName. The \p keyPath is a
/// ':'-separated path addressing an element in sub-dictionaries.
template <class T>
void SetFieldDictValueByKey(const SdfAbstractDataSpecId& id,
const TfToken& fieldName,
const TfToken& keyPath,
const T& val)
{
// Ideally, this would make use of the SdfAbstractDataConstValue
// API to avoid unnecessarily copying the value into a VtValue.
// However, Sdf needs to create a VtValue for change processing.
// If the underlying SdAbstractData implementation also needs
// VtValue, using the SdfAbstractDataConstValue API would cause
// another copy to be made. So, it's more efficient to just create
// the VtValue once here and push that along.
SetFieldDictValueByKey(id, fieldName, keyPath, VtValue(val));
}
/// Remove the field at \p id and \p fieldName, if one exists.
void EraseField(const SdfAbstractDataSpecId& id, const TfToken& fieldName);
/// Remove the field at \p id and \p fieldName and \p keyPath, if one
/// exists. The \p keyPath is a ':'-separated path addressing an
/// element in sub-dictionaries.
void EraseFieldDictValueByKey(const SdfAbstractDataSpecId& id,
const TfToken& fieldName,
const TfToken& keyPath);
/// Convenience API that takes an SdfPath instead of an
/// SdfAbstractDataSpecId. See documentation above for details.
SdfSpecType GetSpecType(const SdfPath& path) const;
bool HasSpec(const SdfPath& path) const;
std::vector<TfToken> ListFields(const SdfPath& path) const;
/// Return a list of the keys of the (sub) dictionary identified by
/// \p keyPath. The \p keyPath is a ':'-separated path addressing an
/// element in sub-dictionaries.
std::vector<TfToken> ListFieldDictKeys(const SdfPath& path,
const TfToken &keyPath) const;
template <class T>
bool HasField(const SdfPath& path,
const TfToken& fieldName, T* value) const;
bool HasField(const SdfPath& path, const TfToken& fieldName) const;
template <class T>
bool HasFieldDictKey(const SdfPath& path, const TfToken& fieldName,
const TfToken &keyPath, T* value) const;
bool HasFieldDictKey(const SdfPath& path, const TfToken& fieldName,
const TfToken &keyPath) const;
VtValue GetField(const SdfPath& path, const TfToken& fieldName) const;
VtValue GetFieldDictValueByKey(
const SdfPath& path, const TfToken& fieldName,
const TfToken &keyPath) const;
template <class T>
T GetFieldAs(const SdfPath& path, const TfToken& fieldName,
const T& defaultValue = T()) const;
template <class T>
void SetField(const SdfPath& path, const TfToken& fieldName, const T& val);
template <class T>
void SetFieldDictValueByKey(const SdfPath& path, const TfToken& fieldName,
const TfToken &keyPath, const T& val);
void EraseField(const SdfPath& path, const TfToken& fieldName);
void EraseFieldDictValueByKey(const SdfPath& path, const TfToken& fieldName,
const TfToken &keyPath);
/// \name Traversal
/// @{
/// Callback function for Traverse. This callback will be invoked with
/// the path of each spec that is visited.
/// \sa Traverse
typedef boost::function<void(const SdfPath&)> TraversalFunction;
// Traverse will perform a traversal of the scene description hierarchy
// rooted at \a path, calling \a func on each spec that it finds.
void Traverse(const SdfPath& path, const TraversalFunction& func);
/// @}
/// \name Metadata
/// @{
/// Returns the comment string for this layer.
///
/// The default value for comment is "".
std::string GetComment() const;
/// Sets the comment string for this layer.
void SetComment(const std::string &comment);
/// Return the defaultPrim metadata for this layer. This field
/// indicates the name of which root prim should be targeted by a reference
/// or payload to this layer that doesn't specify a prim path.
///
/// The default value is the empty token.
TfToken GetDefaultPrim() const;
/// Set the default prim metadata for this layer. The root prim with this
/// name will be targeted by a reference or a payload to this layer that
/// doesn't specify a prim path. Note that this must be a root prim
/// <b>name</b> not a path. E.g. "rootPrim" rather than "/rootPrim". See
/// GetDefaultPrim().
void SetDefaultPrim(const TfToken &name);
/// Clear the default prim metadata for this layer. See GetDefaultPrim()
/// and SetDefaultPrim().
void ClearDefaultPrim();
/// Return true if the default prim metadata is set in this layer. See
/// GetDefaultPrim() and SetDefaultPrim().
bool HasDefaultPrim();
/// Returns the documentation string for this layer.
///
/// The default value for documentation is "".
std::string GetDocumentation() const;
/// Sets the documentation string for this layer.
void SetDocumentation(const std::string &documentation);
/// Returns the layer's start timeCode.
///
/// The start and end timeCodes of a layer represent the suggested playback
/// range. However, time-varying content is not limited to the timeCode range
/// of the layer.
///
/// The default value for startTimeCode is 0.
double GetStartTimeCode() const;
/// Sets the layer's start timeCode.
void SetStartTimeCode(double startTimecode);
/// Returns true if the layer has a startTimeCode opinion.
bool HasStartTimeCode() const;
/// Clear the startTimeCode opinion.
void ClearStartTimeCode();
/// Returns the layer's end timeCode.
/// The start and end timeCode of a layer represent a suggested playback range.
/// However, time-varying content is not limited to the timeCode range of the
/// layer.
///
/// The default value for endTimeCode is 0.
double GetEndTimeCode() const;
/// Sets the layer's end timeCode.
void SetEndTimeCode(double endTimeCode);
/// Returns true if the layer has an endTimeCode opinion.
bool HasEndTimeCode() const;
/// Clear the endTimeCode opinion.
void ClearEndTimeCode();
/// Returns the layer's timeCodes per second.
///
/// Scales the time ordinate for samples contained in the file to seconds.
/// If timeCodesPerSecond is 24, then a sample at time ordinate 24 should
/// be viewed exactly one second after the sample at time ordinate 0.
///
/// The default value of timeCodesPerSecond is 24.
double GetTimeCodesPerSecond() const;
/// Sets the layer's timeCodes per second
void SetTimeCodesPerSecond(double timeCodesPerSecond);
/// Returns true if the layer has a timeCodesPerSecond opinion.
bool HasTimeCodesPerSecond() const;
/// Clear the timeCodesPerSecond opinion.
void ClearTimeCodesPerSecond();
/// Returns the layer's frames per second.
///
/// This makes an advisory statement about how the contained data can be
/// most usefully consumed and presented. It's primarily an indication of
/// the expected playback rate for the data, but a timeline editing tool
/// might also want to use this to decide how to scale and label its
/// timeline.
///
/// The default value for framesPerSecond is 24.
double GetFramesPerSecond() const;
/// Sets the layer's frames per second
void SetFramesPerSecond(double framesPerSecond);
/// Returns true if the layer has a frames per second opinion.
bool HasFramesPerSecond() const;
/// Clear the framesPerSecond opinion.
void ClearFramesPerSecond();
/// Returns the layer's frame precision.
int GetFramePrecision() const;
/// Sets the layer's frame precision.
void SetFramePrecision(int framePrecision);
/// Returns true if the layer has a frames precision opinion.
bool HasFramePrecision() const;
/// Clear the framePrecision opinion.
void ClearFramePrecision();
/// Returns the layer's owner.
std::string GetOwner() const;
/// Sets the layer's owner.
void SetOwner(const std::string& owner);
/// Returns true if the layer has an owner opinion.
bool HasOwner() const;
/// Clear the owner opinion.
void ClearOwner();
/// Returns the layer's session owner.
/// Note: This should only be used by session layers.
std::string GetSessionOwner() const;
/// Sets the layer's session owner.
/// Note: This should only be used by session layers.
void SetSessionOwner(const std::string& owner);
/// Returns true if the layer has a session owner opinion.
bool HasSessionOwner() const;
// Clear the session owner opinion.
void ClearSessionOwner();
/// Returns true if the layer's sublayers are expected to have owners.
bool GetHasOwnedSubLayers() const;
/// Sets whether the layer's sublayers are expected to have owners.
void SetHasOwnedSubLayers(bool);
/// Returns the CustomLayerData dictionary associated with this layer.
///
/// This is a dictionary is custom metadata that is associated with
/// this layer. It allows users to encode any set of information for
/// human or program consumption.
VtDictionary GetCustomLayerData() const;
/// Sets the CustomLayerData dictionary associated with this layer.
void SetCustomLayerData(const VtDictionary& value);
/// Returns true if CustomLayerData is authored on the layer.
bool HasCustomLayerData() const;
/// Clears out the CustomLayerData dictionary associated with this layer.
void ClearCustomLayerData();
/// @}
/// \name Prims
/// @{
// Type for root prims view.
typedef SdfPrimSpecView RootPrimsView;
/// Returns a vector of the layer's root prims
RootPrimsView GetRootPrims() const;
/// Sets a new vector of root prims.
/// You can re-order, insert and remove prims but cannot
/// rename them this way. If any of the listed prims have
/// an existing owner, they will be reparented.
void SetRootPrims(const SdfPrimSpecHandleVector &rootPrims);
/// Adds a new root prim at the given index.
/// If the index is -1, the prim is inserted at the end.
/// The layer will take ownership of the prim, via a TfRefPtr.
/// Returns true if succesful, false if failed (for example,
/// due to a duplicate name).
bool InsertRootPrim(const SdfPrimSpecHandle &prim, int index = -1);
/// Remove a root prim.
void RemoveRootPrim(const SdfPrimSpecHandle &prim);
/// Cause \p spec to be removed if it no longer affects the scene when the
/// last change block is closed, or now if there are no change blocks.
void ScheduleRemoveIfInert(const SdfSpec& spec);
/// Removes scene description that does not affect the scene in the
/// layer namespace beginning with \p prim.
///
/// Calling this method on a prim will only clean up prims with specifier
/// 'over' that are not contributing any opinions. The \p prim will only
/// be removed if all of its nameChildren are also inert. The hierarchy
/// \p prim is defined in will be pruned up to the layer root for each
/// successive inert parent that has specifier 'over'.
///
/// note: PrimSpecs that contain any PropertySpecs, even PropertySpecs with
/// required fields only (see PropertySpec::HasRequiredFieldsOnly)
/// are not considered inert, and thus the prim won't be removed.
void RemovePrimIfInert(SdfPrimSpecHandle prim);
/// Removes prop if it has only required fields (i.e. is not
/// contributing any opinions to the scene other than property
/// instantiation).
///
/// The hierarchy \p prop is defined in will then be pruned up to the
/// layer root for each successive inert parent.
void RemovePropertyIfHasOnlyRequiredFields(SdfPropertySpecHandle prop);
/// Removes all scene description in this layer that does not affect the
/// scene.
///
/// This method walks the layer namespace hierarchy and removes any prims
/// and that are not contributing any opinions.
void RemoveInertSceneDescription();
/// Returns the list of prim names for this layer's reorder rootPrims
/// statement.
///
/// See SetRootPrimOrder() for more info.
SdfNameOrderProxy GetRootPrimOrder() const;
/// Given a list of (possible sparse) prim names, authors a reorder
/// rootPrims statement for this prim.
///
/// This reorder statement can modify the order of root prims that have
/// already been explicitly ordered with InsertRootPrim() or SetRootPrims();
/// but only during composition. Therefore, GetRootPrims(),
/// InsertRootPrim(), SetRootPrims(), etc. do not read, author, or pay any
/// attention to this statement.
void SetRootPrimOrder(const std::vector<TfToken>& names);
/// Adds a new root prim name in the root prim order.
/// If the index is -1, the name is inserted at the end.
void InsertInRootPrimOrder(const TfToken &name, int index = -1);
/// Removes a root prim name from the root prim order.
void RemoveFromRootPrimOrder(const TfToken & name);
/// Removes a root prim name from the root prim order by index.
void RemoveFromRootPrimOrderByIndex(int index);
/// Reorders the given list of prim names according to the reorder rootPrims
/// statement for this layer.
///
/// This routine employs the standard list editing operations for ordered
/// items in a ListEditor.
void ApplyRootPrimOrder(std::vector<TfToken> *vec) const;
/// @}
/// \name Sublayers
/// @{
/// Returns a proxy for this layer's sublayers.
///
/// Sub-layers are the weaker layers directly included by this layer.
/// They're in order from strongest to weakest and they're all weaker
/// than this layer.
///
/// Edits through the proxy changes the sublayers. If this layer does
/// not have any sublayers the proxy is empty.
SdfSubLayerProxy GetSubLayerPaths() const;
/// Sets the paths of the layer's sublayers.
void SetSubLayerPaths(const std::vector<std::string>& newPaths);
/// Returns the number of sublayer paths (and offsets).
size_t GetNumSubLayerPaths() const;
/// Inserts new sublayer path at the given index.
///
/// The default index of -1 means to insert at the end.
void InsertSubLayerPath(const std::string& path, int index = -1);
/// Removes sublayer path at the given index.
void RemoveSubLayerPath(int index);
/// Returns the layer offsets for all the subLayer paths.
SdfLayerOffsetVector GetSubLayerOffsets() const;
/// Returns the layer offset for the subLayer path at the given index.
SdfLayerOffset GetSubLayerOffset(int index) const;
/// Sets the layer offset for the subLayer path at the given index.
void SetSubLayerOffset(const SdfLayerOffset& offset, int index);
/// @}
/// \name Muting
/// @{
/// Returns the set of muted layer paths.
static std::set<std::string> GetMutedLayers();
/// Returns \c true if the current layer is muted.
bool IsMuted() const;
/// Returns \c true if the specified layer path is muted.
static bool IsMuted(const std::string &path);
/// Mutes the current layer if \p muted is \c true, and unmutes it
/// otherwise.
void SetMuted(bool muted);
/// Add the specified path to the muted layers set.
static void AddToMutedLayers(const std::string &mutedPath);
/// Remove the specified path from the muted layers set.
static void RemoveFromMutedLayers(const std::string &mutedPath);
/// @}
/// \name Lookup
/// @{
/// Returns the layer's pseudo-root prim.
///
/// The layer's root prims are namespace children of the pseudo-root.
/// The pseudo-root exists to make the namespace hierarchy a tree
/// instead of a forest. This simplifies the implementation of
/// some algorithms.
///
/// A layer always has a pseudo-root prim.
SdfPrimSpecHandle GetPseudoRoot() const;
/// Returns the object at the given \p path.
///
/// There is no distinction between an absolute and relative path
/// at the SdLayer level.
///
/// Returns \c NULL if there is no object at \p path.
SdfSpecHandle GetObjectAtPath(const SdfPath &path);
/// Returns the prim at the given \p path.
///
/// Returns \c NULL if there is no prim at \p path.
/// This is simply a more specifically typed version of
/// \c GetObjectAtPath().
SdfPrimSpecHandle GetPrimAtPath(const SdfPath &path);
/// Returns a property at the given \p path.
///
/// Returns \c NULL if there is no property at \p path.
/// This is simply a more specifically typed version of