PDA

View Full Version : Passing Python string to Mel?


SKraft
11-22-2011, 01:10 AM
Hi there, I've run into this problem a couple times now. I'm trying to pass a string from python into mel. Here is a simple example of what I'm doing:

import maya.mel as mel

def getPythonString():
pythonString = 'Hello World!'
return pythonString

def mainFunction():
mel.eval('string $melString = python("getPythonString()");')
mel.eval('print $melString;')

mainFunction()

now that works fine if I run it from the script editor but when I save it out to a python module and call it:

import test
test.mainFunction()

...I get:

# RuntimeError: Error occurred during execution of MEL script
# line 1: name 'getPythonString' is not defined

for some reason that function isn't seen by mel. Python can see the function but mel can't. Is there some way to elevate a string or function to a global level within a python module so that mel can see it?

I'm kinda stuck here, any help would be much appreciated!
- Sean

r4inm4ker
11-22-2011, 01:33 AM
import maya.mel as mel

def getPythonString():
pythonString = 'Hello World!'
return pythonString

def mainFunction():

mel.eval('string $melString = python("import test");')
mel.eval('string $melString = python("test.getPythonString()");')
mel.eval('print $melString;')

mainFunction()


you need to import your own module ("test") before using some function inside it.

SKraft
11-22-2011, 05:34 PM
That was it! Thank you so much. The only tweak I made was to remove the 'string $melString' from the first line of mel:

mel.eval('python("import test");')
mel.eval('string $melString = python("test.getPythonString()");')
mel.eval('print $melString;')

Thanks again!
- Sean

CGTalk Moderation
11-22-2011, 05:34 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.