Calling python functions from MEL

693 views
Skip to first unread message

Ofer

unread,
Jan 21, 2008, 8:17:44 PM1/21/08
to python_inside_maya
Is there a nice elegant way of exposing python functions to MEL?
My current solution would be to run something like this:
pm.mel.eval('global proc string func() { return
python("py_func()") }')

But there's a big mess when it comes to dealing with arguments and
return values...

Any bright ideas out there?

farsheed Ashouri

unread,
Jan 21, 2008, 8:43:32 PM1/21/08
to python_inside_maya
No.
Python has a more sophisticated type system than MEL, so it is not
possible to convert all Python data types into native MEL types. The
python command converts those that it knows how to handle. For the
rest, it requests a string representation of the object and returns
that.

reference:
file:///C:/Program%20Files/Autodesk/Maya2008/docs/Maya2008/en_US/wwhelp/wwhimpl/common/html/wwhelp.htm?context=WhatsNew&file=Overview_of_Whats_New.html

Chadrik

unread,
Jan 22, 2008, 1:58:39 AM1/22/08
to python_in...@googlegroups.com

i'm not sure i follow. your subject suggests you want to call python
from mel, but in you're example you're using python to execute mel
code which executes python code. why not just execute the python code
in python?

if you are talking about using mel inside python, you should check out
pymel. while farsheed is right in that not all python data structures
can be converted to mel, you can get a lot done with those structures
that do convert. the syntax is much nicer with pymel because it
handles all the formatting and conversions for you. you can call mel
procs as if they were python functions.

import pymel
data = ['one', 'two', 'three']
pymel.mel.myProc( data )

versus:

import maya.mel
data = ['one', 'two', 'three']
maya.mel.eval( "myProc( [%s] )" % ','.join( map( lambda x: "'%s'" % x,
data ) ) )

-chad

Ofer Koren

unread,
Jan 22, 2008, 2:44:02 AM1/22/08
to python_in...@googlegroups.com
Sorry, I didn't explain this well I guess.
What I'm trying to do is 'expose' a python function to the Mel interperter in a completely transparent way. Exposing it will be done from the python module that defines that python function. Mel scripts that use this function should be unaware that it actually runs python code. This way I could embed python functionality into existing mel code (specifically "hooks", or function-callbacks that expect mel procs)
I've actually managed to create a generic method for this. Here's an [not-so-very-useful] example of how it can be used:


----------------sampleModule.py:
def pySampleFunc(arr, cnt):
   return arr[:cnt]
exportToMel(funcMelName="melSampleFunc", retType="string[]", argType=["string[]","int"], func=pySampleFunc)


----------------sampleScript.mel:
string $ret[] = melSampleFunc({"a","b","c","d"},3);
# {"a","b","c"}
--


- Ofer
www.mrbroken.com

Chadrik

unread,
Jan 22, 2008, 1:51:23 PM1/22/08
to python_inside_maya

there is a file in pymel called pluginFactory.py. it aids in
converting a python class into a mel command. for example, this test
class:

class test(object):
__init__(self, arg ):
self.arg = arg
doThis(self, funcArg)
print self.arg, funcArg

would become this mel command:

test -doThis "this is a flag arg" "this is a command arg"


every class method becomes a flag with its own arguments. the
commands are just wrapping the python code, so every time you change
your class, the mel commands are updated as well.

I never supplied an example of how to use this, but i will do so with
the next release of pymel. if this sounds like something you'd like
to use, i can send you an example in the meantime.

-chad

On Jan 21, 11:44 pm, "Ofer Koren" <kor...@gmail.com> wrote:
> Sorry, I didn't explain this well I guess.
> What I'm trying to do is 'expose' a python function to the Mel interperter
> in a completely transparent way. Exposing it will be done from the python
> module that defines that python function. Mel scripts that use this function
> should be unaware that it actually runs python code. This way I could embed
> python functionality into existing mel code (specifically "hooks", or
> function-callbacks that expect mel procs)
> I've actually managed to create a generic method for this. Here's an
> [not-so-very-useful] example of how it can be used:
>
> ----------------sampleModule.py:
> def pySampleFunc(arr, cnt):
>    return arr[:cnt]
> exportToMel(funcMelName="melSampleFunc", retType="string[]",
> argType=["string[]","int"], func=pySampleFunc)
>
> ----------------sampleScript.mel:
> string $ret[] = melSampleFunc({"a","b","c","d"},3);
> # {"a","b","c"}
>
Reply all
Reply to author
Forward
0 new messages