Evergreen: Pass Python Variable to mel

1,136 views
Skip to first unread message

mputtkam

unread,
May 8, 2009, 12:48:15 PM5/8/09
to python_inside_maya
Hi,

This is about passing python vars to mel using an externally sourced
python script in maya 2008:
Simplified example:

import maya.cmds as cmds
import maya.mel as mel

joints = []
for i in range(5):
joints.append(cmds.joint())

def test():
global tmp
for i in range(5):
tmp = joints[i] + '_sum_up_inputs_calc'
cmds.shadingNode('plusMinusAverage', asUtility = 1, name =
tmp)
mel.eval('string $tmp = python("tmp")')
mel.eval('AEnewNonNumericMultiAddNewItem($tmp, "input3D");')
test()

Paste it in Maya. It should work. At least it does for me. However, it
does not work for me, when I source it via the usual import/reload
procedure using an external .py. Maya will then read: "Tmp not
defined".

I do not get it. I go crazy over it, because it does not make sense.
Paste directly, works. Source externally, does not. I tried putting
global tmp all over the place, but that did not help either. If I
source the script twice, the previously errored utility will suddenly
have four(!) inputs, while the other new ones will not have any.
That's weird for sure. I do not know what else to try to get this
working...

Help is much appreciated.
Martin

Thanks,
Martin

Chad Dombrova

unread,
May 8, 2009, 1:02:18 PM5/8/09
to python_in...@googlegroups.com
it must have to do with what scope the "python" mel command is run
in. the most straightforward solution is to format the eval'd mel
string with the variable value:

mel.eval('AEnewNonNumericMultiAddNewItem("%s", "input3D");' % tmp )


alternately, if you're using pymel, you can just pass the value as if
it were a python script:

pymel.mel.AEnewNonNumericMultiAddNewItem( tmp, 'input3D' )

pymel will save you a lot of formatting head-ache when you start
passing string arrays, vectors, etc.

-chad

Martin Puttkammer

unread,
May 8, 2009, 4:18:25 PM5/8/09
to python_in...@googlegroups.com
Hi Chad,

Thanks indeed, this saved me long hours this night.

Have a nice day,
Martin


2009/5/8, Chad Dombrova <cha...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages