Iterating through many files within a single instance of Maya is much too
volatile I think. A safer thing to do would be run your script in a new
process per file via subprocess. It will add a few seconds to each
file-open, but I can’t see another way without walking on glass.

When doing this however, you may still encounter this issue. It’s happens
too when Maya has been initialised and Python is about to quit. You can
work around it by instead of quitting normally, such as letting the script
finish, you quit by hand via sys.exit() or force-quit with os._exit(0). The
latter command basically shuts down the process without giving it any
chance of performing clean-up and is the safest way I’ve found to get a
reasonable return-code. You’d think there are other problems attached to
this, but so far I’ve been using it for continuous integration which is
running this way tens of times a day in multiple versions of Maya and still
haven’t encountered an issue.

# Exampleimport osimport sysimport inspectimport tempfileimport subprocess
# This code runs in each file
my_script = """
import maya.cmds
import maya.standalone
maya.standalone.initialize()
maya.cmds.file( {fname} )

# some code..

os._exit(0)

"""
# Iterate over each filefor fname in ("/some/file1.ma", "/some/file2.ma"):

  # Create a script per file
  with tempfile.NamedTemporaryFile(suffix=".py") as pyfile:
    pyfile.write(my_script.format(fname=fname)
    pyfile.close()

    # Run script in file
    CREATE_NO_WINDOW = 0x08000000
    popen = subprocess.Popen(["maya", pyfile.name],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             creationflags=CREATE_NO_WINDOW)

    popen.communicate()  # Block till finished

    assert popen.returncode == 0, "An error occurred"

​

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAF0p3TnUp%3DCyR_vYeAa0pOd2tACRNnzBA7NTsNKq5%3D7A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to