[Maya-Python] Can't get Maya to initialize through external interpreter :S

Hey all!

What I’m doing
I’ve been writing some custom maya nodes lately. The nodes are connected to each other by an editor I’ve written in PyQT. The node hierarchy then is outputted to XML which is used by the engine. This allows ARTISTS to build up objects INSIDE maya!

Goal
Now since the nodes are being edited by an editor and no user interface of maya is needed, I want to EXPOSE this tool outside of Maya aswell so people don’t have to launch maya everytime they need to edit an object settings (changing Decal surface or collision type etc), and since the tool is written in PyQT I should be able to do this! Before this tool people usually wrote the XML files by hand or by copying from templates and made alot of errors and cause engine crashes, so that’s what I’m firefighting.

So the external version would just open a Maya file and then since the nodes exists inside that scene I should easily be able to operate on them with my editor written in PyQT. All would be fine if I could launch windows through Mayapy.exe but I can’t :frowning: So I need to use an external interpreter that can load maya and still allow me to show my window!

Problem
Though there is a problem. I just can’t get Maya standalone to initialize at all :S The module is loaded! But initialize fails. It throws two errors:

The first error is thrown 3 times, says can’t find procedure start adress of …:

the second error is thrown once:

I have appended the Maya install location and Maya Python site-packages location to sys.path in the begining of the script. I’ve also added them to the Enviroment variables called MAYA_LOCATION and PYTHONPATH through os.environ[…] = … trick in the begining of the script. It finds the maya.standalone module but initialzie fails. Errors are thrown at my face :frowning:

I’m just using the pythonw.exe to launch the external version of the tool. pythonw.exe has no problems showing PyQT windows so I thought why not make an external version of the same tool so people can do simple changes without needing to open maya. But Maya needs to be initialized first :frowning:

My maya is 2011 64 bit hotfix 3 and python version is 2.6.4 64 bit. So there is no version mismatching at all :frowning:

I just can’t figure out what to do >_< Tried everything…

Would be lovely if someone can help me with this problem!

Best regards Yaz!

Is the Maya bin location in your PATH environment variable? If not, it needs to be.

Hey. Thanks for the tip! I went and checked the enviroment variables and it contained 2009s bin path… Since python uses the first found stuff I guess it retrieved something from 2009. I switched it over to 2011 and now I only get: DLL not found message.

This is the code:


#!/usr/bin/env python
import os
import sys

MAYA_LOCATION   = "C:/Program Files/Autodesk/Maya2011"
PYTHON_LOCATION = MAYA_LOCATION + "/Python/Lib/site-packages"

os.environ["MAYA_LOCATION"] = MAYA_LOCATION
os.environ["PYTHONPATH"] = PYTHON_LOCATION

#Path appending on steroids just to be safe xD
sys.path.append(MAYA_LOCATION)
sys.path.append(PYTHON_LOCATION)
sys.path.append(MAYA_LOCATION+"/bin")
sys.path.append(MAYA_LOCATION+"/lib")
sys.path.append(MAYA_LOCATION+"/Python")
sys.path.append(MAYA_LOCATION+"/Python/DLLs")
sys.path.append(MAYA_LOCATION+"/Python/Lib")
sys.path.append(MAYA_LOCATION+"/Python/Lib/plat-win")
sys.path.append(MAYA_LOCATION+"/Python/Lib/lib-tk")
print sys.path

#Import PyQT4 stuff
from PyQt4 import QtCore, QtGui, uic
import sip

#Import Maya standalone
import maya.standalone
maya.standalone.initialize(name='python')

#Import maya commands
import maya.cmds as cmds
print dir(cmds)

if __name__ == '__main__':

    print "ITS WORKING"

    #Open a new file and create a sphere and print the name of it    
    cmds.file(f=True, new=True)
    cmds.file(szInputFile, o=True)
    x = cmds.polySphere()
    print x

    #From here on it's QT stuff    
    app = QtGui.QApplication(sys.argv)

    widget = QtGui.QWidget()
    widget.show()

    sys.exit(app.exec_())

return message:

Traceback (most recent call last):
File “blablablabla.pyw”, line 28, in <module>
import maya.standalone
ImportError: DLL load failed: Det går inte att hitta den angivna proceduren. (can’t find the given procedure)

Fixed maya.standalone atleast :)!

What I did was I made a script that dumps out the entire os.environ[] and sys.path variables in a nicely formatted fashion. I ran it for pythonw.exe and mayapy.exe. I went and compared it variable by variable. I saw that these where missing from mine so I added them and now I can run Python with maya in an external interpreter

Though it says DLL can’t be found when I import PyQt4 :S But it works when I don’t lol. More firefighting I guess =/, sigh… Good news is PyMEL also works so I can replace the maya.standalone and its initialization with:
import pymel.core as pm

import os
import sys

#======================================================#
#THESE ARE THE MISSING STUFF WHEN RUNNING pythonw.exe
#======================================================#

os.environ["MAYA_LOCATION"] = "C:\Program Files\Autodesk\Maya2011"
os.environ["PYTHONHOME"]    = "C:\Program Files\Autodesk\Maya2011\Python"
os.environ["PATH"] = "C:\\Program Files\\Autodesk\\Maya2011\\bin;" + os.environ["PATH"]

sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\setuptools-0.6c9-py2.6.egg")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\pymel-1.0.0-py2.6.egg")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\ipython-0.10.1-py2.6.egg")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\ply-3.3-py2.6.egg")                         
sys.path.append("C:\Program Files\Autodesk\Maya2011\\bin\python26.zip")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\DLLs")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\plat-win")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\lib-tk")
sys.path.append("C:\Program Files\Autodesk\Maya2011\\bin")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python")
sys.path.append("C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages")


import maya.standalone
maya.standalone.initialize(name='python')

Could someone with Python 2.6.4 installed run the above script TOGETHER with this line: from PyQt4 import QtCore, QtGui, uic

I want to see if it only throws errors on my PC or on others aswell. I don’t understand why importing PyQt4 wouldn’t work when I have maya initialized. If I don’t import maya, then PyQt4 suddenly works :S

It needs to be the regular Python interpreter btw! Not mayapy

Okay I’ve narrowed down the problem…

os.environ["PATH"] = "C:\\Program Files\\Autodesk\\Maya2011\\bin;" + os.environ["PATH"]

Is the line that doens’t let me import PyQt4 :S When I comment it out it works, when I don’t it doesn’t…

The error is that it can’t load the DLL. I found a QtCore4 and QtGui4 DLL inside C:\Program Files\Autodesk\Maya2011\bin; But it also exists inside C:\Python26\Lib\site-packages\PyQt4\bin

I wonder if… :stuck_out_tongue:

EDIT: Nope didn’t work. I guess importing pymel or initializing maya.standalone automatically imports those DLLs… and my PyQT Dlls are smaller than the ones inside Maya bin. So I guess there is a version mismatch…

Found this post about Mayas QT version:

Gonna download PyQT version and try :slight_smile: Hopefully I can run QT and maya in an external interpreter… If mayapy allowed GUIs none of this would have happened >_<

Yesssss fixed it :smiley:

Nathan has compiled 4.5.3 64 bit (version that maya uses) for PyQT:

Get it here :slight_smile: http://www.nathanhorne.com/?p=204

Nathan if you are reading this! Thanks alot ^^
Now I can expose my tools both INSIDE Maya, and OUTSIDE Maya ^^

Hey,

Were you actually able to import maya.standalone and initialize it through vanilla python?

Any chance you might want to share the final setup?
I’ve tried to do that, but I always resort back to wrapping mayapy.

Cheers,
Fredrik

So you can’t use a base python installion to do this anymore, unless you’re still working out for 2012 or lower. Because as of 2013 and beyond Maya doesn’t use the same c runtime as the base python.

Instead you’d need to compile up your own version of python using the same compiler version that maya uses. At this point you could technically run maya.standalone from within python, at least once you get all the environment variables and import paths setup properly.

At this point you’ve basically recreated mayapy.

Cool, thanks. I’ll keep wrapping then.

Cheers!

Hi @fredrik I just switched to PyCharm so I could get your Qt.py stubs working and now I’m trying to see if it’s possible to create a sort of master Python interpreter for Maya, Nuke, and Houdini. That way I can get auto completion while developing in PyCharm and then run the scripts from PyCharm into any software without getting errors.

I’m currently able to get everything working just for Maya, but only if I use mayapy as the interpreter. This obviously won’t work for the other programs that I’m targeting.

So when you say “wrapping mayapy” what do you mean by that? Are you able to get mayapy functionality from a vanilla python interpreter? If so, would it be possible to also get houdini and nuke working in the same manner? Thanks!