As I continue to learn more about python and maya I’m finding more efficient ways of gathering data. Yesterday during a code review I used the following way to grab the base path from a Maya filename.
import maya.cmds as cmds
fileName = cmds.file(sceneName=True, q=True)
baseFileName = fileName.split('/')
del baseFileName[-1]
basePath = "/".join(baseFileName)
print basePath
It seemed to do the job for me, it would return the base path to the maya file that was currently open but drop off the last obj in the array.
My buddy JT reviewed the code and suggested I check out the os. commands. So in a quick exercise I did just that, and found I can do the exact same thing with even fewer lines of code.
import maya.cmds as cmds
import os
fileName = cmds.file(sceneName=True, q=True)
basePath = os.path.dirname(fileName)
print basePath
nice. yeah basename, basenameEx ad dirname save you some splitting in python or tokenizing in mel. good stuffs.
nice. yeah basename, basenameEx ad dirname save you some splitting in python or tokenizing in mel. good stuffs.