PDA

View Full Version : Passing variables from MEL to MayaPython


arcsecond
11-14-2008, 07:44 PM
Apologies if this has been asked before, it seems really basic to me but a search provided no hits.

How do I take the value of a MEL variable I have created and pass it into a python variable?

say I have (MEL):
string $test = "I'm a test";
and I want to have (Python):
testVar = "I'm a test"

My first thought was something like (MEL):
python( "import maya.mel as mm" )
python( "testVar = mm.eval(\"$test\")")

but this and other variations leave me with my python testVar equivalent to either None or with a Runtime Error on the MEL side. On one variation I even got a "maximum recursion depth exceeded".

Thanks in advance for any help.
-James

Norb
11-14-2008, 08:34 PM
I'm curious as to why you would want to do this? Could you provide an example of what you're trying to do? There might be an easier way to do things.

arcsecond
11-14-2008, 08:51 PM
Well, at the moment what I'm trying to do is modify an existing script (jpScriptManager.mel) that hunts down through a given directory structure and makes a button for every .MEL script it finds. Pushing that button sources that script. I'd like to add the ability to handle .py scripts as well. But python scripts cannot simply be sourced with MEL. They are imported with python.

The existing script already does a lot of work to find the locations of all the directories and scripts (and display them in a pleasing manner) and I'd like to re-use that data for python scripts. If I can't, then I might as well just have two separate script managers one for MEL (written in MEL) and one for python (written in Python). Which means a complete re-write, duplicating lots of work.

The missing element is simply a string with a full path name inside of a maya variable that seemingly cannot be transferred into a python variable.

-James

Norb
11-14-2008, 08:59 PM
Could you not modify it to look at the file extensions? Have it differentiate between '.mel' and '.py' and then source or import accordingly?

arcsecond
11-14-2008, 09:05 PM
Yes, that's exactly what I'm trying to do. But knowing where the file is in MEL does me no good at all in trying to import it in python. I have to duplicate all the work that finds the file path in python in order to use it in python.

my file path is in the MEL variable $thisFile.

the MEL statement
python( "from $thisFile import *")
doesn't work because python does not understand the MEL variable $thisFile.
I need a python variable 'thisFile'
in order to execute the MEL statement
python( "from thisFile import *")

-James

arcsecond
11-14-2008, 09:46 PM
AH HA!!

after some more digging in the help files I come across this:
file:///C:/Program%20Files/Autodesk/Maya2008/docs/Maya2008/en_US/wwhelp/wwhimpl/common/html/wwhelp.htm?context=General&file=Important_differences_between_MEL_and_Python.html

Which states down around the bottom:
"Conversely, if you want to use a MEL variable in Python, you need to do something like:
import maya.mel
myPythonVariable = maya.mel.eval ('global $myMELvariable; $temp=$myMELvariable;' )

This works because the assignment statement, which is last in the script passed to the eval command, returns a result. You can access only globally scoped MEL variables in Python."

I was right, I was just missing a step.

-James

Norb
11-14-2008, 09:51 PM
i did this with mel..


string $s = "printMe";
string $sCMD = "python(\"import " + $s + " as pm\")";
eval($sCMD);

arcsecond
11-14-2008, 09:59 PM
Hmm,... I think that's a better solution. For some reason constructing command strings never occurs to me.

Thanks.
-James

CGTalk Moderation
11-14-2008, 09:59 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.