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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_USD_FLATTEN_UTILS_H
25 #define PXR_USD_USD_FLATTEN_UTILS_H
26 
27 /// \file usd/flattenUtils.h
28 ///
29 /// Utilities for flattening layer stacks into a single layer.
30 
31 #include "pxr/pxr.h"
32 #include "pxr/usd/usd/api.h"
33 #include "pxr/usd/usd/stage.h"
36 
38 
40 
41 /// Flatten \p layerStack into a single layer with the given optional \p tag.
42 ///
43 /// A composed UsdStage created from this flattened layer will be the same
44 /// as a composed UsdStage whose root layer stack is the original layer stack.
45 ///
46 /// Unlike UsdStage::Flatten(), this function does not flatten
47 /// composition arcs, such as references, payloads, inherits,
48 /// specializes, or variants.
49 ///
50 /// Sublayer time offsets on the sublayers will be applied to remap
51 /// any time-keyed scene description, such as timeSamples and clips.
52 ///
53 /// Asset paths will be resolved to absolute form, to ensure that
54 /// they continue to identify the same asset from the output layer.
55 ///
56 /// Asset paths containing stage variable expressions will be evaluated
57 /// using the variables from the root and session layer of \p layerStack
58 /// before being resolved. \sa UsdFlattenLayerStackResolveAssetPath
59 ///
60 /// A few historical scene description features cannot be flattened
61 /// into a single opinion because they unfortunately encode
62 /// operations that are not closed under composition. Specifically,
63 /// the SdfListOp operations "add" and "reorder" cannot be flattened.
64 /// Instead, "add" will be converted to "append", and "reorder"
65 /// will be discarded.
66 ///
67 USD_API
69 UsdFlattenLayerStack(const PcpLayerStackRefPtr &layerStack,
70  const std::string& tag = std::string());
71 
72 /// Callback function for overloaded version of \c UsdFlattenLayerStack.
73 ///
74 /// The callback is given the \c sourceLayer and the \c assetPath authored in
75 /// that layer. It should return the \c std::string that should be authored in
76 /// the flattened layer.
77 ///
78 /// \sa UsdFlattenLayerStackResolveAssetPath
79 using UsdFlattenResolveAssetPathFn = std::function<std::string(
80  const SdfLayerHandle& sourceLayer,
81  const std::string& assetPath)>;
82 
83 /// Flatten the \p layerStack into a single layer with the given optional \p tag
84 /// and using the \p resolveAssetPathFn to resolve asset paths that are
85 /// encountered.
86 ///
87 /// This is an advanced version of the above function.
88 ///
89 /// One use case for this version of the function is to flatten a layer stack
90 /// that contains relative asset paths that we want to preserve as relative
91 /// paths. For example:
92 ///
93 /// \code
94 /// /source/root.usd # sublayers a.usd and b.usd
95 /// /source/a.usd # contains reference to ./subdir/layer.usd
96 /// /source/b.usd
97 /// /source/subdir/layer.usd
98 /// \endcode
99 ///
100 /// We may want to generate \c "/dest/root.flat.usd" knowing that we will
101 /// (by some other means) also be copying \c "/source/subdir" into \c
102 /// "/dest/subdir". It's useful then to preserve the relative paths.
103 ///
104 /// Note, only the caller knows the ultimate destination of the flattened layer.
105 /// So to accomplish this, we can provide a \c resolveAssetPathFn callback that
106 /// captures the outputDir, tests if the authored path is relative, and if so,
107 /// computes a new relative path (based on where it will eventually be
108 /// exported).
109 ///
110 /// Asset paths containing stage variable expressions will be evaluated using
111 /// the variables from the root and session layer of \p layerStack. The
112 /// evaluated asset path will be passed to the \p resolveAssetPathFn callback
113 /// instead of the original asset path.
114 /// \sa UsdFlattenLayerStackResolveAssetPath.
115 USD_API
117 UsdFlattenLayerStack(const PcpLayerStackRefPtr &layerStack,
118  const UsdFlattenResolveAssetPathFn& resolveAssetPathFn,
119  const std::string& tag = std::string());
120 
121 /// Implements the default asset path flattening behavior for
122 /// \c UsdFlattenLayerStack. \p assetPath will be anchored to \p sourceLayer
123 /// by calling SdfComputeAssetPathRelativeToLayer. This function assumes
124 /// that \p assetPath does not contain a stage variable expression.
125 USD_API
128  const SdfLayerHandle& sourceLayer,
129  const std::string& assetPath);
130 
131 /// \class UsdFlattenResolveAssetPathContext
132 /// Context object containing information used when resolving asset paths
133 /// during layer stack flattening.
135 {
136 public:
137  /// Layer where the asset path is authored
138  SdfLayerHandle sourceLayer;
139 
140  /// Authored asset path
142 
143  /// Expression variables from the layer stack
145 };
146 
147 /// Callback function for \c UsdFlattenLayerStack.
148 ///
149 /// The callback is given a \c UsdFlattenResolveAssetPathContext containing
150 /// information needed to resolve a given asset path. It should return the
151 /// \c std::string that should be authored in the flattened layer.
152 ///
153 /// \sa UsdFlattenLayerStack
154 using UsdFlattenResolveAssetPathAdvancedFn = std::function<
156 
157 /// Flatten the \p layerStack into a single layer with the given optional \p tag
158 /// and using the \p resolveAssetPathFn to resolve asset paths that are
159 /// encountered.
160 ///
161 /// This is an advanced version of \c UsdFlattenLayerStack that provides full
162 /// control over how asset paths are resolved during flattening via the
163 /// \p resolveAssetPathFn callback. For example, the callback might maintain
164 /// relative asset paths instead of resolving them to absolute form. As
165 /// another example, the callback might maintain stage variable expressions
166 /// in their unevaluated form.
167 USD_API
170  const PcpLayerStackRefPtr &layerStack,
171  const UsdFlattenResolveAssetPathAdvancedFn& resolveAssetPathFn,
172  const std::string& tag = std::string());
173 
174 /// Implements the default asset path flattening behavior for
175 /// \c UsdFlattenLayerStack. The asset path in \p context will be anchored to
176 /// the source layer by calling SdfComputeAssetPathRelativeToLayer. If the
177 /// asset path contains a stage variable expression, it will be evaluated using
178 /// the expression variables in \p context before being anchored.
179 USD_API
182  const UsdFlattenResolveAssetPathContext& context);
183 
185 
186 #endif /* PXR_USD_USD_FLATTEN_UTILS_H */
SdfLayerHandle sourceLayer
Layer where the asset path is authored.
Definition: flattenUtils.h:138
Definition: layer.h:97
#define USD_API
Definition: api.h:40
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:141
std::function< std::string(const UsdFlattenResolveAssetPathContext &)> UsdFlattenResolveAssetPathAdvancedFn
Definition: flattenUtils.h:155
VtDictionary expressionVariables
Expression variables from the layer stack.
Definition: flattenUtils.h:144
USD_API std::string UsdFlattenLayerStackResolveAssetPathAdvanced(const UsdFlattenResolveAssetPathContext &context)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
USD_API std::string UsdFlattenLayerStackResolveAssetPath(const SdfLayerHandle &sourceLayer, const std::string &assetPath)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::function< std::string(const SdfLayerHandle &sourceLayer, const std::string &assetPath)> UsdFlattenResolveAssetPathFn
Definition: flattenUtils.h:81