[Maya python] How to ignore convert unit dialog when using cmds.file to open .ma file? I already specify f=True.

[Maya python] How to ignore convert unit dialog when using cmds.file to open .ma file? I already specify f=True.

jiajun.coding
Advocate Advocate
1,215 Views
5 Replies
Message 1 of 6

[Maya python] How to ignore convert unit dialog when using cmds.file to open .ma file? I already specify f=True.

jiajun.coding
Advocate
Advocate

I need to use a python script to open maya file, then take a snapshot, then close maya file and repeat same operations on next maya file.

This automatic script works fine but sometimes blocked by a modal dialog when file unit is not same with Maya settings... So can I ignore this dialog to continue automatically?

 

PS : I know I can remove this dialog by finding out the GUI .mel script and modify it. But it is a little tricky and not convenient for other users to use my python script.

0 Likes
Accepted solutions (2)
1,216 Views
5 Replies
Replies (5)
Message 2 of 6

FirespriteNate
Advocate
Advocate
Accepted solution

if you are using the cmds.file command to open the file(s), you should be able to use the prompt=False arg to supress prompts.

Message 3 of 6

jiajun.coding
Advocate
Advocate
There seems a bug about "prompt" option. Still doesn't work in "convert unit" case. I will create another post to report bug. Anyways, thanks for your help.
0 Likes
Message 4 of 6

FirespriteNate
Advocate
Advocate

wow, that's pretty annoying 🤔.

I guess if there's no way around that, and your files are in .ma (ASCII) format, you could always parse them first to find out what units they are before you open them (this info is right at the top of a ma file, so shouldn't take any time to  query and is very simple to do). You could then simply change the scene units to the desired units first, then open the file.

It's a lot of extra messing around of course, but at least it should workaround this prompt issue.

Message 5 of 6

FirespriteNate
Advocate
Advocate
Accepted solution

I already had something kinda similar, so here's a function to return the linear unit type from a .ma file if you decide to go that route:

 

def getUnits(maFile):
    """Return the linear unit type from the given ma file"""
    # the line we're looking for is something like:
    # currentUnit -l centimeter -a degree -t ntsc;
    with open(maFile) as f:
        for line in f.readlines():
            if line.startswith('currentUnit '):
                tokens = line.split()
                for n, token in enumerate(tokens, 1):
                    if token.startswith('-l'):
                        return tokens[n]

 

It shouldn't make any differece how large the ma file is, the unit info is always on line 6-ish, so this should return as soon as it finds it, even if the ma file was hundreads of Mb+. The only real drawback is it only works on .ma files, so if you have .mb files... tough 😞 

Message 6 of 6

jiajun.coding
Advocate
Advocate
Yes. I searched all .mel scripts under Maya install folder and user install folder but didn't find where the modal dialog pops up. You are right. If we really want to fix it automatically, the only solution is to parse Maya ASCII in a standalone python batch script...
It works in my case as our studio's standard is to keep .ma in Perforce to help to compare differences. 🙂
0 Likes