Gnn
Gnn

Reputation: 107

Open a .ma file (ASCII) in Maya with Python?

I'm trying to open a maya scene .ma at the end of a Python script,

the path looks like that: G:\ProjectPath\Scene.ma.

But the only command I know for this is MEL command:

file -f -options "v=0; p=17; f=0" -ignoreVersion -typ "mayaAscii" -o 
"G:/ProjectPath/Scene.ma"; 
addRecentFile("G:/ProjectPath/Scene.ma", "mayaAscii");

Do someone know the way to do it in Python?

Upvotes: 4

Views: 12966

Answers (2)

Moobius
Moobius

Reputation: 1

For those getting the # Error: Unsaved changes who want to also save the file first rather than force it to open a new file I created the same operation that normally happens:

import maya.cmds as cmds

def open_scene(fp):
    if cmds.file(mf=True, q=True):  # If current scene is modified.
        current_fp = cmds.file(loc=True, q=True)
        if current_fp == "unknown":
            current_fp = "untitled scene"
        dialog = cmds.confirmDialog(
            title="Warning: Scene Not Saved",
            message=f"Save changes to {current_fp}?",
            button=["Save", "Don't Save", "Cancel"],
            defaultButton="Save",
            cancelButton="Cancel",
            dismissString="Cancel"
        )
        if dialog == "Cancel":
            return
        elif dialog == "Don't Save":
            cmds.file(new=True, force=True)
        else:
            if current_fp != "untitled scene":
                cmds.file(current_fp, s=True)
            else:
                save_dialog = cmds.fileDialog2(fileFilter="Maya Files (*.ma *.mb)")
                if save_dialog is None:
                    return
                cmds.file(rn=save_dialog[0])
                cmds.file(s=True)
    cmds.file(fp, open=True)

Upvotes: 0

Andy Jazz
Andy Jazz

Reputation: 58553

Here's a quick way you can do it via Python:

import maya.cmds as cmds

# Windows path version
cmds.file('G:/ProjectPath/Scene.ma', o=True)

# Mac path version
cmds.file('/Users/mac/Desktop/Scene.ma', o=True)

Or try this version if you get messages like this # Error: Unsaved changes:

file_path = 'G:/ProjectPath/Scene.ma' 
cmds.file(new=True, force=True) 
cmds.file(file_path, open=True)

Upvotes: 5

Related Questions