[Maya] changing cmds.file or pm.openFile prompt flag

So while doing updates to a tool that’s made for batching commands through a big list of files, I was trying to think of a way to not have popups for missing references (some of our artists tend to have some scale models they use that aren’t on source control that they tend forget to unload) and I found this little flag buried in the file command called prompt. What it does if set to false is skips some other prompts on file open including missing reference prompts. That’s totally what I was looking for, but the problem is, it saves that setting so the next time you run any file operation and don’t set it back to true, it’ll just automatically unload any missing references.

I usually use pyMEL, so just tried it with openFile, turns out that works for it, that it’s just an undocumented flag. The issue is, I don’t want it to affect the default open behavior after the batch has run, but I can’t find a way to just set or whatever variable it seems to be saving to in the Maya Session (doesn’t save to option variables) without running another file command. Ideally, the script would open the file, then just immediately set prompts back to true right away so if anything goes wrong, it’s not stuck with the non-default settings we just went with.

I kinda found a little workaround where I open with prompt set to false, then put the batch command in a try statement, then in the except block, I just run newScene setting it back to true, then at the end, if the command runs correctly all the way through, it does newScene. But I’m not a fan and would like to try and find a way to just have it set without the necessity of a file operation.

Here’s kinda of what I’m talking about

# What I have
for path in someFiles:
    pm.openFile(path, f=True, prompt=False)

    try:
        someFunction()
    except:
        pm.newFile(f=True, prompt=True)
        # Handle errors

pm.newFile(f=True, prompt=True)

# What I'd like
for path in someFiles:
    pm.openFile(path, f=True, prompt=False)
    pm.changeSomeFileSetting(prompt=True)
    
    try:
        someFunction()
    except:
        # Handle Errors

Kinda nitpicky, big reason I want to avoid having to do a new file thing is so people can see the state of the file when it errors rather than just trashing the scene. Has anyone ever dealt with going around the file command to change that setting? Or have some other way to bypass those dialogs?

You can change the prompt value using the prompt flag by itself:

print cmds.file(prompt=True, q=True)
cmds.file(prompt=False)
print cmds.file(prompt=True, q=True)

True
False

I’d go farther and make that into a context manager so I could set the prompt value in a with statement:

with setPromptValue(False):
    pm.openFile(path)

Or a decorator:



from functools import wraps

import maya.cmds as cmds

def block_prompt(func):

    @wraps(func)
    def wrap(*args, **kwargs):
        # Grabs the initial value.
        prompt_val = cmds.file(prompt=True, q=True)

        try:
            cmds.file(prompt=False)
            return func(*args, **kwargs)

        finally:
            # Resets to the original value, this way you don't suddenly turn the prompt on, when someone wanted it off.
            cmds.file(prompt=prompt_val)

    return wrap

@block_prompt
def some_function():
    pass

I could have sworn it didn’t work when I set it without any other flags…

doh

Doh indeed for me, I thought I had tested running file with just the prompt flag and nothing else and it gave me an error. Guess it was one of those absent-minded cases where I had just thought I had. Thanks guys.