Hi all, I am doing a simple renaming application but I am encountering a few problems, hopefully someone may point it out to me so that I can revise my code.
Okay, the code below shows a part of my renaming process in which in the searchReplace, it is working fine if the user are selecting a single/ multiple items manually.
However, I have also implemented a selectHierarchy button (as shown in the code) which selects the children. For example:
Group 1
-> pCube1
–> pCone1
So if user selects pCube1 and hit the button, it will selects its child item, pCone1 in this case.
But when I tried to run the searchReplace function, say I am going to replace ‘p’ with ‘test’, it only renames the first item - pCube1 to testCube1 while not doing the same for the child item as it still remains as pCone1 and I also got the following error: # RuntimeError: No object matches name #
I think I may have missed out something in the selectHierarchy function or something… Any ideas?
By the way, I have a side question too. Is it wise to have all my defining function, def() to have cmds.ls(sl=True) in each and every one of them?
Other than the tedios task of typing them the same over and over again, but will it cause any adverse effects? Or will it be okay to leave it as it is since the whole code is able to run without any problems?
def searchReplace(self):
wordSearch = str(self.searchTxt.text())
wordReplace = str(self.replaceTxt.text())
objCnt = cmds.ls(sl=True, sn=True)
if len(objCnt) == 0:
self.searchTxt.clear()
self.replaceTxt.clear()
cmds.warning('Nothing is selected')
else:
for wordString in objCnt:
if wordSearch in wordString:
newWordString = wordString.replace(wordSearch, wordReplace)
cmds.rename(wordString, newWordString)
self.searchTxt.clear()
self.replaceTxt.clear()
print '%s' %wordString + " has changed to : " + "%s" %newWordString
def selectHierarchy(self):
sel = cmds.ls(selection = True)
selCnt = len(sel)
if int(selCnt) != 0:
objHierarchy = cmds.select(hi=True)
else:
cmds.warning('Nothing is selected')