Creating a new folder in your Maya directory from Python
A quick guide to creating a new directory for Maya scripts/library objects using Python.
I’m not going to go into when you might need to do this, if you’ve found yourself on this page then you probably need to do this!
Lets just straight in, we’ll start by creating our new script which we’re going to call ‘createDir.py’.
As always the first thing we need to do it define our imports, and we need 2 of them:
# 1) Import the Maya commands library from maya import cmds
The second module we import is ‘os’, this gives us access to the operating system. We need this if we are going to be creating directories as its outside of the application level of Maya:
# os module to interact with our operating system import os
We now need to define the Maya working folder, this differs between Windows and Mac, so we shouldn’t hardcode it just in case you end up sharing the code with a colleague or friend that uses a different platform to you.
Luckily Maya knows where its working directory is, and we can use Python to query it and store the result in a variable:
USERAPPDIR = cmds.internalVar(userAppDir=True)
USERAPPDATA = Our variable name, notice that it is uppercase, this is good practice for global variables
cmds= From the Maya commands library
.internalVar = Get an internal Maya value
userAppData=True = The value we want is the userAppData
If we print this variable we will get the Maya working folder:
print 'Maya application directory:', USERAPPDATA
Great! we now know where we should be storing our Maya related work. The next step is to create a new directory in this location.
Create a new variable called DIRECTORY which will store our new path:
DIRECTORY = os.path.join(USERAPPDIR, 'newDirectory')
You may be wondering what the os.path.join() means? If you have any previous coding experience you can probably guess, but I’ll explai:
os = Using the operating system module
path = Path is the OS specific path separator (Win = /, mac = \)
join() = Use the os specific separator to join the following 2 things together:
USERAPPDIR = This is our path to the Maya application folder
‘newDirectory’ = This is the name of our NEW directory
We should print the DIRECTORY variable to make sure everything looks good, we are expecting our Maya application path and our new directory name:
print 'Our new directory is', DIRECTORY
Now all we need to do it create our directory:
Or is it?….
Defining a Function
This code doesn’t make much sense, and isn’t reusable at all! we are assuming that everyone who uses this code will always want a new directory called ‘newDirectory’, I guarantee you this will never be the case.
We should define a function which allows us to specify what we want the new folder to be called when we call the function:
def createDirectory(directory=DIRECTORY): os.mkdir(directory)
Here we have a good demonstration of why our global variables should always be uppercase. Both the function argument and the variable name are the same, but even though they are if we understand the good practices of text casing and variable scope they may as well be different words altogether. Nice!
Given we now have a function which accepts arguments in the form of the directory name lets run this code a couple of times:
- Default values:
import createDir reload(createDir) createDir.createDirectory()
Check your Maya application directory for the new folder with the default name:
2. Lets run again but this time pass an argument:
import createDir reload(createDir) createDir.createDirectory(directory='AnotherDir')
Catching existing directories
But what if the directory exists? We should add something to catch this and provide some feedback, we’ll add an ‘if not’ statement, if the path does not already exist then create it using the os.mkdir() method:
# If the path does not already exist if not os.path.exists(directory): # We will make the directory os.mkdir(directory)
The complete code
# To call from Maya # ---------------------------------------- # import createDir # reload(createDir) # createDir.createDirectory() # ---------------------------------------- # Import the Maya commands library from maya import cmds # os module to interact with our operating system import os # Query the users Maya application directory USERAPPDIR = cmds.internalVar(userAppDir=True) print 'Maya application directory:', USERAPPDIR # Construct the name of the directory # Using the application directory and the name of our package # os.path.join() explained: # os: operating system # path: OS specific path seperator (Win = /, mac = \) # join(): Use the os specific separator to join these 2 things together DIRECTORY = os.path.join(USERAPPDIR, 'newDirectory') # Create a function which creates a directory # Use our constructed path as the default value def createDirectory(directory=DIRECTORY): """ Creates a given directory if it doesnt already exist Args: directory(str): The directory to create """ # If the path does not already exist if not os.path.exists(directory): # We will make the directory os.mkdir(directory)