Dashboard_avatar
Jul 20, 2012
Post id: 310365 Report Item

Let me preface this with the fact that I am EXTREMELY new to MEL scripting, and know almost nothing of the syntax or even how to properly set up what I'd like to do. I've managed to make more sense of the Expression editor as it feels much more intuitive than MEL scripting, particularly when creating animations over time.

That said, I need to be able to use a MEL script that will create an expression, mainly so that I can assign similar expressions to multiple objects, and only have to change a variable in one place in the script, rather than having to update every expression when I make a change.

Here's the original expression for one of the objects that works exactly as I would like it to:

$A = 1.5; 
$B = 1.5; 
$b = deg_to_rad(0); 
$b1 = deg_to_rad(360*time*4); 
$b2 = $b1/2; 
$s = 11; 

if (time > 5) 
$b = (deg_to_rad(36*time*4)); 

if (time > $s)
 
$b = deg_to_rad(36*$s*4); 

SphereLeft.translateZ = cos($b)*(($A*sin($b1))+($B*sin($b2)));
 

Now I would like to do exactly this, but instead of using an expression, I would like to start the scene with no animations or expressions, and execute a MEL script to create exactly this animation over time. I've tried several different things, and I always get an error, or I'll manage to get the script to create the expression, but the expression doesn't do anything, it just keeps translateZ at 0 every frame...

This is what I have as a script so far:

float $A = 1.5;
float $B = 1.5;
int $time = `currentTime -q`;
float $b = deg_to_rad (0);
float $b1 = deg_to_rad(360*($time/(float)24)*4);
float $b2 = $b1/2;
int $s = 11;

if (($time/(float)24) > 5) $b = (deg_to_rad(36*($time/(float)24)*4));
if (($time/(float)24) > $s) $b = deg_to_rad(36*$s*4);

float $L = cos($b)*(($A*sin($b1))+($B*sin($b2)));

expression -string "SphereLeft.translateZ = $L";
 

If you print out variable $L, it returns the correct number for each frame of animation, so I know everything is working correctly right up until I actually want to make an expression. However, the expression, even with $L, does not change or update at all over time. What am I missing? Perhaps I just really don't understand how scripting works vs. expressions...

Thanks ahead of time for any help! 

Dashboard_avatar
Jul 20, 2012
Post id: 310369 Report Item

>> $b1 = deg_to_rad(360*time*4);

you assign the value of the calculation of deg_to_rad(360*time*4) into $b. Whereas you want to inert teh calcuation into the expression. Do:

$b1 = "deg_to_rad(360*time*4)";

different thing.

Dashboard_avatar
Jul 23, 2012
Post id: 310385 Report Item

Thanks for the reply, Joojaa. Hm I tried using quotes around the variables that included time calculations, but the script still created expressions that always stayed at 0 regardless of the frame. Just in case I tried quotes on all variables that required calculations, but I still get the same 0 result in the translateZ. Do I have to re-declare variables in the expression string, or include all the time calculations there instead of above?

I'm so confused with all of this :/ 

Dashboard_avatar
Jul 24, 2012
Post id: 310388 Report Item

Yes the local variables wont carry over, globals will tough*. The expression is a completely separate script and you should treat it that way.


basically you want your script to look as follows:












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


$xpr = (
"float $A = 1.5;\n"+
"float $B = 1.5;\n"+
"float $b = deg_to_rad (0);\n"+
"float $b1 = deg_to_rad(360*(time/(float)24)*4);\n"+
"float $b2 = $b1/2;\n"+
"int $s = 11;\n"+
"\n"+
"if ((time/(float)24) > 5) "+
"$b = (deg_to_rad(36*(time/(float)24)*4));\n"+
"if ((time/(float)24) > $s)"+
"$b = deg_to_rad(36*$s*4);\n"+
"\n"+
"SphereLeft.translateZ = cos($b)*(($A*sin($b1))+($B*sin($b2)));\n"
);
 
expression -string $xpr;




PLEASE NOTE: Expressions are NOT MEL, its important to understand this or you will have a lot of trouble. They can execute MEL but they are not supposed to. One of the big differences is that you should get values form nodes such as using time instead of `currentTime -q`.


* but you shouldn't have any, you should use attributes to store your expression variables. The purpose of expressions is to make nodes and not script elements. Attributes are the storage mechanism of nodes.

Dashboard_avatar
Jul 24, 2012
Post id: 310390 Report Item

Thanks so much for the help! The script does work as intended now, thank you.

I'm beginning to understand the differences between expressions and MEL, though in some cases it seems like it would be ideal to combine both to solve a problem. That was the only reason I brought up global variables. My boss wants to basically use this expression but apply slightly different math (albeit using the same variables) to other objects simultaneously, and he only wants to change one set of variables to try different results instead of changing every variable in each expression all the time. Perhaps there's a better way to achieve this instead of using global variables?

Thank you for being so patient with me here.