diff --git a/lib/mayaUsd/ufe/UsdSceneItem.cpp b/lib/mayaUsd/ufe/UsdSceneItem.cpp index 2d50c39896..221aaf59de 100644 --- a/lib/mayaUsd/ufe/UsdSceneItem.cpp +++ b/lib/mayaUsd/ufe/UsdSceneItem.cpp @@ -15,6 +15,16 @@ // #include "UsdSceneItem.h" +#include +#if USD_VERSION_NUM < 2005 +#include +#else +#include +#endif +#if USD_VERSION_NUM >= 2008 +#include +#endif + MAYAUSD_NS_DEF { namespace ufe { @@ -39,5 +49,48 @@ std::string UsdSceneItem::nodeType() const return fPrim.GetTypeName(); } +#if UFE_PREVIEW_VERSION_NUM >= 2020 +std::vector UsdSceneItem::ancestorNodeTypes() const +{ + std::vector strAncestorTypes; + +#if USD_VERSION_NUM < 2005 + static const TfType schemaBaseType = TfType::Find(); + const TfType schemaType = schemaBaseType.FindDerivedByName(fPrim.GetTypeName().GetString()); +#else + // Get the actual schema type from the prim definition. + const TfType& schemaType = fPrim.GetPrimTypeInfo().GetSchemaType(); +#endif + if (!schemaType) { + TF_CODING_ERROR("Could not find prim type '%s' for prim %s", + fPrim.GetTypeName().GetText(), UsdDescribe(fPrim).c_str()); + return strAncestorTypes; + } + + // According to the USD docs GetAllAncestorTypes() is expensive, so we keep a cache. + static std::map> ancestorTypesCache; + const auto iter = ancestorTypesCache.find(schemaType); + if (iter != ancestorTypesCache.end()) { + return iter->second; + } + + std::vector tfAncestorTypes; + schemaType.GetAllAncestorTypes(&tfAncestorTypes); + for (const TfType& ty : tfAncestorTypes) + { +#if USD_VERSION_NUM >= 2008 + // If there is a concrete schema type name, we'll return that since it is what + // is used/shown in the UI (ex: 'Xform' vs 'UsdGeomXform'). + auto concreteType = UsdSchemaRegistry::GetConcreteSchemaTypeName(ty); + strAncestorTypes.emplace_back(!concreteType.IsEmpty() ? concreteType : ty.GetTypeName()); +#else + strAncestorTypes.emplace_back(ty.GetTypeName()); +#endif + } + ancestorTypesCache[schemaType] = strAncestorTypes; + return strAncestorTypes; +} +#endif + } // namespace ufe } // namespace MayaUsd diff --git a/lib/mayaUsd/ufe/UsdSceneItem.h b/lib/mayaUsd/ufe/UsdSceneItem.h index 0e6fc5044c..727dedd15b 100644 --- a/lib/mayaUsd/ufe/UsdSceneItem.h +++ b/lib/mayaUsd/ufe/UsdSceneItem.h @@ -48,6 +48,9 @@ class MAYAUSD_CORE_PUBLIC UsdSceneItem : public Ufe::SceneItem // Ufe::SceneItem overrides std::string nodeType() const override; +#if UFE_PREVIEW_VERSION_NUM >= 2020 + std::vector ancestorNodeTypes() const override; +#endif private: UsdPrim fPrim; diff --git a/test/lib/ufe/CMakeLists.txt b/test/lib/ufe/CMakeLists.txt index dabe2aa3a5..5b66c1ba78 100644 --- a/test/lib/ufe/CMakeLists.txt +++ b/test/lib/ufe/CMakeLists.txt @@ -28,6 +28,7 @@ if(CMAKE_UFE_V2_FEATURES_AVAILABLE) testParentCmd.py testRotateCmd.py testScaleCmd.py + testSceneItem.py testTransform3dTranslate.py ) if(UFE_PREVIEW_VERSION_NUM GREATER_EQUAL 2009) diff --git a/test/lib/ufe/testSceneItem.py b/test/lib/ufe/testSceneItem.py new file mode 100644 index 0000000000..d434936199 --- /dev/null +++ b/test/lib/ufe/testSceneItem.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# +# Copyright 2020 Autodesk +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import maya.cmds as cmds + +from ufeTestUtils import usdUtils, mayaUtils +import ufe + +import unittest +import os + +class SceneItemTestCase(unittest.TestCase): + '''Verify the SceneItem UFE interface. + ''' + + pluginsLoaded = False + + @classmethod + def setUpClass(cls): + if not cls.pluginsLoaded: + cls.pluginsLoaded = mayaUtils.isMayaUsdPluginLoaded() + + def setUp(self): + ''' Called initially to set up the maya test environment ''' + self.assertTrue(self.pluginsLoaded) + + # Open top_layer.ma scene in test-samples + mayaUtils.openTopLayerScene() + + @unittest.skipIf(os.getenv('UFE_PREVIEW_VERSION_NUM', '0000') < '2020', 'testNodeTypes only available in UFE preview version 0.2.20 and greater') + def testNodeTypes(self): + '''Engine method to run scene item test.''' + + # Get a UFE scene item for one of the balls in the scene. + ball35Path = ufe.Path([ + mayaUtils.createUfePathSegment("|world|transform1|proxyShape1"), + usdUtils.createUfePathSegment("/Room_set/Props/Ball_35")]) + ball35Item = ufe.Hierarchy.createItem(ball35Path) + + # Ball35 is an Xform. + ball35NodeType = ball35Item.nodeType() + self.assertEqual(ball35NodeType, "Xform") + + # This node type should be the first item on the ancestor node type list + # and it should have other items. + ball35AncestorNodeTypes = ball35Item.ancestorNodeTypes() + self.assertEqual(ball35NodeType, ball35AncestorNodeTypes[0]) + self.assertTrue(len(ball35AncestorNodeTypes) > 1)