Blog

Find an object by naming convention script, Python, Maya

Posted by:

  1. #jfm_findObjectsByNames.py
  2. #created by Seth Meshko, 12/26/17
  3. #website: www.3danimationartist.com
  4. #you are welcome to use and redistribute this code, please just give credit where credit is due.
  5.  
  6.  
  7. '''
  8. This script allows you to pass in a list of strings and iterate through your scene to find all matches for objects with
  9. those strings in the object names. This script is not really usefull on its own. It is to be used in conjunction with other
  10. functions to process the objects that get returned
  11.  
  12. the flaw that you may run into in this script is if your search words are contained in other words then you will get false returns
  13. for instance if you are searching for 'Rig' and you have a bunch of stuff in your scene with the name 'Right' then you will get
  14. those words returned too. This script is written for a really solid naming convention and the way that I get around things
  15. like this is by using 'Rt' for 'Right'. Basically anything that I know is going to be a key word gets a special treatment, another
  16. way of making a distinction is using all caps like: 'RIG' or 'SKIN'
  17. '''
  18.  
  19. import maya.cmds as cmds
  20.  
  21. def findObjectByNamingConvention(objectToSearch, listOfNames):
  22.  
  23. countList = []
  24. i= 0
  25. for eachName in listOfNames:
  26. print('eachName is : ' + listOfNames[i])
  27. countList.append(objectToSearch.count(listOfNames[i]))
  28. print(countList)
  29. i = i+1
  30.  
  31. numOfOccurances = 0
  32. for eachCountListEntry in countList:
  33. numOfOccurances = numOfOccurances + eachCountListEntry
  34.  
  35. if(numOfOccurances == len(listOfNames)):
  36. return True
  37. else:
  38. return False
  39.  
  40. selectAll = cmds.select(allDagObjects = True)
  41. selectAllList = cmds.ls(selection = True)
  42. print('the selectAllList is: ' + str(selectAllList))
  43. objectList = []
  44.  
  45. i = 0
  46. for eachObject in selectAllList:
  47. eachHierarchy = cmds.listRelatives(eachObject, allDescendents = True)
  48. eachHierarchy.extend(selectAllList)
  49. print('eachHierarchy is: ' + str(eachHierarchy))
  50. listOfNames = []
  51. listOfNames = ['blerg', 'floop'] #Change and add to this list for more search criteria
  52.  
  53. for eachSubObject in eachHierarchy:
  54. print('the object being searched is: ' + str(eachSubObject))
  55. objectFound = findObjectByNamingConvention(eachSubObject, listOfNames)
  56. if(objectFound == True):
  57. print('duplicateJoints(), the joint found is: ' + str(eachSubObject))
  58. objectList.append(eachSubObject)
  59.  
  60. print('the objectList is: ' + str(objectList))
  61. cmds.select(objectList)

 

 

0
  Related Posts
  • No related posts found.

You must be logged in to post a comment.