diff --git a/pxr/usd/sdf/primSpec.cpp b/pxr/usd/sdf/primSpec.cpp index 5936a9e5c2..6e38a8b2ac 100644 --- a/pxr/usd/sdf/primSpec.cpp +++ b/pxr/usd/sdf/primSpec.cpp @@ -762,6 +762,18 @@ SdfPrimSpec::SetVariantSelection(const std::string& variantSetName, } } +void +SdfPrimSpec::BlockVariantSelection(const std::string& variantSetName) +{ + if (_ValidateEdit(SdfFieldKeys->VariantSelection)) { + SdfVariantSelectionProxy proxy = GetVariantSelections(); + if (proxy) { + SdfChangeBlock block; + proxy[variantSetName] = std::string(); + } + } +} + // // Relocates // diff --git a/pxr/usd/sdf/primSpec.h b/pxr/usd/sdf/primSpec.h index e70daf7fdd..204b1458e5 100644 --- a/pxr/usd/sdf/primSpec.h +++ b/pxr/usd/sdf/primSpec.h @@ -695,6 +695,11 @@ class SdfPrimSpec : public SdfSpec void SetVariantSelection(const std::string& variantSetName, const std::string& variantName); + /// Blocks the variant selected for the given variant set by setting + /// the variant selection to empty. + SDF_API + void BlockVariantSelection(const std::string& variantSetName); + /// @} /// \name Relocates /// @{ diff --git a/pxr/usd/sdf/wrapPrimSpec.cpp b/pxr/usd/sdf/wrapPrimSpec.cpp index d37ff9102c..cca945e94d 100644 --- a/pxr/usd/sdf/wrapPrimSpec.cpp +++ b/pxr/usd/sdf/wrapPrimSpec.cpp @@ -361,6 +361,7 @@ void wrapPrimSpec() .def("GetAttributeAtPath", &This::GetAttributeAtPath) .def("GetRelationshipAtPath", &This::GetRelationshipAtPath) .def("GetVariantNames", &This::GetVariantNames) + .def("BlockVariantSelection", &This::BlockVariantSelection) .add_property("variantSelections", &This::GetVariantSelections, diff --git a/pxr/usd/usd/testenv/testUsdVariants.py b/pxr/usd/usd/testenv/testUsdVariants.py index c6e8f218a4..90901d8c8b 100644 --- a/pxr/usd/usd/testenv/testUsdVariants.py +++ b/pxr/usd/usd/testenv/testUsdVariants.py @@ -62,6 +62,19 @@ def test_VariantSetAPI(self): "localDanglingVariant")) self.assertFalse(prim.GetVariantSets().HasVariantSet( "referencedDanglingVariant")) + # ClearVariantSelection clears the variant set selection from the edit target, + # permitting any weaker layer selection to take effect + stage.SetEditTarget(stage.GetSessionLayer()) + prim.GetVariantSet('modelingVariant').SetVariantSelection('Carton_Sealed') + self.assertEqual(prim.GetVariantSet('modelingVariant').GetVariantSelection(), + 'Carton_Sealed') + prim.GetVariantSet('modelingVariant').ClearVariantSelection() + self.assertEqual(prim.GetVariantSet('modelingVariant').GetVariantSelection(), + 'Carton_Opened') + # BlockVariantSelection sets the selection to empty, which blocks weaker variant + # selection opinions + prim.GetVariantSet('modelingVariant').BlockVariantSelection() + self.assertEqual(prim.GetVariantSet('modelingVariant').GetVariantSelection(), '') def test_VariantSelectionPathAbstraction(self): for fmt in allFormats: diff --git a/pxr/usd/usd/variantSets.cpp b/pxr/usd/usd/variantSets.cpp index 01a43d741e..a66646f6f4 100644 --- a/pxr/usd/usd/variantSets.cpp +++ b/pxr/usd/usd/variantSets.cpp @@ -148,6 +148,17 @@ UsdVariantSet::ClearVariantSelection() return SetVariantSelection(string()); } +bool +UsdVariantSet::BlockVariantSelection() +{ + if (SdfPrimSpecHandle spec = _CreatePrimSpecForEditing()) { + spec->BlockVariantSelection(_variantSetName); + return true; + } + + return false; +} + UsdEditTarget UsdVariantSet::GetVariantEditTarget(const SdfLayerHandle &layer) const { diff --git a/pxr/usd/usd/variantSets.h b/pxr/usd/usd/variantSets.h index 7cdd530aa6..5397577774 100644 --- a/pxr/usd/usd/variantSets.h +++ b/pxr/usd/usd/variantSets.h @@ -110,6 +110,11 @@ class UsdVariantSet { USD_API bool ClearVariantSelection(); + /// Blocks any weaker selection for this VariantSet, setting it to empty in + /// the stage's current EditTarget. Return true on success, false otherwise. + USD_API + bool BlockVariantSelection(); + /// Return a \a UsdEditTarget that edits the currently selected variant in /// this VariantSet in \a layer. If there is no currently /// selected variant in this VariantSet, return an invalid EditTarget. diff --git a/pxr/usd/usd/wrapVariantSets.cpp b/pxr/usd/usd/wrapVariantSets.cpp index a05edc4319..2567e88199 100644 --- a/pxr/usd/usd/wrapVariantSets.cpp +++ b/pxr/usd/usd/wrapVariantSets.cpp @@ -83,6 +83,7 @@ void wrapUsdVariantSets() .def("SetVariantSelection", &UsdVariantSet::SetVariantSelection, arg("variantName")) .def("ClearVariantSelection", &UsdVariantSet::ClearVariantSelection) + .def("BlockVariantSelection", &UsdVariantSet::BlockVariantSelection) .def("GetVariantEditTarget", &UsdVariantSet::GetVariantEditTarget, arg("layer")=SdfLayerHandle()) .def("GetVariantEditContext", _GetVariantEditContext,