Using MEL variables in maya.standalone

Hi, I’m having trouble casting my python variables to mel when using maya standalone. The end goal is to use maya standalone to export fbx. My main py script works fine inside of maya, but I get errors trying to convert variables - here is a small test case:

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

import maya.cmds as cmds
import maya.mel as mel

#set globals
mel.eval('global string $mymel;')
mypy = 'hello'

def PrintVar():
	mel.eval('$mymel = python("mypy");') 
	mel.eval('print $mymel') 

PrintVar()

So this is the error I’m getting in standalone (the script is called melvar.py):


Error: line 1: name 'mypy' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'mypy' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "melvar.py", line 15, in <module>
    PrintVar()
  File "melvar.py", line 12, in PrintVar
    mel.eval('$mymel = python("mypy");')
RuntimeError: Error occurred during execution of MEL script
line 1: name 'mypy' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'mypy' is not defined

Any ideas? Or even workarounds to export fbx without using mel are welcome!

THANKS!!

I suspect there is a difference to how maya and mayastandalone handle variable namespaces. But who knows, python/MEL interop is atrocious and I have no idea how the variable namespacing is going to decide to work. I’d suggest NOT doing what you’re doing, and just have python create MEL strings that you can call mel.eval on- I don’t know why you’d want to do that.

That said, if you were to do this in python (to put ‘mayapy’ into your globals), your MEL may work:
globals()[‘mayapy’] = ‘hello’

Thanks - creating a python string to use in my mel.eval is exactly what I ended up doing! Not sure why I didn’t think of that after spending WAY too much time getting the mel var to work :D: