Search code examples
pythontypeerrormaya

TypeError, making directory with Python in Maya


I am trying to make a folder to save a file in with Python in Maya. However, I am getting an error, of which I am not sure how to solve. (not very experienced in scripting, yet)

This is the code that creates the directory string: (Maya correctly prints out the DIRECTORY)

# Creates a directory to save the .json file to
USERAPPDIR = cmds.internalVar(userAppDir = True)
DIRECTORY = os.path.join(USERAPPDIR, "gradingManager")
print('Maya application directory: ', DIRECTORY)

The function that creates the directory is controlled with a button in Maya:

    ##########################
    # Safe data in json file #
    ##########################
    cmds.text(label = "")
    cmds.text(label = " Save all results to a .json file for record keeping.",
              font = "boldLabelFont")
    cmds.button(label = "Save to .json file", command = self.save, width = 600)

The actual function(s):

def save(self, directory=DIRECTORY, *args):
    ######################################################################
    ## This method saves the information gathered above in a .json file ##
    ######################################################################

    # creates a directory
    self.createDir(directory)

    print("saving things")

def createDir(self, directory=DIRECTORY):
    ###################################################################
    ## This function creates a directory for the save functionality. ##
    ###################################################################

    if not os.path.exists(directory):
        os.mkdir(directory)

The error & function it refers to:

# Error: TypeError: file C:\Program Files\Autodesk\Maya2020\bin\python27.zip\genericpath.py line 26: coercing to Unicode: need string or buffer, bool found #

# Does a path exist?
# This is false for dangling symbolic links on systems that support them.
def exists(path):
    """Test whether a path exists.  Returns False for broken symbolic links"""
    try:
        os.stat(path)
    except os.error:
        return False
    return True

I hope this is enough information. I kept the functions as clean as possible so it is clear that the issue comes from checking if the path for the new directory already exists.. If you need any more information, I'll be happy to supply it.


Solution

  • When you press button then you expect it will run save(some_path) or save() with default value DIRECTORY but it doesn't work this way.

    Button executes function with some values predefined by Maya authors. I don't know what values send button in Maya to save() but in other GUIs usually it sends event informations - ie. what widget was click, what mouse button was used, what was mouse position, etc.

    So it seems button executes save(True) or maybe even save(True, other values) and this assign True to dictionary in your def save(self, dictionary, ...) and later it runs createDir(True) and exists(True)` and you get error message.

    You should use DIRECTORY directly inside function.

    def save(self, *args):
    
        directory = DIRECTORY
    
        # creates a directory
        self.createDir(directory)
    
        print("saving things")
    

    And if you have some widget to selecte directory or write manually directory then you would have to also use it inside function

    def save(self, *args):
    
        directory = some_widget_to_select_folder.get_selected_folder()
       
        if not directory:
            directory = DIRECTORY
    
        # creates a directory
        self.createDir(directory)
    
        print("saving things")