dynamic expressions? implimenting python in MEL native maya script ?

734 views
Skip to first unread message

pric...@googlemail.com

unread,
Mar 11, 2009, 12:17:05 AM3/11/09
to python_inside_maya

Hi Guys,
this is probably one of those questions that has been asked a
million times but meh, dynamic expressions as we all know are written
in maya, as mel expressions, and to use these in python we simply
write it as MEL in brackets and all is dandy. except how do we edit
the contents of these expressions? can we even do so ? as they are MEL
scripts i am unsure. i thought the MEL.EVAL might be of help but am
unsure.

Example:....

mc.dynExpression('%s'%spriteOutParticleName,s="spriteScaleXPP=rand
(0.5,1.8);\r\nspriteScaleYPP=spriteScaleXPP;\r\nspriteSpin=rand
(-90-45);\r\nspriteTwistPP=rand(-90,90);",c=1)

basically how can i edit the values inside the " ", inserting
variables etc. HELP !


so i surrender this to the PYTHON gods, how do we edit MEL dynamic
expresssions, through python ?

As an offering to the said gods i present the solution to
another python/Mel problem

if you need the outliner in your GUI, which is quite useful, and need
a command to be executed whenever the user clicks on an element with
in the outliner... you can t. it will only execute mel commands. weird
no? after a few hours of pointlessly playing i ve solved this issue by
using a mel wrapper. this is just one example but the principle is the
same

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

def tester():
print "hello"

mel.eval( " global proc wrapper() { python( \"tester()\" ); } " )

cmds.window()
cmds.frameLayout( labelVisible=False )
panel = cmds.outlinerPanel()
outliner = cmds.outlinerPanel(panel, query=True,outlinerEditor=True)
cmds.outlinerEditor( outliner, edit=True, sec="wrapper()")
cmds.showWindow()

just thought i d share it

Paul Molodowitch

unread,
Mar 12, 2009, 1:31:25 PM3/12/09
to python_in...@googlegroups.com
Unfortunately, there are still a bunch of places in maya that only
take MEL (and not python) - expressions being a big culprit.

My general approach when I have to do stick in some mel code into a
python script is to have the mel call a python function, instead of
putting in a mel literal string.

ie, instead of:

Method 1 - Raw String:
=================

melCommand = r'''
$someVal = $someOtherVal
'''

I might do:

Method 2 - Call python function:
=======================

melCommand = r'''python("import myCommand;myCommand.go()")'''

However, expressions are actually their own language, similar but
somewhat different from mel... and I seem to recall that last time I
tried to mix python and expressions, there were some additional
complications... something to do with imports, I think? So, trying to
do Method 2 may end up being more trouble than it's worth, especially
if it's a very simple expression.

If you want to use a mel string, but need to dynamically change it,
you'll have to fall back on string formatting operations, I'm afraid.
Ie:

expressionCommand = r'''
spriteScaleXPP=rand(%f,%f);
spriteScaleYPP=spriteScaleXPP;
spriteSpin=rand(%f);
spriteTwistPP=rand(%f,%f);''' % (scaleMin, scaleMax, spinMax,
twistMin, twistMax)

(Look up Section "3.6.2 String Formatting" in the python help for details.)

Note that, for readability, using the r (for a raw string literal),
and triple quoting it can help a lot (see section "2.4.1 String
Literals" in the python help for details).

Finally, as regards your outlinerEditor problem - maya GUI callback
CAN generally run python code, but there's some weird issues to work
around. For details, see this topic:

http://groups.google.com/group/python_inside_maya/browse_thread/thread/4fe55cd54ebb822/7fffe668f7b814e1?lnk=gst&q=gui+callback#7fffe668f7b814e1

...and look for Ofer Koren's post, which actually quotes the pymel docs.

Good luck!

- Paul

Nikhil Deshmukh

unread,
Jun 23, 2022, 3:06:21 PM6/23/22
to Python Programming for Autodesk Maya
Hi,
I'm also havung the same issue. i want to create a expression to give the rotation values of a locator to a selected control of a rig. the rig name may vary.

import maya.cmds as mc
cntl = mc.ls(selection=True)[0]
print(cntl)
loc1 = mc.spaceLocator()[0]
print(loc)
mc.expression( s = ('loc1 + ".RotateX" = cntl + ".RotateX"') )

can you please help me understan the procedure
Reply all
Reply to author
Forward
0 new messages