diff --git a/cmake/modules/FindUSD.cmake b/cmake/modules/FindUSD.cmake index 26239d626f..2caef7acd6 100644 --- a/cmake/modules/FindUSD.cmake +++ b/cmake/modules/FindUSD.cmake @@ -131,6 +131,13 @@ if (USD_INCLUDE_DIR AND EXISTS "${USD_INCLUDE_DIR}/pxr/imaging/hd/changeTracker. endif() endif() +# See if USD changetracker has instance count. +set(USD_HAS_NAMESPACE_EDIT FALSE CACHE INTERNAL "USD.NamespaceEdit") +if (USD_INCLUDE_DIR AND EXISTS "${USD_INCLUDE_DIR}/pxr/usd/sdf/namespaceEdit.h") + set(USD_HAS_NAMESPACE_EDIT TRUE CACHE INTERNAL "USD.NamespaceEdit") + message(STATUS "USD has namespace edit") +endif() + # See if MaterialX shaders with color4 inputs exist natively in Sdr: # Not yet in a tagged USD version: https://github.com/PixarAnimationStudios/USD/pull/1894 set(USD_HAS_COLOR4_SDR_SUPPORT FALSE CACHE INTERNAL "USD.Sdr.PropertyTypes.Color4") diff --git a/lib/usdUfe/CMakeLists.txt b/lib/usdUfe/CMakeLists.txt index c8070e33a2..c994cfdb9d 100644 --- a/lib/usdUfe/CMakeLists.txt +++ b/lib/usdUfe/CMakeLists.txt @@ -56,6 +56,14 @@ if (USD_HAS_COLOR4_SDR_SUPPORT) ) endif() +message(STATUS "USD_HAS_NAMESPACE_EDIT is ${USD_HAS_NAMESPACE_EDIT}") +if (USD_HAS_NAMESPACE_EDIT) + target_compile_definitions(${PROJECT_NAME} + PRIVATE + USD_HAS_NAMESPACE_EDIT=1 + ) +endif() + message(STATUS "MAYA_HAS_DISPLAY_LAYER_API is ${MAYA_HAS_DISPLAY_LAYER_API}") if (MAYA_HAS_DISPLAY_LAYER_API) target_compile_definitions(${PROJECT_NAME} diff --git a/lib/usdUfe/ufe/UsdUndoInsertChildCommand.cpp b/lib/usdUfe/ufe/UsdUndoInsertChildCommand.cpp index 00f308375b..e9418a79cf 100644 --- a/lib/usdUfe/ufe/UsdUndoInsertChildCommand.cpp +++ b/lib/usdUfe/ufe/UsdUndoInsertChildCommand.cpp @@ -29,6 +29,9 @@ #include #include +#ifdef USD_HAS_NAMESPACE_EDIT +#include +#endif #include #include #include @@ -215,6 +218,23 @@ static void doInsertion( throw std::runtime_error(error); } +#ifdef USD_HAS_NAMESPACE_EDIT + // Try to use a single-layer renaming namespace edit. + // This only works correctly if there is a single layer and the destination layer + // is the same as the source layer. If it fails we will fall through to the other + // algorithm below. + if (1 == authLayerAndPaths.size()) { + SdfBatchNamespaceEdit edits; + + const auto parentPath = dstUsdPath.GetParentPath(); + edits.Add(SdfNamespaceEdit::Reparent(srcUsdPath, parentPath, SdfNamespaceEdit::Same)); + + if (dstLayer->Apply(edits)) { + return; + } + } +#endif + UsdUfe::MergePrimsOptions options; options.verbosity = UsdUfe::MergeVerbosity::None; options.mergeChildren = true; diff --git a/test/lib/ufe/testGroupCmd.py b/test/lib/ufe/testGroupCmd.py index d41d7bd54a..53339dace6 100644 --- a/test/lib/ufe/testGroupCmd.py +++ b/test/lib/ufe/testGroupCmd.py @@ -1137,6 +1137,32 @@ def testGroupNested(self): self.assertNotIn(groupName, groupNames) groupNames.add(groupName) + @unittest.skipUnless(Usd.GetVersion() >= (0, 24, 3), 'Requires USD 24.03 or greater for namespace edits') + def testGroupPrimWithRef(self): + """ + Tests that grouping a prim with a reference does not flatten + the reference when done in a single layer. + """ + cmds.file(new=True, force=True) + import mayaUsd_createStageWithNewLayer + + proxyShapePathStr = mayaUsd_createStageWithNewLayer.createStageWithNewLayer() + stage = mayaUsd.lib.GetPrim(proxyShapePathStr).GetStage() + self.assertTrue(stage) + + # create a over containing a reference + primWithRef = stage.OverridePrim("/primWithRef") + self.assertTrue(primWithRef) + cubeRefFile = testUtils.getTestScene("cubeRef", "cube.usda") + primWithRef.GetReferences().AddReference(cubeRefFile) + + cmds.group('%s,/primWithRef' % proxyShapePathStr) + + self.assertFalse(stage.GetPrimAtPath('/primWithRef')) + primWithRef = stage.GetPrimAtPath('/group1/primWithRef') + self.assertTrue(primWithRef) + self.assertTrue(primWithRef.HasAuthoredReferences()) + if __name__ == '__main__': unittest.main(verbosity=2)