View Full Version : writing Expressions with MEL?

11 November 2007, 01:52 AM
Hi, I'm doing some character setup and my mel script needs to write an expression for each loop. I can't figure out how to do this properly.
This is what I have so far:

this is a rough of what I got when I made it manually in the Expression Editor (what gets echo-ed):
expression -e -s "nurbsPlane1FollicleShape1250.parameterU = null1.modPU + joint1.pU;\r\n nurbsPlane1FollicleShape1250.parameterV = null1.modPV + joint1.pV;" -o "" -ae 1 -uc all folliculeOffset;

This is what I want (I think) for each loop:
expression -e -s ($NSFName +".parameterU") = (($grp + ".offsetParaU") + ($jtName + ".paraU"));\r\n ($NSFName +".parameterV") = (($grp + ".offsetParaV") + ($jtName + ".paraV"));
" -o "" -ae 1 -uc all o;

I know I'm totally off base as I get : $NSFShapeU = $grpOPU + $jtNamePU in the expression editor instead of folliculeShape.parameterU = faceGrp.offsetParaU + faceJt.paraU
Any thoughts?

11 November 2007, 05:13 AM
Variables from outside the expression must be written in this way:

int $a;
expression -s ("$expressionA = "+$a+"")

11 November 2007, 06:26 AM
okay, that's a start. But I'm not sure I completely follow. What do the plus signs do "+$a+" ??
And is the entire expression suppose to be assigned to your $a?
so if the expressions formula is:
string $x,$y,$z;
$x = $y + $z;
Does it go:
expression -s ("$expressionA = "+$x = $y + $z+" ");
string $all = ($x = $y + $z);
expression -s ("$expressionA = "+$all+" ");
Am I thinking along the right lines?
Thanks for the help BTW

11 November 2007, 08:34 AM
In my opinion the best way to add expression in mel script is like this:

take ur formula for example:

if u r using the following expression

string $x,$y,$z;
$x = $y + $z;
// i added the following to show how " works
string $cams[] = `ls -type "camera"`;

the correct way is :

// use a string to hold ur expression, u could manually add escape sequences (change " into \")
// if u have many " in ur expression, u can paste them out into a text editing editor, use search and replace function.

// after replacing the ", ur expression will look like this

string $x,$y,$z;
$x = $y + $z;
// i added the following to show how \" works
string $cams[] = `ls -type \"camera\"`;

now here comes a trick, the MEL documentation didn't mention this, i think. u can use \ to tell maya ur script definition is not finished

string $cmd = " \
string $x,$y,$z; \n\
$x = $y + $z; \n\
// i added the following to show how \" works \n\
string $cams[] = `ls -type \"camera\"`; \n\

notice the \n\ appearing in each line, \n is escape character, \ means string definition continues, it shows an error color in melstudio. but actually it works.

The Benifiit of coding like this:

1. the expression added to expression editor has the same indentation as u typed here,(u can store ur expression in comment area or another file), easier to read, that means easier to track for bugs ,and easier to maintain

2. it's also easier to read in ur mel script

if for some reason ur script need more ", u could use encodeString mel command to do the trick.(sometimes when dealing with path with whitespace or on special occasions, u'll need this command)

At last u could add ur expression easily.

expression -s $cmd;

11 November 2007, 08:23 PM
okay, so the string $cmd = "\ was a nice way to bundle it up. But I'm not transfering all the info I want to the expression.

Here's what I say:
string $cmd = "\
$NSFShapeU= $grpOPU + $jtNamePU; \n\
and the expression gets written out just like that : $NSFShapeU= $grpOPU + $jtNamePU;
But what I want it to say is the stuff assigned in the loops, which should be something like:

nurbsPlane1FollicleShape.parameterU = null1.modPU + joint1.pU;
nurbsPlane1FollicleShape.parameterV= null1.modPV + joint1.pV;

Do I need to define the arguments in the expresison as well? What am I missing?

CGTalk Moderation
11 November 2007, 08:23 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.