Skip to content

Commit dbeae8b

Browse files
MAYA-122926 reset auto-edit in all variants
- Add a utility function to apply a function to all variants of a prim. - Add a utility class to automatically restore the current variant on exit. - Use these to clear the auto-edit flag in all variants. - Remove obsolete comments about type info not being correctly preserved and psuhEnd not being called (now it is called).
1 parent ce4d45a commit dbeae8b

4 files changed

Lines changed: 151 additions & 12 deletions

File tree

lib/mayaUsd/utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target_sources(${PROJECT_NAME}
2222
util.cpp
2323
utilFileSystem.cpp
2424
utilSerialization.cpp
25+
variants.cpp
2526
)
2627

2728
set(HEADERS
@@ -43,6 +44,7 @@ set(HEADERS
4344
util.h
4445
utilFileSystem.h
4546
utilSerialization.h
47+
variants.h
4648
)
4749

4850
set(PLUGINFO

lib/mayaUsd/utils/variants.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
#include "variants.h"
17+
18+
#include <pxr/usd/usd/stage.h>
19+
20+
/// General utility functions for variants
21+
namespace MAYAUSD_NS_DEF {
22+
23+
void applyToAllVariants(
24+
const PXR_NS::UsdPrim& primWithVariants,
25+
bool includeNonVariant,
26+
const std::function<void()>& func)
27+
{
28+
// Record if we saw at least one variant to apply the function on.
29+
// Used when non-variant are included.
30+
bool atLeastOneVariant = false;
31+
32+
// Clear the edit flag in all other variants, in all variant sets if any.
33+
PXR_NS::UsdStagePtr stage = primWithVariants.GetStage();
34+
PXR_NS::UsdVariantSets variantSets = primWithVariants.GetVariantSets();
35+
for (const std::string& variantSetName : variantSets.GetNames()) {
36+
37+
PXR_NS::UsdVariantSet variantSet = primWithVariants.GetVariantSet(variantSetName);
38+
39+
// Make sure to restore the current selected variant even if the face
40+
// of exceptions.
41+
AutoVariantRestore variantRestore(variantSet);
42+
43+
for (const std::string& variantName : variantSet.GetVariantNames()) {
44+
if (variantSet.SetVariantSelection(variantName)) {
45+
PXR_NS::UsdEditTarget target = stage->GetEditTarget();
46+
47+
PXR_NS::UsdEditContext switchEditContext(
48+
stage, variantSet.GetVariantEditTarget(target.GetLayer()));
49+
50+
func();
51+
atLeastOneVariant = true;
52+
}
53+
}
54+
}
55+
56+
// When not a single variant was found and the caller wants to apply
57+
// the function even in the absence of vairants, call it now.
58+
if (includeNonVariant && !atLeastOneVariant)
59+
func();
60+
}
61+
62+
} // namespace MAYAUSD_NS_DEF

lib/mayaUsd/utils/variants.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

lib/usd/translators/mayaReferenceUpdater.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <mayaUsd/utils/editRouter.h>
2626
#include <mayaUsd/utils/util.h>
2727
#include <mayaUsd/utils/utilSerialization.h>
28+
#include <mayaUsd/utils/variants.h>
2829
#include <mayaUsdUtils/MergePrims.h>
2930
#include <mayaUsd_Schemas/ALMayaReference.h>
3031
#include <mayaUsd_Schemas/MayaReference.h>
@@ -48,14 +49,21 @@
4849

4950
namespace {
5051

52+
// Clear the auto-edit flag on a USD Maya Reference so that it does not
53+
// get edited immediately again. Clear in all variants, since each
54+
// variant has its own copy of the flag.
5155
void clearAutoEdit(const UsdPrim& prim)
5256
{
53-
if (prim.IsValid()) {
57+
UsdPrim parentPrim = prim.GetParent();
58+
MAYAUSD_NS::applyToAllVariants(parentPrim, true, [prim]() {
59+
// Note: the prim might not exist in all variants, so check its validity.
60+
if (!prim.IsValid())
61+
return;
62+
5463
UsdAttribute mayaAutoEditAttr = prim.GetAttribute(MayaUsd_SchemasTokens->mayaAutoEdit);
55-
if (mayaAutoEditAttr.IsValid()) {
64+
if (mayaAutoEditAttr.IsValid())
5665
mayaAutoEditAttr.Set<bool>(false);
57-
}
58-
}
66+
});
5967
}
6068

6169
std::string findValue(const PXR_NS::VtDictionary& routingData, const PXR_NS::TfToken& key)
@@ -214,9 +222,6 @@ UsdMayaPrimUpdater::PushCopySpecs PxrUsdTranslators_MayaReferenceUpdater::pushCo
214222

215223
// The Maya reference is meant as a cache, and therefore fully
216224
// overwritten, so we don't call MayaUsdUtils::mergePrims().
217-
// As of 13-Dec-2021 pushEnd() will not be called on the
218-
// MayaReferenceUpdater, because the prim updater type information
219-
// is not correctly preserved. Unload the reference here. PPT.
220225
if (SdfCopySpec(srcLayer, srcSdfPath, dstLayer, dstPath)) {
221226
const MObject& parentNode = getMayaObject();
222227
UsdMayaTranslatorMayaReference::UnloadMayaReference(parentNode);
@@ -256,11 +261,6 @@ bool PxrUsdTranslators_MayaReferenceUpdater::discardEdits()
256261
/* virtual */
257262
bool PxrUsdTranslators_MayaReferenceUpdater::pushEnd()
258263
{
259-
// As of 25-Feb-2022 the Maya transform node pulled from the Maya reference
260-
// prim ends up being unlocked by the unlock traversal in
261-
// PrimUpdaterManager::mergeToUsd(). However, more robust to enforce
262-
// separation of concerns and perform the inverse of editAsMaya() here.
263-
264264
// Unnecessary to unlock individual attributes, as the Maya transform node
265265
// is removed at pushEnd().
266266
MDagPath transformPath;

0 commit comments

Comments
 (0)