Re: [Maya-Python] Passing a mel variable into a Python Function

673 views
Skip to first unread message

Justin Israel

unread,
Oct 14, 2012, 12:02:47 AM10/14/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
You could just concatenate the strings to make is a literal string:

python("import ZebraRig\nZebraRig.doLeftSwitch('" + $refname + "')");

Note the single quotes to wrap around the literal value of $refname



On Oct 13, 2012, at 8:13 PM, Bay <gannbo...@gmail.com> wrote:

> python("import ZebraRig\nZebraRig.doLeftSwitch($refname)");

Bay

unread,
Oct 14, 2012, 3:40:44 AM10/14/12
to python_in...@googlegroups.com
Thank you so much for the reply. It looks like the syntax error i've been getting is indeed the python's script inability to read a mel variable but the problem with your solution is now it take  +$refrname+ literally, rather than the value in it but at least now I'm able to isolate the problem and know what I did wrong. Thanks alot ! 

Justin Israel

unread,
Oct 14, 2012, 10:13:35 AM10/14/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
The + $refName + should be outside of all quotes so that it is a mel operation of adding strings. You probably left it inside of quotes and are letting python see that literally for evaluation. 

Bay

unread,
Oct 14, 2012, 1:34:16 PM10/14/12
to python_in...@googlegroups.com
Hrm...then that presents a problem because the command is supposed to call a definition that takes in that value. How could I achieve that if I had to take away the quotes? Am i reading your example wrong? It's 3 single quotes yes? I tried different combinations to no avail.
 
Thanks so much for continuing to reply though, it's a great help
 
This is the code I'm still using.
 python("import ZebraRig\nZebraRig.doLeftSwitch('''+$refname+''')");
 
I''ve see the eval command being used in parasing a mel string into a python command but I'm still figuring out how i could do it.
 
Thank you

Justin Israel

unread,
Oct 14, 2012, 3:38:58 PM10/14/12
to python_in...@googlegroups.com
Ah yes. The quotes didn't read very easily, sorry.

<SQ> = single quote
<DQ> = double quote

python(<DQ>import ZebraRig\nZebraRig.doLeftSwitch(<SQ><DQ> + $refname + <DQ><SQ>)<DQ>);

What you are doing is just wrapping the value of $refname in single quotes, so that when python see it, it will be:
(assuming $refname = "fooName")

import ZebraRig
ZebraRig.doLeftSwitch('fooName')


MEL and Python are two separate environments, so they cannot see each others varaible, unless you use something like this, or an intermediate storage like optionVar. But this string concat should work. it was just a misunderstanding of the format.
Reply all
Reply to author
Forward
0 new messages