HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
flattenUtils.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_USD_FLATTEN_UTILS_H
8 #define PXR_USD_USD_FLATTEN_UTILS_H
9 
10 /// \file usd/flattenUtils.h
11 ///
12 /// Utilities for flattening layer stacks into a single layer.
13 
14 #include "pxr/pxr.h"
15 #include "pxr/usd/usd/api.h"
16 #include "pxr/usd/usd/stage.h"
19 
21 
23 
24 /// Flatten \p layerStack into a single layer with the given optional \p tag.
25 ///
26 /// A composed UsdStage created from this flattened layer will be the same
27 /// as a composed UsdStage whose root layer stack is the original layer stack.
28 ///
29 /// Unlike UsdStage::Flatten(), this function does not flatten
30 /// composition arcs, such as references, payloads, inherits,
31 /// specializes, or variants.
32 ///
33 /// Sublayer time offsets on the sublayers will be applied to remap
34 /// any time-keyed scene description, such as timeSamples and clips.
35 ///
36 /// Asset paths will be resolved to absolute form, to ensure that
37 /// they continue to identify the same asset from the output layer.
38 ///
39 /// Asset paths containing stage variable expressions will be evaluated
40 /// using the variables from the root and session layer of \p layerStack
41 /// before being resolved. \sa UsdFlattenLayerStackResolveAssetPath
42 ///
43 /// A few historical scene description features cannot be flattened
44 /// into a single opinion because they unfortunately encode
45 /// operations that are not closed under composition. Specifically,
46 /// the SdfListOp operations "add" and "reorder" cannot be flattened.
47 /// Instead, "add" will be converted to "append", and "reorder"
48 /// will be discarded.
49 ///
50 USD_API
52 UsdFlattenLayerStack(const PcpLayerStackRefPtr &layerStack,
53  const std::string& tag = std::string());
54 
55 /// Callback function for overloaded version of \c UsdFlattenLayerStack.
56 ///
57 /// The callback is given the \c sourceLayer and the \c assetPath authored in
58 /// that layer. It should return the \c std::string that should be authored in
59 /// the flattened layer.
60 ///
61 /// \sa UsdFlattenLayerStackResolveAssetPath
62 using UsdFlattenResolveAssetPathFn = std::function<std::string(
63  const SdfLayerHandle& sourceLayer,
64  const std::string& assetPath)>;
65 
66 /// Flatten the \p layerStack into a single layer with the given optional \p tag
67 /// and using the \p resolveAssetPathFn to resolve asset paths that are
68 /// encountered.
69 ///
70 /// This is an advanced version of the above function.
71 ///
72 /// One use case for this version of the function is to flatten a layer stack
73 /// that contains relative asset paths that we want to preserve as relative
74 /// paths. For example:
75 ///
76 /// \code
77 /// /source/root.usd # sublayers a.usd and b.usd
78 /// /source/a.usd # contains reference to ./subdir/layer.usd
79 /// /source/b.usd
80 /// /source/subdir/layer.usd
81 /// \endcode
82 ///
83 /// We may want to generate \c "/dest/root.flat.usd" knowing that we will
84 /// (by some other means) also be copying \c "/source/subdir" into \c
85 /// "/dest/subdir". It's useful then to preserve the relative paths.
86 ///
87 /// Note, only the caller knows the ultimate destination of the flattened layer.
88 /// So to accomplish this, we can provide a \c resolveAssetPathFn callback that
89 /// captures the outputDir, tests if the authored path is relative, and if so,
90 /// computes a new relative path (based on where it will eventually be
91 /// exported).
92 ///
93 /// Asset paths containing stage variable expressions will be evaluated using
94 /// the variables from the root and session layer of \p layerStack. The
95 /// evaluated asset path will be passed to the \p resolveAssetPathFn callback
96 /// instead of the original asset path.
97 /// \sa UsdFlattenLayerStackResolveAssetPath.
98 USD_API
100 UsdFlattenLayerStack(const PcpLayerStackRefPtr &layerStack,
101  const UsdFlattenResolveAssetPathFn& resolveAssetPathFn,
102  const std::string& tag = std::string());
103 
104 /// Implements the default asset path flattening behavior for
105 /// \c UsdFlattenLayerStack. \p assetPath will be anchored to \p sourceLayer
106 /// by calling SdfComputeAssetPathRelativeToLayer. This function assumes
107 /// that \p assetPath does not contain a stage variable expression.
108 USD_API
109 std::string
111  const SdfLayerHandle& sourceLayer,
112  const std::string& assetPath);
113 
114 /// \class UsdFlattenResolveAssetPathContext
115 /// Context object containing information used when resolving asset paths
116 /// during layer stack flattening.
118 {
119 public:
120  /// Layer where the asset path is authored
121  SdfLayerHandle sourceLayer;
122 
123  /// Authored asset path
124  std::string assetPath;
125 
126  /// Expression variables from the layer stack
128 };
129 
130 /// Callback function for \c UsdFlattenLayerStack.
131 ///
132 /// The callback is given a \c UsdFlattenResolveAssetPathContext containing
133 /// information needed to resolve a given asset path. It should return the
134 /// \c std::string that should be authored in the flattened layer.
135 ///
136 /// \sa UsdFlattenLayerStack
137 using UsdFlattenResolveAssetPathAdvancedFn = std::function<
138  std::string(const UsdFlattenResolveAssetPathContext&)>;
139 
140 /// Flatten the \p layerStack into a single layer with the given optional \p tag
141 /// and using the \p resolveAssetPathFn to resolve asset paths that are
142 /// encountered.
143 ///
144 /// This is an advanced version of \c UsdFlattenLayerStack that provides full
145 /// control over how asset paths are resolved during flattening via the
146 /// \p resolveAssetPathFn callback. For example, the callback might maintain
147 /// relative asset paths instead of resolving them to absolute form. As
148 /// another example, the callback might maintain stage variable expressions
149 /// in their unevaluated form.
150 USD_API
153  const PcpLayerStackRefPtr &layerStack,
154  const UsdFlattenResolveAssetPathAdvancedFn& resolveAssetPathFn,
155  const std::string& tag = std::string());
156 
157 /// Implements the default asset path flattening behavior for
158 /// \c UsdFlattenLayerStack. The asset path in \p context will be anchored to
159 /// the source layer by calling SdfComputeAssetPathRelativeToLayer. If the
160 /// asset path contains a stage variable expression, it will be evaluated using
161 /// the expression variables in \p context before being anchored.
162 USD_API
163 std::string
165  const UsdFlattenResolveAssetPathContext& context);
166 
168 
169 #endif /* PXR_USD_USD_FLATTEN_UTILS_H */
SdfLayerHandle sourceLayer
Layer where the asset path is authored.
Definition: flattenUtils.h:121
Definition: layer.h:81
#define USD_API
Definition: api.h:23
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
USD_API SdfLayerRefPtr UsdFlattenLayerStack(const PcpLayerStackRefPtr &layerStack, const std::string &tag=std::string())
std::string assetPath
Authored asset path.
Definition: flattenUtils.h:124
std::function< std::string(const UsdFlattenResolveAssetPathContext &)> UsdFlattenResolveAssetPathAdvancedFn
Definition: flattenUtils.h:138
VtDictionary expressionVariables
Expression variables from the layer stack.
Definition: flattenUtils.h:127
USD_API std::string UsdFlattenLayerStackResolveAssetPathAdvanced(const UsdFlattenResolveAssetPathContext &context)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USD_API std::string UsdFlattenLayerStackResolveAssetPath(const SdfLayerHandle &sourceLayer, const std::string &assetPath)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::function< std::string(const SdfLayerHandle &sourceLayer, const std::string &assetPath)> UsdFlattenResolveAssetPathFn
Definition: flattenUtils.h:64