|
| 1 | +// |
| 2 | +// Copyright 2022 Autodesk |
| 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 | +#ifndef MAYAUSD_UTILS_VARIANTS_H |
| 17 | +#define MAYAUSD_UTILS_VARIANTS_H |
| 18 | + |
| 19 | +#include <mayaUsd/base/api.h> |
| 20 | + |
| 21 | +#include <pxr/usd/usd/editContext.h> |
| 22 | +#include <pxr/usd/usd/prim.h> |
| 23 | +#include <pxr/usd/usd/variantSets.h> |
| 24 | + |
| 25 | +#include <functional> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +/// General utility functions for variants |
| 29 | +namespace MAYAUSD_NS_DEF { |
| 30 | + |
| 31 | +/*! \brief Apply a function to all variants on a prim. |
| 32 | +
|
| 33 | + Optionally, if includeNonVariant is true, apply it even if the prim |
| 34 | + has no variant at all, which is useful when you want to edit something |
| 35 | + on all variations of a prim, even if there are no variations. |
| 36 | +*/ |
| 37 | +MAYAUSD_CORE_PUBLIC |
| 38 | +void applyToAllVariants( |
| 39 | + const PXR_NS::UsdPrim& primWithVariants, |
| 40 | + bool includeNonVariant, |
| 41 | + const std::function<void()>& func); |
| 42 | + |
| 43 | +/*! \brief Keep track of the current variant and restore it on destruction. |
| 44 | +
|
| 45 | + The reason we don't make this an variant auto-switcher is that |
| 46 | + switching variant recomposes the stage and one main user of the |
| 47 | + restore is visiting all variants, which would double the number |
| 48 | + of recompose if we restored the variant between each visit. |
| 49 | +
|
| 50 | + IOW, for a set with three variants A, B, C, this design permits |
| 51 | + the switch Current -> A -> B -> C -> Current instead of doing |
| 52 | + Current -> A -> Current -> B -> Current -> C -> Current. |
| 53 | +*/ |
| 54 | + |
| 55 | +class AutoVariantRestore |
| 56 | +{ |
| 57 | +public: |
| 58 | + MAYAUSD_CORE_PUBLIC |
| 59 | + AutoVariantRestore(PXR_NS::UsdVariantSet& variantSet) |
| 60 | + : _variantSet(variantSet) |
| 61 | + , _variant(variantSet.GetVariantSelection()) |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + MAYAUSD_CORE_PUBLIC |
| 66 | + ~AutoVariantRestore() { _variantSet.SetVariantSelection(_variant); } |
| 67 | + |
| 68 | +private: |
| 69 | + PXR_NS::UsdVariantSet& _variantSet; |
| 70 | + std::string _variant; |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace MAYAUSD_NS_DEF |
| 74 | + |
| 75 | +#endif // MAYAUSD_UTILS_VARIANTS_H |
0 commit comments