Script for Importing Objects From an Asset Folder, Maya, Python

Posted by:

I implement a rigging workflow such that I frequently reuse a bunch of common shapes.  I keep them all in a specific directory (a folder called “assets” in the my documents maya folder).

I have a python function that I use to import those shapes and here it is!:

  1. #Function to import an object from a local folder
  2. #Author: Seth Meshko,
  4. def importShape(nameOfFile):
  5. scriptsUserDirectory = cmds.internalVar(usd=True) #getting this version of maya scripts directory which is in the same directory as the assets folder
  6. pathToFile = '%s/%s' % (scriptsUserDirectory.rsplit('/', 3)[0], 'assets/' + nameOfFile + '.mb')#regroving that path to go to the assets folder
  7. cmds.file(pathToFile, i = True, groupReference = True, groupName = 'loadedObjectGroup')#importing the shape
  8. #note: Nothing is returned in an import. If you want the objects you have to bundle them in a group first, then work with the group.
  9. importedObjectList= cmds.listRelatives('loadedObjectGroup', allDescendents = True, type = 'shape')#getting the childern of the import group
  10. importedObject = importedObjectList[0]#getting the first object in the list of imported objects. If you had more than one then you would have to deal with this differently
  11. cmds.parent(importedObject, world = True)#unparenting the object from the group
  12. cmds.delete('loadedObjectGroup')#deleting the group
  13. return footControlShape#returning the imported object to the function that called it.
  15. importShape('nameOfFile')#this is just the name of the file in the directory, the path is specified in the method. Also, don't include file type eg. ".ma"


  Related Posts

You must be logged in to post a comment.