Skip to content

Commit

Permalink
EMSUSD-533 - Incremental naming doesn't take zero into consideration
Browse files Browse the repository at this point in the history
* Code review feedback - fixed case where zero padding was missing.
* Added python binding for 'uniqueName' and added new tests to
  cover case above.
* Added missing tests for python bindings.
  • Loading branch information
seando-adsk committed Sep 6, 2023
1 parent 0365bc8 commit 639a949
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
16 changes: 7 additions & 9 deletions lib/usdUfe/python/wrapUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <boost/python.hpp>
#include <boost/python/def.hpp>

#include <vector>
#include <string>

using namespace boost::python;
Expand All @@ -54,9 +55,10 @@ std::string _usdPathToUfePathSegment(
return UsdUfe::usdPathToUfePathSegment(usdPath, instanceIndex).string();
}

std::string _uniqueChildName(const PXR_NS::UsdPrim& parent, const std::string& name)
std::string _uniqueName(const std::vector<std::string>& existingNames, std::string srcName)
{
return UsdUfe::uniqueChildName(parent, name);
PXR_NS::TfToken::HashSet existingNamesSet(existingNames.begin(), existingNames.end());
return UsdUfe::uniqueName(existingNamesSet, srcName);
}

std::string _stripInstanceIndexFromUfePath(const std::string& ufePathString)
Expand All @@ -75,11 +77,6 @@ int _ufePathToInstanceIndex(const std::string& ufePathString)
return UsdUfe::ufePathToInstanceIndex(Ufe::PathString::path(ufePathString));
}

bool _isEditTargetLayerModifiable(const PXR_NS::UsdStageWeakPtr stage)
{
return UsdUfe::isEditTargetLayerModifiable(stage);
}

PXR_NS::UsdTimeCode _getTime(const std::string& pathStr)
{
const Ufe::Path path = Ufe::PathString::path(pathStr);
Expand All @@ -104,11 +101,12 @@ void wrapUtils()
def("usdPathToUfePathSegment",
_usdPathToUfePathSegment,
(arg("usdPath"), arg("instanceIndex") = PXR_NS::UsdImagingDelegate::ALL_INSTANCES));
def("uniqueChildName", _uniqueChildName);
def("uniqueName", _uniqueName);
def("uniqueChildName", UsdUfe::uniqueChildName);
def("stripInstanceIndexFromUfePath", _stripInstanceIndexFromUfePath, (arg("ufePathString")));
def("ufePathToPrim", _ufePathToPrim);
def("ufePathToInstanceIndex", _ufePathToInstanceIndex);
def("isEditTargetLayerModifiable", _isEditTargetLayerModifiable);
def("isEditTargetLayerModifiable", UsdUfe::isEditTargetLayerModifiable);
def("getTime", _getTime);
def("isAttributeEditAllowed", _isAttributeEditAllowed);
}
5 changes: 4 additions & 1 deletion lib/usdUfe/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ std::string uniqueName(const TfToken::HashSet& existingNames, std::string srcNam
suffixStr = std::string(lenSuffix - std::min(lenSuffix, suffixStr.length()), '0') + suffixStr;
std::string dstName = base + suffixStr;
while (existingNames.count(TfToken(dstName)) > 0) {
dstName = base + std::to_string(++suffix);
suffixStr = std::to_string(++suffix);
suffixStr
= std::string(lenSuffix - std::min(lenSuffix, suffixStr.length()), '0') + suffixStr;
dstName = base + suffixStr;
}
return dstName;
}
Expand Down
88 changes: 66 additions & 22 deletions test/lib/ufe/testPythonWrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from maya import cmds
from maya import standalone
from pxr import Usd

import ufe

Expand Down Expand Up @@ -82,28 +83,71 @@ def testWrappers(self):

# Test the maya-usd ufePathToPrim() wrapper.
mayaUsdStage.DefinePrim("/Capsule1", "Capsule")
if ufeUtils.ufeFeatureSetVersion() >= 2:
capsulePrim = mayaUsd.ufe.ufePathToPrim('%s,/Capsule1' % proxyShape)
else:
capsulePrim = mayaUsd.ufe.ufePathToPrim('|world%s,/Capsule1' % proxyShape)
capsulePrim = mayaUsd.ufe.ufePathToPrim('%s,/Capsule1' % proxyShape)
self.assertIsNotNone(capsulePrim)

if ufeUtils.ufeFeatureSetVersion() >= 2:
# Test the maya-usd getPrimFromRawItem() wrapper.
capsulePath = proxyShapePath + usdUtils.createUfePathSegment('/Capsule1')
capsuleItem = ufe.Hierarchy.createItem(capsulePath)
rawItem = capsuleItem.getRawAddress()
capsulePrim2 = mayaUsd.ufe.getPrimFromRawItem(rawItem)
self.assertIsNotNone(capsulePrim2)
self.assertEqual(capsulePrim, capsulePrim2)

# Test the maya-usd getNodeTypeFromRawItem() wrapper.
nodeType = mayaUsd.ufe.getNodeTypeFromRawItem(rawItem)
self.assertIsNotNone(nodeType)

# Test the maya-usd getNodeNameFromRawItem() wrapper.
nodeName = mayaUsd.ufe.getNodeNameFromRawItem(rawItem)
self.assertIsNotNone(nodeName)
# Test maya-usd usdPathToUfePathSegment wrapper.
ufePath = mayaUsd.ufe.usdPathToUfePathSegment('/Capsule1')
self.assertEqual(ufePath, str(usdUtils.createUfePathSegment('/Capsule1')))

# Test the uniqueName wrapper.
# Note: param1 = list of existing children names.
# param2 = source name that you want made unique
# return = unique name from input source name
newName = mayaUsd.ufe.uniqueName([], 'Capsule')
self.assertEqual(newName, 'Capsule1')
newName = mayaUsd.ufe.uniqueName([], 'Capsule1')
self.assertEqual(newName, 'Capsule2')
newName = mayaUsd.ufe.uniqueName(['Cone1'], 'Capsule1')
self.assertEqual(newName, 'Capsule2')
newName = mayaUsd.ufe.uniqueName(['Capsule1'], 'Capsule1')
self.assertEqual(newName, 'Capsule2')
newName = mayaUsd.ufe.uniqueName(['Capsule1', 'Capsule5'], 'Capsule1')
self.assertEqual(newName, 'Capsule2')
newName = mayaUsd.ufe.uniqueName(['Capsule001'], 'Capsule001')
self.assertEqual(newName, 'Capsule002')
newName = mayaUsd.ufe.uniqueName(['Capsule001', 'Capsule002'], 'Capsule001')
self.assertEqual(newName, 'Capsule003')
newName = mayaUsd.ufe.uniqueName(['Capsule001', 'Capsule005'], 'Capsule001')
self.assertEqual(newName, 'Capsule002')

# Test uniqueChildName wrapper.
newName = mayaUsd.ufe.uniqueChildName(capsulePrim, 'Cone1')
self.assertEqual(newName, 'Cone1')
mayaUsdStage.DefinePrim("/Capsule1/Cone1", "Cone")
newName = mayaUsd.ufe.uniqueChildName(capsulePrim, 'Cone1')
self.assertEqual(newName, 'Cone2')
mayaUsdStage.DefinePrim("/Capsule1/Sphere001", "Sphere")
newName = mayaUsd.ufe.uniqueChildName(capsulePrim, 'Sphere001')
self.assertEqual(newName, 'Sphere002')

# stripInstanceIndexFromUfePath/ufePathToInstanceIndex wrappers are tested
# by testPointInstances.

# isEditTargetLayerModifiable wrapper is tested by testBlockedLayerEdit.

# Test the getTime wrapper.
cmds.currentTime(10)
t = mayaUsd.ufe.getTime(stagePath)
self.assertEqual(t, Usd.TimeCode(10))

# isAttributeEditAllowed wrapper is tested by testAttribute.

# Test the maya-usd getPrimFromRawItem() wrapper.
capsulePath = proxyShapePath + usdUtils.createUfePathSegment('/Capsule1')
capsuleItem = ufe.Hierarchy.createItem(capsulePath)
rawItem = capsuleItem.getRawAddress()
capsulePrim2 = mayaUsd.ufe.getPrimFromRawItem(rawItem)
self.assertIsNotNone(capsulePrim2)
self.assertEqual(capsulePrim, capsulePrim2)

# Test the maya-usd getNodeTypeFromRawItem() wrapper.
nodeType = mayaUsd.ufe.getNodeTypeFromRawItem(rawItem)
self.assertIsNotNone(nodeType)

# Test the maya-usd getNodeNameFromRawItem() wrapper.
nodeName = mayaUsd.ufe.getNodeNameFromRawItem(rawItem)
self.assertIsNotNone(nodeName)

# Test the maya-usd runtime id wrappers.
mayaRtid = mayaUsd.ufe.getMayaRunTimeId()
Expand All @@ -118,9 +162,9 @@ def testWrappers(self):
# a worker thread.
cmds.file(new=True, force=True)

# In Maya 2020 and 2022, undo does not restore the stage. To be
# In Maya 2022, undo does not restore the stage. To be
# investigated as needed.
@unittest.skipUnless((mayaUtils.mayaMajorVersion() == 2023) or mayaUtils.previewReleaseVersion() >= 139, 'Only supported in Maya 2023 or greater.')
@unittest.skipUnless(mayaUtils.mayaMajorVersion() == 2023, 'Only supported in Maya 2023 or greater.')
def testGetAllStages(self):
cmds.file(new=True, force=True)

Expand Down

0 comments on commit 639a949

Please sign in to comment.