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.
# Example
import os
import sys
import inspect
import tempfile
import 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 file
for 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 a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/rOnS_4vY5sE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAF0p3TnUp%3DCyR_vYeAa0pOd2tACRNnzBA7NTsNKq5%3D7A%40mail.gmail.com.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM2ybkUBUxMo%3D6qh3EX7GFmhsqa9uwmoMa-KA93sHACmmPW2_w%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCm%2BrWytxFtEe_9gwpz9_ciko2axwa_MvamtgcGAYG9Lw%40mail.gmail.com.
Hmm running multiple Maya’s could be a solution that I could consider
I’d start with one, and once you have that up and running, it will be clear how to launch 2, and then 3 and so on..
Do you mean on the instance of each file being opened in batch force the GUI to boot?
I noticed a typo in the previous call to subprocess.Popen
, it should have been mayapy
, not maya
, which would launch the Python interpreter within which you initialize Maya and run your script.
In the case of launching the GUI however, you could use maya
and pass it a Maya file. It’s possible you could pass in a startup-script this way as well, but otherwise it’s safe to generate a userSetup.py
and append it’s parent directory to the PYTHONPATH
when running Popen
. It would cause the Maya GUI to open, and then run userSetup.py
. Basically the same as above, but without the need to maya.standalone.initialize()
.
You could generate the userSetup.py
in the same way as above, and launch maya
in the same way as well.
I’m typing the code in the browser btw to don’t rely on syntax too much, just hoping to get the general idea across!
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM2ybkWKnjQkcoAJjo6%2BbudORF2fYA_cx7U63oQcTCTaKjHLjg%40mail.gmail.com.
Marcus is right. subprocess is the best way. me likey.
However. you’re using pymel so before you begin be sure and set an environment variable to turn off initializing plugins. Chad Vernon sussed out this crash a week or two ago on this group.
so set this variable if you’re on widows in your environment vars:
set PYMEL_SKIP_MEL_INIT = 1;
or on linux:
setenv PYMEL_SKIP_MEL_INIT 1
test it before hand to make sure it’ll work in subprocess. you don't want to hunt down two different issues.
in a shell type:mayapy
you should get a python prompt then test this to make sure you don't get a traceback. then you're ready to convert your code below to a subprocess call.
import maya.standalone maya.standalone.initialize() import os import pymel.core as pm # you should see this next line if you set the environment variable correctly. #pymel.internal.startup : INFO : Skipping MEL initialization cube = pm.polyCube() ## your code goes here pm.saveAs("someFile.ma") # result: Path('someFile.ma') os._exit(0)
--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/rOnS_4vY5sE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABPXW4jTXuTzP3VMbqLq3yXB9RgP%2B_9W2B10%2B3Xk9wt2Uw5%3DPA%40mail.gmail.com.
Hey Ben,
If Maya is crashing due to something in the scene, the things I’ve suggested is void. The issue I was tackling was whether Maya crashed inbetween runs, which can sometimes cause a segmentation fault.
If you are still getting a crash, even when launching via subprocess, then it can’t have anything to do with the clean-up that normally occurs when closing one file and opening another, because each subprocess will be its own unique process that couldn’t possibly affect the following or prior processes.
As far as the code you posted goes, I’m not sure what’s going on.
d
?MAYA_EXE
variable?BATCH_CONVERT
variable?Oh and stdout
and stderr
of Popen
are only necessary if you intend to print their output, as in my previous example, and if you are calling .communicate()
you can replace Popen
with call
which basically does Popen
followed by .communicate()
. Less code == happier Marcus.
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM2ybkUTZq-gpvpcO1zKJGxSm0aZAUbmU10sRYOxmt%2Bk7WTv_Q%40mail.gmail.com.To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
list_result = [] len_raw_dict = len(raw_dict) if len_raw_dict > num: base_num = len_raw_dict / num addr_num = len_raw_dict % num for i in range(num): this_dict = dict() keys = list() if addr_num > 0: keys = raw_dict.keys()[:base_num + 1] addr_num -= 1 else: keys = raw_dict.keys()[:base_num] for key in keys: this_dict[key] = raw_dict[key] del raw_dict[key] list_result.append(this_dict) else: for d in raw_dict: this_dict = dict() this_dict[d] = raw_dict[d] list_result.append(this_dict) return list_result
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM2ybkUa3AVtweJkHCkxOVP683y4t_ba6BHBTDEtr22%3DoVM9pQ%40mail.gmail.com.
I don’t want to steal the thread, but how do you do a batch playblast? I thought thats not possible.
Maybe open up a separate thread about it. That way that the answer won’t get lost as easily for future searchers.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/565CF1DF.8030000%40gmail.com.
def createReplacementCgfx(material='', texturePaths={}, cgfxChannels={}, cgfxShaderPath=''): """ If we find a shaderFxShader type material we need to replace wiuth CGFX for export purposes """ print 'Replacing shader with CGFX' #mel.eval('cgfxShader -fx "%s" ' %cgfxShaderPath) matName = str(material) shader = pm.listConnections(material, type='shadingEngine')[0] members = list(set(shader.members(flatten=True))) pm.delete(matName) print 'got members' if 'diffuse_layer0_texture' in texturePaths: newMat = pm.shadingNode('cgfxShader', asShader=True, name=matName) mel.eval('cgfxShader -e -fx "%s" %s;' %(cgfxShaderPath,newMat)) for key in texturePaths: slot = cgfxChannels[key] texture = texturePaths[key] newFileNode = createFileNode() pm.setAttr('%s.fileTextureName' %newFileNode, texture, type='string') pm.connectAttr('%s.outColor'%newFileNode, '%s.%s'%(newMat,slot), force=True) print 'connected', newMat pm.select(members) pm.hyperShade(assign=newMat) print 'CGFX function complete'
Cheers,
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
try replacing these lines:
pm.select(members)
pm.hyperShade(assign=newMat)
with this:
pm.sets(newMat, edit=True, forceElement=members)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM2ybkUa3AVtweJkHCkxOVP683y4t_ba6BHBTDEtr22%3DoVM9pQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e8f82c8e-a92e-465a-b838-52b18219f72d%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/rOnS_4vY5sE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAGq9Q7EpTi7mySZGBaMrF1WJ3Yw9UfR2rvyzy9fZfVsAz%2B2KRg%40mail.gmail.com.