Skip to content

Commit

Permalink
Merge pull request #628 from Autodesk/sabrih/MAYA-104778/fix_invalid_…
Browse files Browse the repository at this point in the history
…char_crashes_maya

Fix naming a prim to something that contains an invalid character crashes Maya
  • Loading branch information
Krystian Ligenza authored Jul 7, 2020
2 parents 1c0de92 + 2c89935 commit c0280d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/mayaUsd/ufe/UsdUndoRenameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ bool UsdUndoRenameCommand::renameRedo()
auto primPath = prim.GetPath();
auto defaultPrimPath = _stage->GetDefaultPrim().GetPath();

// all special characters are replaced with `_`
const std::string specialChars{"~!@#$%^&*()-=+,.?`':{}|<>[]/"};
std::replace_if(_newName.begin(), _newName.end(), [&](auto c){
return std::string::npos != specialChars.find(c);
}, '_');

// set prim's name
// XXX: SetName successfuly returns true but when you examine the _prim.GetName()
// after the rename, the prim name shows the original name HS, 6-May-2020.
Expand Down
38 changes: 38 additions & 0 deletions test/lib/ufe/testRename.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import mayaUsd.ufe

import unittest
import re

class RenameTestCase(unittest.TestCase):
'''Test renaming a UFE scene item and its ancestors.
Expand Down Expand Up @@ -308,4 +309,41 @@ def testRenameRestrictionExternalReference(self):
newName = 'leaf_ref_2_renaming'
cmds.rename(newName)

def testRenameSpecialCharacter(self):
# open twoSpheres.ma scene in test-samples
mayaUtils.openTwoSpheresScene()

# clear selection to start off
cmds.select(clear=True)

# select a USD object.
mayaPathSegment = mayaUtils.createUfePathSegment('|world|usdSphereParent|usdSphereParentShape')
usdPathSegment = usdUtils.createUfePathSegment('/sphereXform/sphere')
basePath = ufe.Path([mayaPathSegment, usdPathSegment])
usdSphereItem = ufe.Hierarchy.createItem(basePath)

ufe.GlobalSelection.get().append(usdSphereItem)

# get the USD stage
stage = mayaUsd.ufe.getStage(str(mayaPathSegment))

# check GetLayerStack behavior
self.assertEqual(stage.GetLayerStack()[0], stage.GetSessionLayer())
self.assertEqual(stage.GetEditTarget().GetLayer(), stage.GetSessionLayer())

# set the edit target to the root layer
stage.SetEditTarget(stage.GetRootLayer())
self.assertEqual(stage.GetEditTarget().GetLayer(), stage.GetRootLayer())

# rename with special chars
newNameWithSpecialChars = '!@#%$@$=sph^e.re_*()<>}021|'
cmds.rename(newNameWithSpecialChars)

# get the prim
pSphereItem = ufe.GlobalSelection.get().front()
usdPrim = stage.GetPrimAtPath(str(pSphereItem.path().segments[1]))
self.assertTrue(usdPrim)

# prim names are not allowed to have special characters except '_'
regex = re.compile('[@!#$%^&*()<>?/\|}{~:]')
self.assertFalse(regex.search(usdPrim.GetName()))

0 comments on commit c0280d7

Please sign in to comment.