maya.mel.eval() problem

676 views
Skip to first unread message

Ling

unread,
Feb 7, 2011, 9:22:22 PM2/7/11
to python_inside_maya

hey guys:

have a problem with maya.mel.eval() today..

cmd = 'python(\"mayaString = customClass()\");'
cmd += 'python(\"mayaString.doit()\");'

maya.mel.eval(cmd)

# mayaString is an instance of a custom class, and it will error out
like saying

customClass is not defined... any work around to deal with this??

thanks a lot in advance!!

-ling

Adam Mechtley

unread,
Feb 7, 2011, 9:55:13 PM2/7/11
to python_in...@googlegroups.com
Maybe a dumb question, but is your customClass available in __main__?


damon shelton

unread,
Feb 7, 2011, 9:59:52 PM2/7/11
to python_in...@googlegroups.com
you need to have import customClass?



Chad Dombrova

unread,
Feb 7, 2011, 10:59:47 PM2/7/11
to python_in...@googlegroups.com
>
> cmd = 'python(\"mayaString = customClass()\");'
> cmd += 'python(\"mayaString.doit()\");'
>
> maya.mel.eval(cmd)

wait, i don't get it. why are you using maya.mel.eval to run python code?

-chad

C. B. Esquire

unread,
Feb 7, 2011, 11:27:42 PM2/7/11
to python_in...@googlegroups.com
if youre in a python module, which is overriding mel procs, this is a perfectly good reason to use python calls in the proc override
if this is the case, you you can import the module in the same call, to throw to maya.mel to eval ~

cmd = 'python("import sys;sys.path.append(\'/the/path/to/your/script\');import yourModule; mayaString=customClass();mayaString.doit()");'
mm.eval(cmd)




Ling

unread,
Feb 8, 2011, 12:43:24 AM2/8/11
to python_inside_maya
Thanks for the replay guys:

@Adam, I am not sure, will give it a try..

@Chad, the reason I am doing this is hoping if I can have some better
call back if hit "ctrl-Z" in maya.
I could just run the python command directly, but the ctrl+z would
only undo the last step defined in the customClass().doIt()

@C. B. Esquire: currently the customClass is in the same file as the
caller functions,
but I will try to make it a separate file and see how it works, thanks
for the suggestion!


thanks again! will report after try it in the morning!

-ling


On Feb 7, 8:27 pm, "C. B. Esquire" <crackerbu...@gmail.com> wrote:
> if youre in a python module, which is overriding mel procs, this is a
> perfectly good reason to use python calls in the proc override
> if this is the case, you you can import the module in the same call, to
> throw to maya.mel to eval ~
>
> cmd = 'python("import
> sys;sys.path.append(\'/the/path/to/your/script\');import yourModule;
> mayaString=customClass();mayaString.doit()");'
> mm.eval(cmd)
>

Ofer Koren

unread,
Feb 8, 2011, 3:38:02 AM2/8/11
to python_in...@googlegroups.com
When you call the 'python' mel-command, you're essentially running that python code in the "__main__" module (the script editor window). This means that only variables/functions defined in that __main__ module will be available to the python call, and definitions made in the module where the python call is made will not.
So you'll have to refer to your module where your definitions exist in order to use them. You can do this generically like so:

cmd = "import %s; mayaString=%s.customClass(); mayaString.doit()" % (__name__, __name__)
mm.eval('python("%s")' % cmd)

(beware of sys.path.append(...) as it will keep adding to your python path in each cmd call and slow down your python import mechanism; use it only if needed - i assume that since the module is actually running it is already imported and so it probably already exists in the python path)

- Ofer
www.mrbroken.com


Ling

unread,
Feb 8, 2011, 2:11:02 PM2/8/11
to python_inside_maya
put the custom class in a separate file worked!!

and using the maya.mel.eval() call makes the ctrl+z undo the whole
thing! sweeeet~


thanks a lot guys!

-ling

On Feb 8, 12:38 am, Ofer Koren <kor...@gmail.com> wrote:
> When you call the 'python' mel-command, you're essentially running that
> python code in the "__main__" module (the script editor window). This means
> that only variables/functions defined in that __main__ module will be
> available to the python call, and definitions made in the module where the
> python call is made will not.
> So you'll have to refer to your module where your definitions exist in order
> to use them. You can do this generically like so:
>
> cmd = "import %s; mayaString=%s.customClass(); mayaString.doit()" %
> (__name__, __name__)
> mm.eval('python("%s")' % cmd)
>
> (beware of sys.path.append(...) as it will keep adding to your python path
> in each cmd call and slow down your python import mechanism; use it only if
> needed - i assume that since the module is actually running it is already
> imported and so it probably already exists in the python path)
>
> - Oferwww.mrbroken.com
>

C. B. Esquire

unread,
Feb 10, 2011, 7:41:27 AM2/10/11
to python_in...@googlegroups.com
glad to know it worked,

Ofer is absolutely correct, appending or inserting (  sys.path.insert(0, yourModulePathHere)  ) will just keep adding it
however, One quick now Ling, that code I gave was meant to be run (and will work) within the module it's importing (I know that sounds strange)


Jefri Yeh

unread,
Feb 11, 2011, 2:49:34 AM2/11/11
to python_in...@googlegroups.com
if you want a mel-like undo queue, just use undoInfo(openChunk=1) and undoInfo(closeChunk=1) to wrap the code that affect the queue.


Ling

unread,
Feb 12, 2011, 2:07:48 PM2/12/11
to python_inside_maya
@C.B.Esquire
Thanks! we have some relative import modules, so that wasn't really a
big prob for me.

@Jefri:
Ha, good know, tried and works like a charm!
thanks a lot!

On Feb 10, 11:49 pm, Jefri Yeh <r4inm4...@gmail.com> wrote:
> if you want a mel-like undo queue, just use undoInfo(openChunk=1) and
> undoInfo(closeChunk=1) to wrap the code that affect the queue.
>
> On Thu, Feb 10, 2011 at 8:41 PM, C. B. Esquire <crackerbu...@gmail.com>wrote:
>
> > glad to know it worked,
>
> > Ofer is absolutely correct, appending or inserting (  sys.path.insert(0,
> > yourModulePathHere)  ) will just keep adding it
> > however, One quick now Ling, that code I gave was meant to be run (and will
> > work) within the module it's importing (I know that sounds strange)
>
Reply all
Reply to author
Forward
0 new messages